// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import PlayerConst, { DATA_STORAGE_KEY } from "../data/PlayerConst";
import GameConst from "../data/GameConst";
import Start from "../Start";
import StorageMgr from "./StorageMgr";
import { PROPTYPE } from "../data/Enum";
import GamePlay from "../GamePlay";
import DailyCashOutUI from "../view/DailyCashOutUI";
import HttpMgr from "./HttpMgr";
import CashOutUI from "../view/CashOutUI";
import { EVENT_TYPE } from "../../game/data/GameData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DataMgr extends cc.Component {

    //单例模式---------------------------
    private static instance: DataMgr = null;

    public static get Inst(): DataMgr {
        if (!DataMgr.instance) {
            DataMgr.instance = new DataMgr();
        }
        return DataMgr.instance;
    }

    /**初始化缓存数据 */
    initStorageData() {
        //uin
        let uin = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.uin);
        mk.console.log("uin", uin);
        if (!!uin) {
            GameConst.uin = mk.encrypt.decrypt(uin, GameConst.localEncryptKey, GameConst.appid);
            GameConst.tmp_uin = GameConst.uin;
            mk.console.log("GameConst.uin", GameConst.uin);
        }

        //session_key 
        let session_key = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.session_key);
        if (!!session_key) {
            GameConst.session_key = mk.encrypt.decrypt(session_key, GameConst.localEncryptKey, GameConst.appid);
        }

        //login_ticket
        let login_ticket = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.login_ticket);
        if (!!login_ticket) {
            GameConst.login_ticket = mk.encrypt.decrypt(login_ticket, GameConst.localEncryptKey, GameConst.appid);
        }

        //是否巨量api 回传激活
        let ifJuliangApiActivie = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.ifJuliangApiApiActivie);
        if (!!ifJuliangApiActivie) {
            PlayerConst.ifJuliangApiApiActivie = ifJuliangApiActivie;
        }

        //是否巨量api 二次激活
        let ifJuliangApiSencondActivie = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.ifJuliangApiSencondActivie);
        if (!!ifJuliangApiSencondActivie) {
            PlayerConst.ifJuliangApiSencondActivie = ifJuliangApiSencondActivie;
        }

        //今天通关的红包数目
        let todayPassRedPacketNum = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.todayPassRedPacketNum);
        if (!!todayPassRedPacketNum) {
            PlayerConst.todayPassRedPacketNum = todayPassRedPacketNum;
        }

        //地址
        let address = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.adress);
        if (!!address) {
            PlayerConst.adress = address;
        }

        //平均ecpm
        let average_ecpm = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.average_ecpm);
        if (!!average_ecpm) {
            PlayerConst.average_ecpm = average_ecpm;
        }

        //体力回复
        let energyNum = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.energyNum);
        if (!!energyNum) {
            PlayerConst.energyNum = energyNum;
        }

        //剩余体力恢复时间
        let leftEnergyRecoverTime = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.leftEnergyRecoverTime);
        if (!!leftEnergyRecoverTime) {
            PlayerConst.leftEnergyRecoverTime = leftEnergyRecoverTime;
        }

        let date = new Date();

        //体力值恢复计算--------------------------------------------------------------------
        let energyChangeTime = StorageMgr.Inst.getStorage(DATA_STORAGE_KEY.energyChangeTime);
        mk.console.log("energyChangeTime", energyChangeTime);
        if (!!energyChangeTime) {

            let curTime = date.getTime();

            //计算总的间隔时间
            let timeInterval = Math.floor((curTime - energyChangeTime) / 1000);
            //计算整段恢复的数量
            let recoverNum = Math.floor(timeInterval / GameConst.recoverTimePerEnergy);

            //去除整段恢复剩余的时间
            let leftTime = timeInterval - recoverNum * GameConst.recoverTimePerEnergy;

            //如果存在剩余恢复时间
            if (leftEnergyRecoverTime) {
                if (leftTime >= leftEnergyRecoverTime) {
                    recoverNum += 1;
                    PlayerConst.leftEnergyRecoverTime = leftTime - leftEnergyRecoverTime;
                }
                else {
                    PlayerConst.leftEnergyRecoverTime = leftEnergyRecoverTime - leftTime;
                }
            }

            if (recoverNum > 0) {
                PlayerConst.energyNum += recoverNum;
                if (PlayerConst.energyNum >= GameConst.maxEnergyNum) {
                    PlayerConst.energyNum = GameConst.maxEnergyNum;
                    StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.energyNum, PlayerConst.energyNum);
                }
            }
            mk.console.log("recoverNum", recoverNum);
            mk.console.log("timeInterval", timeInterval);
        }
    }

    /**更新金钱数量
     * @param cashNum 加减的cashNum值
     */
    public updateMoneyNum(cashNum: number) {
        PlayerConst.cashNum += cashNum;
        if (cashNum < 0) {
            PlayerConst.cashNum = PlayerConst.cashNum <= 0 ? 0 : PlayerConst.cashNum;
        }
        StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.cashNum, PlayerConst.cashNum);

        if (Start.Inst) {
            Start.Inst.initCashNum();
        }

        let node_cashOutUI = mk.ui.getCurOnPanel("CashOutUI");
        if (node_cashOutUI) {
            node_cashOutUI.getComponent(CashOutUI).initCashNum();
        }
        mk.event.emit(EVENT_TYPE.UPDATE_CashNum);
    }

    /**
     * 更新道具数据
     * @param updateType 更新类型 0 刷新 1 增 2减
     * @param propType 道具类型
     * @param propNum  道具数目
     */
    updatePropNum(updateType: number, propType: PROPTYPE, propNum: number = 1) {
        mk.console.log("更新道具数据", propType, propNum);
        switch (propType) {
            case PROPTYPE.Hammer:
                if (updateType == 0) {
                    PlayerConst.hanmmerPropNum = propNum;
                }
                else {
                    PlayerConst.hanmmerPropNum += propNum;
                }

                if (GamePlay.Inst) {
                    GamePlay.Inst.initHammerPropNum();
                }
                break;
            case PROPTYPE.Change:
                if (updateType == 0) {
                    PlayerConst.changePropNum = propNum;
                }
                else {
                    PlayerConst.changePropNum += propNum;
                }

                if (GamePlay.Inst) {
                    GamePlay.Inst.initChanegPropNum();
                }
                break;
            case PROPTYPE.Reset:
                if (updateType == 0) {
                    PlayerConst.resetPropNum = propNum;
                }
                else {
                    PlayerConst.resetPropNum += propNum;
                }
                if (GamePlay.Inst) {
                    GamePlay.Inst.initResetPropNum();
                }
                break;
        }

        if (updateType === 0) {
            return;
        }

        HttpMgr.Inst.prop(updateType, GamePlay.Inst.curPropType, 1).then((data) => {

        }).catch((err) => {

        });
    }

    /**更新当前过关红包 */
    updateCurPassRedPacket() {
        PlayerConst.todayPassRedPacketNum++;
    }

    /** */
    updateTodayPassRedPacketNum(num: number) {
        PlayerConst.todayPassRedPacketNum = num;
        StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.todayPassRedPacketNum, PlayerConst.todayPassRedPacketNum);
    }

    //更新地址
    updateAdress(adress: string) {
        PlayerConst.adress = adress;
        StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.adress, PlayerConst.adress);
    }

    //巨量回传数据--------------------------------------------------------------------------------------------

    /**更新巨量回传 */
    updateJuliangApi() {
        if (!PlayerConst.ifJuliangApiApiActivie) {
            this.updateJuliangApiActive(true);
            HttpMgr.Inst.juliangAPIActive(0);
        }
        else {
            if (!PlayerConst.ifJuliangApiSencondActivie && PlayerConst.loginDay > 1) {
                this.updateJuliangApiSecondActive(true);
                HttpMgr.Inst.juliangAPIActive(6);
            }
        }
    }

    /**更新巨量激活回传 */
    updateJuliangApiActive(ifActive: boolean) {
        PlayerConst.ifJuliangApiApiActivie = true;
        StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.ifJuliangApiApiActivie, PlayerConst.ifJuliangApiApiActivie);
    }

    /**更新巨量次留激活回传 */
    updateJuliangApiSecondActive(ifActive: boolean) {
        PlayerConst.ifJuliangApiSencondActivie = true;
        StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.ifJuliangApiSencondActivie, PlayerConst.ifJuliangApiSencondActivie);
    }

    /**重设数据 */
    resteData() {
        mk.console.log("重设数据!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        this.updateTodayPassRedPacketNum(0);
        this.updateWatchVideoNum(0);
        this.updateOnlineTime(0);
        this.updatePassNum(0);

    }
}
