import Scene from "./Scene";
import {AutoSpinsSettings, BetPicker, GameSettings, InfoBar} from "../components";
import Locator from "../services/Locator";


const ATTACHMENT_OPTIONS = {resolution: 2};

export class UI extends Scene {
  constructor() {
    super();
    this.zIndex = 4;
    this.name = 'Main UI';

    /**
     *
     * @type {UIViewModel}
     */
    this.inputs = {
      title: "game_name",
      balance: "0",
      win: 0,
      totalWin: 0,

      // Buy Bonus
      canBuyBonus: false,
      // Bets
      bet: "0",
      bets: [],
      isMinBet: false,
      isMaxBet: false,
      areBetsLocked: false,
      // Turbo mode
      isTurboModeActive: false,
      // Casino rounds indicator flags and props
      isCasinoRoundsActive: false,
      casinoRoundsIssued: 0,
      casinoRoundsLeft: 0,
      // Bonus game indicator flags and properties
      bonusGameActive: false,
      bonusGameIndicatorTitle: "",
      bonusGameIndicatorIssued: 0,
      bonusGameIndicatorLeft: 0,
      // Sound button flags
      isMute: false,
      // Menu flags
      isMenuShown: false,
      // Buy bonus flags
      betFeatureActive: false,
      isBuyBonusButtonPressed: false,
      // Buttons flags
      isMinusButtonPressed: false,
      isPlusButtonPressed: false,
      isMaxButtonPressed: false,
      isTurboButtonPressed: false,
      isSoundOnButtonPressed: false,
      isSoundOffButtonPressed: false,
      // Auto game settings and flags
      isStopAutoGameButtonPressed: false,
      isAdvancedAutoGameSettings: false,
      isAnyWinChecked: false,
      isSingleWinExceedsChecked: false,
      isBonusWonChecked: false,
      isBalanceIncreaseChecked: false,
      isBalanceDecreaseCheckbox: false,
      winExceedsTextField: "",
      balanceIncreaseTextField: "",
      balanceDecreaseTextField: "",
      isAutoGameAvailable: false,
      isAutoGameActive: false,
      autoRoundsLeft: "",
      autoRoundsIssued: "0",
      isAutoGameSettingsModeButtonPressed: false,
      isAutoGameSettingsShown: false,
      isAutoGameButtonPressed: false,
      isSpinButtonPressed: false,
      isRoundStarted: false,
      // Settings inputs
      isSettingsShown: false,
      isSettingsButtonPressed: false,
      isUserInputLocked: false,
    };
  }

  createInfoBar() {
    return new InfoBar(this.inputs, ATTACHMENT_OPTIONS);
  }

  createBetPicker() {
    return new BetPicker(ATTACHMENT_OPTIONS);
  }

  createAutoSpinsSettings() {
    return new AutoSpinsSettings(this.inputs, ATTACHMENT_OPTIONS);
  }

  createGameSettings() {
    return new GameSettings(this.inputs, ATTACHMENT_OPTIONS);
  }

  create() {
    this.infoBar = this.createInfoBar();
    this.infoBar.name = 'InfoBar';
    this.betPicker = this.createBetPicker();
    this.betPicker.name = 'BetPicker';
    this.autospins = this.createAutoSpinsSettings();
    this.autospins.name = 'Autospins';
    this.settings = this.createGameSettings();
    this.settings.name = 'Settings';
    this.addChild(
      this.infoBar,
      this.betPicker,
      this.autospins,
      this.settings
    );
    this.infoBar.on('infobar/bet_indicator/down', this.handleBetIndicatorDown, this);
    this.infoBar.on('infobar/bet_btn/down', this.handleBetIndicatorDown, this);
    this.infoBar.on('infobar/bet_indicator/up', this.handleBetIndicatorUp, this);
    this.infoBar.on('infobar/bet_btn/down', this.handleBetIndicatorDown, this);
    this.infoBar.on('infobar/bet_btn/up', this.handleBetIndicatorUp, this);
    this.betPicker.on('pick', this.handlePickBet, this);
  }

  run() {
    super.run();
  }

  update(dt) {
    this.eventMode = this.inputs.isUserInputLocked ? 'none' : 'auto';
    this.infoBar.update();
    this.autospins.update(dt);
    this.settings.update();
    this.betPicker.update();
  }

  stop() {
    this.infoBar.destroy();
    this.betPicker.destroy();
    this.autospins.destroy();
    this.settings.destroy();
    super.stop();
  }

  resize() {
    this.infoBar.resize();
    this.betPicker.resize();
    this.autospins.resize();
    this.settings.resize();
  }

  handleBetIndicatorDown() {
    this._skipGlobal = true;
  }

  handleBetIndicatorUp() {
    if (this.betPicker.visible) {
      this.hideBetPicker();
    } else {
      this.showBetPicker();
    }
    this._skipGlobal = false;
  }

  showBetPicker() {
    Locator.events.on('globalpointerup', this.globalUpHandler, this);
    this.betPicker.createButtons(this.inputs.bets);
    this.betPicker.show();
  }

  hideBetPicker() {
    this.betPicker.hide();
    Locator.events.off('globalpointerup', this.globalUpHandler, this);
  }

  handlePickBet(e) {
    this.inputs.bet = e.index;
  }

  globalUpHandler = (e) => {
    if (this._skipGlobal) return;
    if (!this.betPicker.isClickOnPicker(e)) {
      this.hideBetPicker();
    }
  };
}
