import { showInterstitialAd } from "../../adsense-h5g-api/h5_games_ads";
import { InterstitialType } from "../../adsense-h5g-api/interstitial_type";
import LocalStorage from "../Data/LocalStorage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SkinController extends cc.Component {
  @property(cc.Node) coinLabel: cc.Node = null;
  @property(cc.Node) playerSkins: cc.Node = null;
  @property(cc.Node) enemySkins: cc.Node = null;
  @property(cc.AudioClip) soundClick: cc.AudioClip = null;
  @property(cc.Node) adsBgr: cc.Node = null;
  @property(cc.Node) notiLabel: cc.Node = null;

  static instance: SkinController;
  allCoin: number;
  allSkins: Array<{ skin: string; available: boolean }>;

  protected onLoad(): void {
    SkinController.instance = this;

    this.allCoin = LocalStorage.getAllCoin();
    this.allSkins = LocalStorage.getAllSkins();
    this.updateStatus();
    this.loadCurrentSkin();
  }

  onPlayerClickAds() {
    showInterstitialAd(InterstitialType.START, "earn15Coin");
  }

  onClickButtonBack() {
    cc.director.loadScene("start");
    cc.audioEngine.playEffect(this.soundClick, false);
    cc.audioEngine.stopAllEffects();
  }

  onShowNotiAds(status: string) {
    if (status == "ok") {
      this.notiLabel.getComponent(cc.Label).string =
        "Received coins successfully!";
      LocalStorage.setAllCoin(LocalStorage.getAllCoin() + 15);
      this.adsBgr.active = true;
      this.notiLabel.active = true;
      this.scheduleOnce(() => {
        this.adsBgr.active = false;
        this.notiLabel.active = false;
      }, 2);
      this.allCoin = LocalStorage.getAllCoin();
      this.coinLabel.getComponent(cc.Label).string = `x${this.allCoin}`;
    }

    if (status == "cancel") {
      this.showFailedNotiAds("Receiving coins failed!");
    }

    if (status == "notReady") {
      this.showFailedNotiAds("Advertisement is preparing..!");
    }

    if (status == "frequencyCapped") {
      this.showFailedNotiAds("Ads frequency limit!");
    }
  }

  showFailedNotiAds(status: string) {
    this.notiLabel.getComponent(cc.Label).string = status;
    this.adsBgr.active = true;
    this.notiLabel.active = true;
    this.scheduleOnce(() => {
      this.adsBgr.active = false;
      this.notiLabel.active = false;
    }, 2);
  }

  loadCurrentSkin() {
    const skinPlayer = LocalStorage.getCurrentSkinPlayer();
    const enemyPlayer = LocalStorage.getCurrentSkinEnemy();

    for (let j = 0; j < this.playerSkins.children.length; j++) {
      let playerSkin = this.playerSkins.children[j];
      if (playerSkin.name === skinPlayer) {
        let selectedZone = playerSkin.getChildByName("SelectedZone");
        selectedZone.active = true;
      }
    }

    for (let k = 0; k < this.enemySkins.children.length; k++) {
      let enemySkin = this.enemySkins.children[k];
      if (enemySkin.name === enemyPlayer) {
        let selectedZone = enemySkin.getChildByName("SelectedZone");
        selectedZone.active = true;
      }
    }
  }

  updateStatus() {
    this.coinLabel.getComponent(cc.Label).string = `x${this.allCoin}`;

    for (let i = 0; i < this.allSkins.length; i++) {
      let skin = this.allSkins[i];
      if (skin.available == true) {
        for (let j = 0; j < this.playerSkins.children.length; j++) {
          let playerSkin = this.playerSkins.children[j];
          if (playerSkin.name === skin.skin) {
            let buttonBuy = playerSkin.getChildByName("ButtonBuy");
            if (buttonBuy) {
              buttonBuy.active = false;
            }
          }
        }

        for (let k = 0; k < this.enemySkins.children.length; k++) {
          let enemySkin = this.enemySkins.children[k];
          if (enemySkin.name === skin.skin) {
            let buttonBuy = enemySkin.getChildByName("ButtonBuy");
            if (buttonBuy) {
              buttonBuy.active = false;
            }
          }
        }
      }
    }
  }

  onClickBuySkin(event: cc.Event.EventTouch) {
    if (this.allCoin >= 500) {
      event.currentTarget.active = false;
      LocalStorage.setAllCoin(this.allCoin - 500);
      this.allCoin = LocalStorage.getAllCoin();
      LocalStorage.setSkinAvailability(event.currentTarget.parent.name, true);
      this.coinLabel.getComponent(cc.Label).string = `x${this.allCoin}`;
    }
  }

  onClickChooseSkin(event: cc.Event.EventTouch) {
    let skin = event.currentTarget;
    if (skin.parent.getChildByName("ButtonBuy").active == true) return;

    skin.parent.getChildByName("SelectedZone").active = true;

    if (skin.parent.parent.name == "SkinPlayers") {
      LocalStorage.setCurrentSkinPlayer(skin.parent.name);
      for (let i = 0; i < this.playerSkins.children.length; i++) {
        let playerSkin = this.playerSkins.children[i];
        if (playerSkin.name != skin.parent.name) {
          let selectedZone = playerSkin.getChildByName("SelectedZone");
          if (selectedZone) {
            selectedZone.active = false;
          }
        }
      }
    }

    if (skin.parent.parent.name == "SkinEnemys") {
      LocalStorage.setCurrentSkinEnemy(skin.parent.name);
      for (let i = 0; i < this.enemySkins.children.length; i++) {
        let enemySkin = this.enemySkins.children[i];
        if (enemySkin.name != skin.parent.name) {
          let selectedZone = enemySkin.getChildByName("SelectedZone");
          if (selectedZone) {
            selectedZone.active = false;
          }
        }
      }
    }
  }
}
