import { GlobalVariables } from "../Data/GlobalVariables";
import LocalStorage from "../Data/LocalStorage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StartScene extends cc.Component {
  @property(cc.SpriteFrame) soundOffSprite: cc.SpriteFrame = null;
  @property(cc.SpriteFrame) soundOnSprite: cc.SpriteFrame = null;
  @property(cc.AudioClip) soundIntro: cc.AudioClip = null;
  @property(cc.AudioClip) soundClick: cc.AudioClip = null;
  @property(cc.Node) animNode: cc.Node = null;

  sprite: cc.Sprite = null;
  currentSprite: cc.SpriteFrame;
  pressed: boolean = false;
  highestUnlockedLevel: number;
  currentAnimation: string = null;
  isStarted: boolean = false;

  protected onLoad(): void {
    cc.audioEngine.playEffect(this.soundIntro, true);
    if (LocalStorage.getMusic()) {
      this.currentSprite = this.soundOnSprite;
    } else {
      this.currentSprite = this.soundOffSprite;
      cc.audioEngine.setMusicVolume(0);
      cc.audioEngine.setEffectsVolume(0);
    }
    this.updateButtonSprite();

    window.addEventListener("beforeunload", this.handleGameReload);
    window.addEventListener("unload", this.handleGameReload);

    this.isStarted = LocalStorage.getGameStarted();
    if (this.isStarted) this.node.getChildByName("Intro").active = false;

    this.highestUnlockedLevel = LocalStorage.getHighestLevel();
    LocalStorage.getCurrentLevel();
    this.setPosAnimation();
    this.updateAllStarLabel();
    this.currentAnimation = "DogRun";
    this.animNode
      .getComponent(cc.Animation)
      .on("lastframe", this.onLastFrame, this);
    this.node
      .getChildByName("New Node")
      .getChildByName("CoinLabel")
      .getComponent(cc.Label).string = `x${LocalStorage.getAllCoin()}`;
  }

  handleGameReload() {
    LocalStorage.setGameStarted(false);
  }

  onDestroy() {
    window.removeEventListener("beforeunload", this.handleGameReload);
    window.removeEventListener("unload", this.handleGameReload);
  }

  onClickOpenGame() {
    LocalStorage.setGameStarted(true);
    this.node
      .getChildByName("Intro")
      .getComponent(cc.Animation)
      .play("Opening");

    this.scheduleOnce(() => {
      this.node.getChildByName("Intro").active = false;
    }, 0.6);
  }

  updateAllStarLabel() {
    let sum = 0;
    for (const item of LocalStorage.getAllStars()) {
      if (item.hasOwnProperty("star")) {
        sum += item.star;
      }
    }
    this.node
      .getChildByName("StarLabel")
      .getComponent(cc.Label).string = `${sum}/90`;
  }

  onLastFrame() {
    if (this.currentAnimation === "DogRun") {
      this.currentAnimation = "DogRolling";
    } else {
      this.currentAnimation = "DogRun";
    }

    this.animNode
      .getChildByName("Dog")
      .getComponent(cc.Animation)
      .play(this.currentAnimation);
  }

  setPosAnimation() {
    const startPos = -cc.winSize.width / 2 + this.animNode.x;
    const endPos = cc.winSize.width / 2 - this.animNode.x;
    const frameAnim = this.animNode.getComponent(cc.Animation).defaultClip
      .curveData.props;

    frameAnim.x[0].value = startPos;
    frameAnim.x[1].value = endPos + 150;
    frameAnim.x[2].value = endPos;
    frameAnim.x[3].value = startPos - 150;

    if (startPos <= 1000)
      this.animNode.getComponent(cc.Animation).defaultClip.speed = 0.4;
  }

  onClickButtonSound() {
    cc.audioEngine.playEffect(this.soundClick, false);

    if (this.currentSprite === this.soundOnSprite) {
      this.currentSprite = this.soundOffSprite;
      cc.audioEngine.setMusicVolume(0);
      cc.audioEngine.setEffectsVolume(0);
      LocalStorage.setMusic(false);
    } else {
      this.currentSprite = this.soundOnSprite;
      cc.audioEngine.setMusicVolume(1);
      cc.audioEngine.setEffectsVolume(1);
      LocalStorage.setMusic(true);
    }
    this.updateButtonSprite();
  }

  onClickToLevels() {
    cc.audioEngine.playEffect(this.soundClick, false);
    cc.director.loadScene("levels");
  }

  onClickToShop() {
    cc.audioEngine.playEffect(this.soundClick, false);
    cc.director.loadScene("shop");
  }

  updateButtonSprite() {
    const button = this.node
      .getChildByName("ButtonSound")
      .getComponent(cc.Button);
    const sprite = button.node.getComponent(cc.Sprite);
    sprite.spriteFrame = this.currentSprite;
  }

  onClickButtonPlay() {
    cc.audioEngine.playEffect(this.soundClick, false);
    GlobalVariables.levelToLoad = this.highestUnlockedLevel - 1;
    LocalStorage.setCurrentLevel(this.highestUnlockedLevel - 1);
    if (this.highestUnlockedLevel == 31) {
      GlobalVariables.levelToLoad = 0;
      LocalStorage.setCurrentLevel(0);
    }
    cc.director.loadScene("gameplay");
  }
}
