// BiPartisan Hoops (bpH) module
// bpHoops.js
   console.log('BPH version 0.0');
// 7:39 PM Sun April 17, 2022
// Written by: James D. Miller

/*
Dependencies:
   gwModule.js (gW.)
*/

var bpH = (function() {
   "use strict";
   
   // Names starting with m_ indicate module-scope globals.
   
   // m_goodShotReported gets set to false when the user launches a shot. It's true
   // here to inhibit the report for the basketball shot that starts the demo.
   var m_goodShotReported = true; 
   var m_backBoardShot = false;
   var m_hoopContact = false;
   var m_hitTopSensor = false;
   var m_hitBottomSensorFirst = false;
   
   var m_callCount = 0;
   
   var m_wallMap = {'wall5':'leftHoop', 'wall12':'rightHoop', 'wall21':'backboard', 'wall23':'bottomSensor', 'wall24':'topSensor'};
   var m_nameMap = {
      'trump2':   {'name':'Donald', 'title':'   (former President of the United States, Donald Trump)'}, 
      'biden2':   {'name':'Joe',    'title':'   (President of the United States, Joe Biden)'}, 
      'harris':   {'name':'Kamala', 'title':'   (Vice President of the United States, Kamala Harris)'}, 
      'pence':    {'name':'Mike',   'title':'   (former Vice President of the United States, Mike Pence)'}, 
      'pelosi':   {'name':'Nancy',  'title':'   (Speaker of the United States House of Representatives, Nancy Pelosi)'}, 
      'schumer':  {'name':'Chuck',  'title':'   (Majority Leader of the United States Senate, Chuck Schumer)'}, 
      'mcconnell':{'name':'Mitch',  'title':'   (Minority Leader of the United States Senate, Mitch McConnell)'}, 
      'mccarthy': {'name':'Kevin',  'title':'   (Minority Leader of the United States House of Representatives, Kevin McCarthy)'}   
   };
   
   var m_playList = [];
   var m_reportedWin = false;
   
   // module globals for objects brought in by initializeModule
   // (none)
   
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
   function initializeModule() {
   }
   
   // when the user launches a shot (see ghostBall.js)
   function resetShotState() {
      m_goodShotReported = false;
      m_backBoardShot = false;
      m_hoopContact = false;
      m_hitTopSensor = false;
      m_hitBottomSensorFirst = false;
   }
   
   function setUpPreGameHelp() {
      m_reportedWin = false;
      m_playList = [];
      for (let key in m_nameMap) {
         m_playList.push( key);
      }
      gW.messages['gameTitle'].loc_px = {'x':450,'y':350};
      
      gW.messages['help'].loc_px = {'x':75,'y':75};
      gW.messages['help'].newMessageSeries({
         1:{'tL_s':8.0, 'message':"The view:" + 
                                "\\    use the \"[base,yellow]esc[base]\" key to exit back to normal view" +
                                "\\    \"[base,yellow]v[base]\" key to return to full-screen view" + 
                                "\\    \"[base,yellow]5[base]\" key to restart this demo (including this help)" +
                                "\\    \"[base,yellow]0[base]\" key to exit and reset (nuke) everything"},
         2:{'tL_s':8.0, 'message':'Position the ball before the shot:' + 
                                '\\    hold the "[base,yellow]ctrl[base]" key down while dragging the basketball' + 
                                '\\    in a similar way, encourage one of the politicians (or me) to get into the game'},
         3:{'tL_s':8.0, 'message':"Aim your shot:" + 
                                "\\    [base,yellow]drag[base] the cursor, over, and then off the basketball, to change shot speed and direction" +
                                "\\    (you'll see the predicted path of the ball)"},
         4:{'tL_s':8.0, 'message':"Fine tune it:" + 
                                '\\    press the "[base,yellow]b[base]" key to enable finer positioning' +
                                '\\    press "[base,yellow]b[base]" again to disable it'},
         5:{'tL_s':8.0, 'message':'Shoot:' + 
                                '\\    [base,yellow]release[base] the mouse button' + 
                                '\\    (release over the basketball to cancel the shot)'},
         6:{'tL_s':12.0, 'message':'Aim for a bank shot:' + 
                                '\\    Bank shots with a face are easier if it hits the backboard with a [base,yellow]vertical[base] orientation.' + 
                                "\\    Click on a face while it's resting on the ground," + 
                                '\\    then use the [base,yellow]"c"[base] key to attach to the center (and avoid rotation).' + 
                                '\\    This will keep the orientation vertical while aiming the shot.'},
         7:{'tL_s':8.0, 'message':"Try unlocked shooting (flinging can be fun):" + 
                                 '\\   press "[base,yellow]ctrl-shift-L[base]"' + 
                                 '\\   ("[base,yellow]ctrl-shift-L[base]" again to lock it)'}
      });
   }
   
   // This gets called in gwModule for any wall collision in demo 5.e.basketball.
   function processBasketBallCollisions( wall, puck) {
      m_callCount++;
      
      // Exit if shooting a puck with no image.
      if (puck.imageID === null) return;
      
      if (m_wallMap[ wall.name] == "backboard") {
         m_backBoardShot = true;
      }
      if ((m_wallMap[ wall.name] == "leftHoop") || (m_wallMap[ wall.name] == "rightHoop")) {
         m_hoopContact = true;
      }
      if (m_wallMap[ wall.name] == "topSensor") {
         m_hitTopSensor = true;
      }
      if ((m_wallMap[ wall.name] == "bottomSensor") && ( ! m_hitTopSensor)) {
         m_hitBottomSensorFirst = true;
      }
      
      // Run final check on bottom sensor. 
      if ( (m_wallMap[ wall.name] == "bottomSensor") && ( ! m_hitBottomSensorFirst) && (m_hitTopSensor) && ( ! m_goodShotReported) ) {
         let messageString = "";
         let messageDuration = 4.0;
         
         m_playList = m_playList.filter( name => (name != puck.imageID) );
         if (m_playList.length == 0) {
            if ( ! m_reportedWin) {
               gW.messages['help'].newMessageSeries({
                  1:{'tL_s':messageDuration, 'message':"[1px Arial]."},
                  2:{'tL_s':2.0, 'message':"[35px Arial]That's pretty good coaching!"},
                  3:{'tL_s':2.0, 'message':"[35px Arial]Eight politicians,"},
                  4:{'tL_s':2.0, 'message':"[35px Arial]from both sides of the aisle,"},
                  5:{'tL_s':2.0, 'message':"[35px Arial]playing together."},
                  6:{'tL_s':0.5, 'message':"[50px Arial]      ."},
                  7:{'tL_s':0.5, 'message':"[50px Arial]     ..."},   
                  8:{'tL_s':0.5, 'message':"[50px Arial]    ....."},
                  9:{'tL_s':0.5, 'message':"[50px Arial]   ......."},
                 10:{'tL_s':0.5, 'message':"[50px Arial]    ....."},
                 11:{'tL_s':0.5, 'message':"[50px Arial]     ..."},
                 12:{'tL_s':0.5, 'message':"[50px Arial]      ."},
                 13:{'tL_s':3.0, 'message':"[30px Arial]press the 5 key to start again..."},
               });
               m_reportedWin = true;   
            }
         }
         
         let shotTypeString = ""
         if ((m_backBoardShot) && (m_hoopContact)) {
            shotTypeString = " --- bank shot rattles in";
         } else if ((m_backBoardShot) && ( ! m_hoopContact)) {
            shotTypeString = " --- clean bank shot";
         } else if (( ! m_backBoardShot) && (m_hoopContact)) {
            shotTypeString = " --- it rattles in";
         } else if (( ! m_backBoardShot) && ( ! m_hoopContact)) {
            shotTypeString = " --- swish shot";
         }
         
         if (puck.imageID == "miller") {
            messageString = "a bit sluggish there, [base,yellow]Jim[base]" + shotTypeString +
                    "\\   [30px Arial] but it counts; maybe grab some coffee";
         
         } else if (puck.imageID == "basketball") {
            messageString = "excellent shooting" + shotTypeString +
                    "\\   [30px Arial] Good choice: a real basketball. Makes sense.";
                    
         } else if (puck.imageID == "dandelion") {
            messageString = "almost too easy" + shotTypeString +
                    "\\   [30px Arial] have to hand it to you; nice shot";
                                                 
         } else {
            messageString = "good one, [base,yellow]" + m_nameMap[ puck.imageID]['name'] + "[base]" + shotTypeString + "\\[25px Arial]" + m_nameMap[ puck.imageID]['title'];
         }
         
         gW.messages['gameTitle'].newMessage( messageString, messageDuration);
         
         m_goodShotReported = true;
         
         console.log("processBasketBallCollisions call count = " + m_callCount);
      }
   }
   
   // see comments before the "return" section of gwModule.js
   return {
      // Objects
      
      // Variables
      
      // Methods
      initializeModule: initializeModule,
      
      resetShotState: resetShotState,
      setUpPreGameHelp: setUpPreGameHelp,
      processBasketBallCollisions: processBasketBallCollisions
   };

})();