import { find, Node, sp, _decorator } from 'cc';
import { BaseLayer } from '../../common/BaseLayer';
import { cocosUtil } from '../../utils/cocosUtil';
import { constants } from '../data/constants';
import { localText } from '../data/localText';
import { msgac } from '../data/msgac';
import { audioManager } from '../manager/audioManager';
import { levelManager } from '../manager/levelManager';
import { sdkManager } from '../manager/sdkManager';
import { playerModel } from '../model/playerModel';
import { FlutterEffect } from './effect/FlutterEffect';
const { ccclass, property } = _decorator;

@ccclass('StartLayer')
export class StartLayer extends BaseLayer {

    coinNode: Node;
    levelNode: Node;
    itemList: Node;
    bossEyeNode: Node;
    roleBodySkeleon: sp.Skeleton;

    turnTableNode: Node;
    turnTableShine: Node;

    startTextHint: Node;
    btnLevelEsp: Node;
    btnSkip: Node;

    audioCloseNode: Node;
    audioOpenNode: Node;

    onLoad() {
        super.onLoad();

        this.coinNode = this.getNodeByPath("coinLayer/num");
        this.levelNode = this.getNodeByPath("level/level");

        this.audioCloseNode = this.getNodeByPath("btnAudio/close");
        this.audioOpenNode = this.getNodeByPath("btnAudio/open");

        this.roleBodySkeleon = this.getNodeByPath("tribe/body").getComponent(sp.Skeleton);

        this.turnTableNode = this.getNodeByPath("btns/btnTurnTable/turnTable");
        this.turnTableShine = this.getNodeByPath("btns/btnTurnTable/bg");

        this.itemList = this.getNodeByPath("level/itemList");
        let chs = this.itemList.children;
        this.bossEyeNode = find("eye", chs[chs.length - 1]);

        this.getNodeByPath("cloundLayer").addComponent(FlutterEffect);

        this.startTextHint = this.getNodeByPath("gameStart");
        this.btnLevelEsp = this.getNodeByPath("btnLevelEsp");
        this.btnSkip = this.getNodeByPath("btnSkip");

        this.initUI();

        if (this.obj && this.obj.openLevelEspSelectLayer) {
            this.openLayer(constants.layers.LevelEspSelectLayer);
        }

        audioManager.instance.playMusic(constants.audioNames.bgm);

        sdkManager.instance.showBannerAd();
        sdkManager.instance.showCustomAdCommon();
        sdkManager.instance.onStartLayerShow();
        sdkManager.instance.sendEvent("进入游戏主界面");
    }

    start() {
        this.scheduleOnce(() => {
            let coinLayer = this.getNodeByPath("coinLayer");
            playerModel.instance.coinPosEnd = cocosUtil.convertToWorldSpace(coinLayer);
        });
    }

    onEnable() {
        this.addEventArr([
            msgac.openLevelLayer,
            msgac.coinChange,
        ]);
    }

    onDisable() {
        this.removeEventArr([
            msgac.openLevelLayer,
            msgac.coinChange,
        ]);
    }

    initUI() {
        this.initLocaltext();
        this.initEffect();
        this.refreshCoinLayer();
        this.refreshBtnAudio();
        this.refreshRoleBodyLayer();

        let level = playerModel.instance.getLevel();
        this.setString(this.levelNode, level);
        level = level % 10;
        if (level == 0) {
            level = 10;
        }
        let chs = this.itemList.children;
        for (let i = 0, len = chs.length; i < len; i++) {
            let tmp = i + 1;
            if (tmp == 10) {
                continue;
            }
            let node = chs[i];
            let noNode = find("no", node);
            let hasNode = find("has", node);
            let currentNode = find("current", node);
            noNode.active = false;
            hasNode.active = false;
            currentNode.active = false;
            if (level > tmp) {
                hasNode.active = true;
            } else if (level == tmp) {
                currentNode.active = true;
            } else {
                noNode.active = true;
            }
        }
    }

    refreshRoleBodyLayer() {
        let skinName = levelManager.instance.getSkinName(playerModel.instance.getEquipWeaponId(), playerModel.instance.getEquipDressId());
        this.roleBodySkeleon.setSkin(skinName);
    }

    initLocaltext() {
        this.setString(this.startTextHint, localText.textObj.clickGameStart);
    }

    initEffect() {
        cocosUtil.tweenScaleBreath(this.startTextHint);
        cocosUtil.tweenShake(this.btnLevelEsp);
        cocosUtil.tweenScaleBreath(this.btnSkip);
        cocosUtil.tweenScaleBreath(this.turnTableShine, 0.3);
        cocosUtil.tweenRotate(this.turnTableNode);
        cocosUtil.tweenBlink(this.bossEyeNode);
    }

    refreshBtnAudio() {
        if (audioManager.instance.getMusiceVolume() > 0) {
            this.audioCloseNode.active = false;
            this.audioOpenNode.active = true;
        } else {
            this.audioCloseNode.active = true;
            this.audioOpenNode.active = false;
        }
    }

    refreshCoinLayer() {
        this.setString(this.coinNode, playerModel.instance.getCoin());
    }

    openLevelLayerRet(data: object) {
        this.openLayer(constants.layers.LevelLayer, data);
        this.closeLayer();
    }

    coinChangeRet(data: object) {
        this.refreshCoinLayer();
    };

    process_event(event: any) {
        let ac = event.ac;
        let data = event.data;
        switch (ac) {
            case msgac.coinChange:
                this.coinChangeRet(data);
                break;
            case msgac.openLevelLayer:
                this.openLevelLayerRet(data);
                break;
            default:
                break;
        }
    }

    onButtonClick(node: Node, name: string) {
        switch (name) {
            case "btnCoinAdd":
                this.onClickBtnCoinAdd(node);
                break;
            case "btnSkin":
                this.onClickBtnSkin(node);
                break;
            case "btnLevelEsp":
                this.onClickBtnLevelEsp(node);
                break;
            case "btnTurnTable":
                this.onClickBtnTurnTable(node);
                break;
            case "btnAudio":
                this.onClickBtnAudio(node);
                break;
            case "bg":
                this.onClickBg(node);
                break;
            default:
                break;
        }
    }

    onClickBtnCoinAdd(node: Node) {
        this.openLayer(constants.layers.AdCoinLayer);
    }

    onClickBtnSkin(node: Node) {
        this.openLayer(constants.layers.SkinLayer);
    }

    onClickBtnLevelEsp(node: Node) {
        this.openLayer(constants.layers.LevelEspSelectLayer, null);
    }

    onClickBtnTurnTable(node: Node) {
        this.openLayer(constants.layers.TurnTableLayer);
    }

    onClickBtnAudio(node: Node) {
        audioManager.instance.switchIsMute();
        this.refreshBtnAudio();
    }

    onClickBg(node: Node) {
        let obj: any = {};
        obj.level = playerModel.instance.getLevel();
        this.openLayer(constants.layers.LevelLayer, obj);
        this.closeLayer();
        sdkManager.instance.sendEvent("开始游戏");
    }

}

