// 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 StorageMgr from "./StorageMgr";
import { PROPTYPE } from "../data/Enum";
import GamePlay from "../GamePlay";
import { EVENT_TYPE, GameProp } 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() {

        //是否巨量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;
        }

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

    /**
     * 更新道具数据
     * @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:
                let hammerPropNum = gData.gameData.getProp(GameProp.hammerPropNum);
                if (updateType == 0) {

                }
                else {
                    hammerPropNum += propNum;
                }
                gData.gameData.setProp(GameProp.hammerPropNum, hammerPropNum);
                if (GamePlay.Inst) {
                    GamePlay.Inst.initHammerPropNum();
                }
                break;
            case PROPTYPE.Change:
                let changePropNum = gData.gameData.getProp(GameProp.changePropNum);
                if (updateType == 0) {

                }
                else {
                    changePropNum += propNum;
                }
                gData.gameData.setProp(GameProp.changePropNum, changePropNum);
                if (GamePlay.Inst) {
                    GamePlay.Inst.initChanegPropNum();
                }
                break;
            case PROPTYPE.Reset:
                let resetPropNum = gData.gameData.getProp(GameProp.resetPropNum);
                if (updateType == 0) {

                }
                else {
                    resetPropNum += propNum;
                }
                gData.gameData.setProp(GameProp.resetPropNum, resetPropNum);
                if (GamePlay.Inst) {
                    GamePlay.Inst.initResetPropNum();
                }
                break;
        }

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

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

    /**更新巨量回传 */
    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);
    }
}
