import PlayerConst from "./data/PlayerConst";
import GameConst from "./data/GameConst";
import Util from "./util/Util";
import SelectSceneItem from "./view/uiItem/SelectSceneItem";
import GameMgr, { UI_NAME } from "./mgr/GameMgr";
import { EVENT_TYPE } from "../game/data/GameData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Start extends cc.Component {

    public static Inst: Start = null;
    @property(cc.Node)
    node_bg: cc.Node = null;

    @property(cc.Node)
    node_top: cc.Node = null;

    @property(cc.Node)
    node_scene: cc.Node = null;
    @property(cc.Node)
    node_leftBtn: cc.Node = null;
    @property(cc.Node)
    node_rightBtn: cc.Node = null;

    @property(cc.Node)
    node_startGameBtn: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        Start.Inst = this;
        // HttpMgr.Inst.shopGoodsList();
        // HttpMgr.Inst.shopPrizeList();
    }

    start() {

        mk.console.log(" if(cc.sys.isBrowser)", cc.sys.isBrowser);

        this.adapt();
        this.initView();
        this.initEvent();
    }

    /**适配*/
    adapt() {

        this.node_bg.width = cc.winSize.width;
        this.node_bg.height = cc.winSize.height;

        let gap = ((cc.winSize.height - 100) - 1344) * 0.5
        this.node_top.y += gap;
    }

    onEnable() {

        // if (Game.Inst && Game.Inst.node.active) {
        //     Game.Inst.restart();
        // }

        GameMgr.Inst.sendEvent(UI_NAME.Start, "进入主界面");
        //this.adapt();

        //AudioMgr.Inst.playMusic(AUDIO_CLIP_NAME.bg_start);
        mk.audio.playMusic("music_startBg");
    }

    onDisable() {

    }

    initView() {
        //this.adapt();
        this.initHead();
        this.initNickName();
        this.initCashNum();
        this.initEnergyNum();
        this.initShopScoreNum();
        this.initEnergyRecoverTime();
        this.initSelectSceneItem();
    }

    /** */
    initPlayerInfo() {

    }

    /**初始化头像 */
    initHead() {
        mk.console.log("开始界面初始化 Head111111111111111111", PlayerConst.headImgUrl);
        //LoaderUtil.loadHeadImg(this.spr_playerHead, 100);
    }

    initNickName() {
        mk.console.log("开始界面初始化 NickName111111111111111111", PlayerConst.nickName);
        //this.label_nickName.string = PlayerConst.nickName.toString();
    }

    initCashNum() {

        //Start界面需要先隐藏
        // LogUtil.log("PlayerConst.cashNum!!!!!!!!!!!!!!!!",PlayerConst.cashNum);
        // let num = Math.round((PlayerConst.cashNum * 100)) / 100;
        let num = Util.numberFixed(PlayerConst.cashNum, 2);
        //this.label_cashNum.string = `¥ ${num}`;
    }

    /**初始化能量值 */
    initEnergyNum() {

        // //如果体力无值
        // if (!PlayerConst.energyNum) {
        //     PlayerConst.energyNum = 0;
        // }

        // if (PlayerConst.energyNum >= GameConst.maxEnergyNum) {
        //     this.label_energyNum.string = "10(满)";
        //     this.label_energyRecoverTime.node.active = false;
        // }
        // else {
        //     this.label_energyNum.string = PlayerConst.energyNum.toString();
        // }

        // if (PlayerConst.energyNum < GameConst.maxEnergyNum) {
        //     if (!PlayerConst.leftEnergyRecoverTime) {
        //         PlayerConst.leftEnergyRecoverTime = GameConst.recoverTimePerEnergy;
        //     }
        //     DataMgr.Inst.recoverEnergyNum();
        //     this.initEnergyRecoverTime();
        // }
    }

    initEnergyRecoverTime() {

        // this.label_energyRecoverTime.string = Util.ParseTime2Format(PlayerConst.leftEnergyRecoverTime, "m:s");
    }

    initShopScoreNum() {
        // this.label_shopScoreNum.string = PlayerConst.shopScoreNum.toString();
    }

    initSelectSceneItem() {

        mk.loader.load("prefab/SelectSceneItem", cc.Prefab).then((prefab) => {
            for (var i = 0; i <= GameConst.maxIconIndex; i++) {
                let node_selectSceneItem: cc.Node = cc.instantiate(prefab);
                if (i < GameConst.maxIconIndex) {
                    node_selectSceneItem.getComponent(SelectSceneItem).init(i);
                }
                else {
                    node_selectSceneItem.getComponent(SelectSceneItem).init();
                }
                this.node_scene.addChild(node_selectSceneItem);
            }
            this.node_scene.getComponent(cc.Layout).updateLayout;
            this.setNodeSelectScenePos(GameConst.iconIndex);
        }).catch(() => {

        })

    }

    /**初始化事件 */
    initEvent() {
        this.node_startGameBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_leftBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_rightBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);

        if (!GameConst.isAuth) {
            mk.event.register(EVENT_TYPE.BACK_WxAuth, this.onWxAuthBack, this);
        }
    }

    // update (dt) {}

    onClick(event: cc.Event.EventTouch) {
        //EffectMgr.Inst.addTip("点击按钮！！！！！！！！！！！！！！！")
        // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.buttonClick);
        mk.audio.playEffect("ef_button_click");

        // switch (event.currentTarget) {
        //     case this.node_playerInfo:
        //         this.onClickPlayerInfo();
        //         break;
        //     case this.node_energy:
        //         this.onClickEnergy();
        //         break;
        //     case this.node_settingBtn:
        //         this.onClickSettingBtn();
        //         break;
        //     case this.node_clockInBtn:
        //         this.onClickClockInBtn();
        //         break;
        //     case this.node_startGameBtn:
        //         this.onClickStartGameBtn();
        //         break;
        //     case this.node_leftBtn:
        //         this.onClickLeftBtn();
        //         break;
        //     case this.node_rightBtn:
        //         this.onClickRightBtn();
        //         break;
        //     case this.node_cashOutBtn:
        //         this.onClickCashOutBtn();
        //         break;
        //     case this.node_dailyCashOutBtn:
        //         this.onClickDailyCashOutBtn();
        //         break;
        //     case this.node_shopScore:
        //         this.onClickShopScore();
        //         break;
        //     case this.node_shopBtn:
        //         this.onClickShopBtn();
        //         break;
        //     case this.node_shopActivityBtn:
        //         this.onClickShopActivityBtn();
        //         break;
        //     case this.node_moreGameBtn:
        //         this.onClickMoreGameBtn();
        //         break;
        //     case this.node_turnplateBtn:
        //         this.onClickTurnplateBtn();
        //         break;
        //     case this.node_shareBtn:
        //         this.onClickShareBtn();
        //         break;
        //     case this.node_bankBtn:
        //         this.onClickBankBtn();
        //         break;
        //     case this.node_luckPacketBtn:
        //         this.onClickLuckyPacketBtn();
        //         break;
        //     case this.node_signBtn:
        //         this.onClickSignBtn();
        //         break;
        // }
    }


    onClickPlayerInfo() {
        GameMgr.Inst.sendEvent(UI_NAME.Start, "点击玩家信息按钮");
        mk.ui.openPanel("PlayerInfoUI");
        //JsbMgr.ShareToMiniGame();
    }


    onClickSettingBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.Start, "点击设置按钮");
        mk.ui.openPanel("SettingUI");
    }

    onClickStartGameBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.Start, "点击开始按钮");
        if (PlayerConst.energyNum <= 0) {
            mk.ui.openPanel("GetEnergyUI");
            return;
        }

        // if (GameConst.iconIndex >= GameConst.maxIconIndex) {
        //     EffectMgr.Inst.addTip("场景未解锁,请选择解锁场景");
        //     return;
        // }

        if (GameConst.iconIndex >= GameConst.maxIconIndex) {
            GameConst.iconIndex = 4;
            this.setNodeSelectScenePos(GameConst.iconIndex);
        }
    }

    onClickLeftBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.Start, "点击左切换按钮");
        GameConst.iconIndex--;
        this.setNodeSelectScenePos(GameConst.iconIndex);
    }

    onClickRightBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.Start, "点击右切换按钮");
        GameConst.iconIndex++;
        this.setNodeSelectScenePos(GameConst.iconIndex);
    }

    onClickCashOutBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.Start, "点击提现按钮");
        mk.ui.openPanel("CashOutUI");
    }


    setNodeSelectScenePos(iconIndex: number) {
        //let maxLeftX = GameConst.maxIconIndex * 750 * 0.5 - 750 * 0.5;

        let maxLeftX = this.node_scene.width * 0.5 - 750 * 0.5;
        let x = maxLeftX - (iconIndex) * 750;
        this.node_leftBtn.off(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_rightBtn.off(cc.Node.EventType.TOUCH_END, this.onClick, this);
        cc.tween(this.node_scene).to(0.5, { x: x }).call(() => {
            this.node_leftBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
            this.node_rightBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        }).start();
        mk.console.log("GameConst.iconIndex", GameConst.iconIndex, x);

        this.node_leftBtn.active = GameConst.iconIndex > 0;
        this.node_rightBtn.active = GameConst.iconIndex < GameConst.maxIconIndex;
    }

    /**点击分享按钮 */
    onClickShareBtn() {

    }

    /**点击存钱罐按钮 */
    onClickBankBtn() {

    }

    /**点击福袋按钮 */
    onClickLuckyPacketBtn() {

    }

    /**点击签到按钮 */
    onClickSignBtn() {

    }

    /** 设置更多游戏图片 */
    setMoreGameSp(arr) {
        let len = arr.length
        let index = Util.rnd(0, len - 1)
        let data = arr[index]
        this.showMoreGameSp(data)

        this.schedule(() => {
            index++
            index = (index % len)
            // LogUtil.logV('setMoreGameSp  ', 'index ' + index)
            data = arr[index]
            this.showMoreGameSp(data)
        }, 10)
    }

    showMoreGameSp(data) {
        // LoadResUtil.loadRemoteSprite(data.icon, this.spr_moreGame, () => {
        //     this.label_moreGameName.string = data.title
        // })
    }

    //自定义事件---------------------------------------------------------------------
    /**微信授权返回 */
    onWxAuthBack() {
        this.initHead();
        this.initNickName();
        mk.event.remove(EVENT_TYPE.BACK_WxAuth, this.onWxAuthBack, this);
    }
}
