import * as DivCommon from './div-page-common';

export default function GameCreationDivPage(element, listener) {

  const id='game-creation';
  const className=GameCreationDivPage.name;

  let div = element.getChildByID(id);
  let divSelectDeckMode = element.getChildByID("deck-mode-select");
  let connectButton = element.getChildByName('loginButton');
  let username = element.getChildByName('username');
  let connectionStatus = element.getChildByID('connectionStatus');
  let onlineGame = element.getChildByName('onlineGame');
  let createGameButton = element.getChildByName('validGameCreation');

  let deckModeSelected = null;
  let advancedOptions = [];

  let clickEventListener = function(event) {
    let eventName = event.target.name;
    switch (eventName) {
        case "backToPrev":
          listener.onGoPreviousPage(gameCreationDivPage);
          break;

        case "validGameCreation":
          console.log(deckModeSelected);
          console.log(advancedOptions);
          listener.onValidGameCreation(deckModeSelected, advancedOptions);
          break;
        case "discard":
        case "popularity":
          if(event.target.checked){
            advancedOptions.push(eventName);
          } else {
            let index = advancedOptions.indexOf(eventName);
            if (index !== -1){
              advancedOptions.splice(index, 1);
            }
          }
        break;

      default:
        break;
    }
  };

  let changeDeckEventListener = function(event) {
    let eventValue = event.target.value;
    if (eventValue !== 0) {
      createGameButton.disabled = false;
      deckModeSelected = event.target.value;
    } else {
      createGameButton.disabled = true;
      deckModeSelected = event.target.value;
    }
  };

  div.addEventListener('click',clickEventListener);

  divSelectDeckMode.addEventListener('change',changeDeckEventListener);

  let onShow = function() {

  };

  let onHide = function() {

  };

  let gameCreationDivPage = {
    className,
    div,
    onShow,
    onHide
  };
  return Object.assign(
    gameCreationDivPage,
    DivCommon.canShow(gameCreationDivPage),
    DivCommon.canHide(gameCreationDivPage)
  );

}



