import DataMgr from "../mgr/DataMgr";
import StorageMgr from "../mgr/StorageMgr";
import Util from "../util/Util";
import { PROPTYPE } from "./Enum";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayerConst extends cc.Component {

    //玩家状态信息---------------------------------------------------------------
    /**今日是否首次登陆 */
    public static ifFirstLoginToday: boolean = false;
    /**是否显示了惊喜任务 */
    public static ifShowSurpriseTask: boolean = false;

    //玩家微信信息---------------------------------------------------------------
    /**玩家昵称 */
    public static headImgUrl: string = "";
    public static nickName: string = "微信未授权";
    public static inviteCode: string = "";

    //玩家数据信息---------------------------------------------------------------
    /**玩家现金数目 */
    public static cashNum: number = 0;

    /**关卡数 */
    public static levelNum: number = 0;

    /**体力数量 */
    public static energyNum: number = 9;

    /**体力更改的时间 */
    public static energyChangeTime: number = 0;

    /**剩余体力恢复时间 */
    public static leftEnergyRecoverTime: number = null;

    /**锤子道具数量 */
    public static hanmmerPropNum: number = 0;
    /**变色道具数量 */
    public static changePropNum: number = 0;
    /**重设道具数量 */
    public static resetPropNum: number = 0;

    public passRedPacketCashNum: Number = 0;

    /**登陆天数 */
    public static loginDay: number = 0;

    //Api回传数据-------------------------------------------------------------------
    public static videoNum: number = 0;
    /**是否巨量激活 */
    public static ifJuliangApiApiActivie: boolean = false;
    /**是否巨量次留激活 */
    public static ifJuliangApiSencondActivie: boolean = false;


    //打卡红包相关数据
    public static clockInCashInfo: any = null;

    //打卡相关数据------------------------------------------------------------------
    
    /**通关红包数 */
    public static todayPassRedPacketNum: number = 0;
    /**今日目标红包数 */
    public static todayTargetRedPacketNum: number = 10;

    //平均ecpm----------------------------------------------------------------------
    public static average_ecpm: number = 0;

    //商城信息-----------------------------------------------------------------------
    public static adress: string = "";

    /**获取用户detail信息 */
    public static getDetailInfo(data: any) {
        PlayerConst.cashNum = !!data.amount ? Number(data.amount) : 0;
        PlayerConst.levelNum = !!data.cur_pass_no ? Number(data.cur_pass_no) : 0;
        PlayerConst.energyNum = !!data.strength ? data.strength : 0;
        PlayerConst.shopScoreNum = !!data.score_total ? data.score_total : 0;
     
        //登陆天数
        PlayerConst.loginDay = !!data.loginDays ? data.loginDays : 1;
      
        //给Ecpm赋值
        if (!!data.adShowConfig) {
            if (PlayerConst.ifFirstLoginToday || !PlayerConst.average_ecpm) {
                PlayerConst.average_ecpm = data.adShowConfig.average_ecpm;
                StorageMgr.Inst.setStorage(DATA_STORAGE_KEY.average_ecpm, PlayerConst.average_ecpm);
                mk.console.log("[HttpMgr] detail PlayerConst.average_ecpm 首日赋值", PlayerConst.average_ecpm);
            }
            mk.console.log("[HttpMgr] detail PlayerConst.average_ecpm", PlayerConst.average_ecpm);
        }

        //刷新红包数目
        DataMgr.Inst.updateMoneyNum(0);
        //刷新道具数量
        this.getPropNum(data.props_data);
    }

    /**获取道具数量 */
    public static getPropNum(data: any) {
        if (data) {
            mk.console.log("data", data);
            Object.keys(data).forEach((key) => {
                let num = Number(data[key]);
                let propType = PROPTYPE.Null;
                switch (key) {
                    case PROPTYPE.Hammer:
                        propType = PROPTYPE.Hammer;
                        break;
                    case PROPTYPE.Reset:
                        propType = PROPTYPE.Reset;
                        break;
                    case PROPTYPE.Change:
                        propType = PROPTYPE.Change;
                        break;
                }
                DataMgr.Inst.updatePropNum(0, propType, num);
            })
        }
    }

    /** 获取特定类型的道具数量 */
    public static getPropNumByType(propType: PROPTYPE): number {
        let num: number = 0;
        switch (propType) {
            case PROPTYPE.Hammer:
                num = PlayerConst.hanmmerPropNum;
                break;
            case PROPTYPE.Reset:
                num = PlayerConst.resetPropNum;
                break;
            case PROPTYPE.Change:
                num = PlayerConst.changePropNum;
                break;
            default:
                num = 0;
                break;
        }
        return num;
    }

    /**检测是否是新授权用户 */
    public static cheeckIfNewAuthUser(registerTime: number): boolean {
        if (!registerTime) {
            return true;
        }
        let curDate = new Date();
        let date_registerTime = new Date(registerTime * 1000);
        let day_curDate = Util.parseTimestamp(curDate);
        let day_registerTime = Util.parseTimestamp(date_registerTime);
        mk.console.log("Util.parseTimestamp(day_curDate)", Util.parseTimestamp(curDate));
        mk.console.log("Util.parseTimestamp(day_registerTime)", Util.parseTimestamp(date_registerTime));
        if (day_curDate == day_registerTime) {
            return true;
        }
        else {
            return false;
        }
    }
}

export enum DATA_STORAGE_KEY {
    /**现金数目 */
    cashNum = "cashNum",
    /**商城积分数量 */
    shopScoreNum = "shopScoreNum",
    /**关卡等级 */
    levelNum = "levelNum",
    /**能量数目 */
    energyNum = "energyNum",
    /**能量变更时间 */
    energyChangeTime = "energyChangeTime",
    /**剩余的能量回复时间 */
    leftEnergyRecoverTime = "leftEnergyRecoverTime",
    /**登陆ticket */
    login_ticket = "login_ticket",
    /**uin */
    uin = "uin",
    /**session_key */
    session_key = "session_key",
    /**是否授权 */
    isAuth = "isAuth",
    /**昵称 */
    nickname = "nickname",
    /**今天通关红包数目 */
    todayPassRedPacketNum = "todayPassRedPacketNum",
    /**今日观看视频数目 */
    watchVideoNum = "watchVideoNum",
    /**今日在线时长 */
    onlineTime = "onlineTime",
    /**今日通关数目 */
    passNum = "passTime",
    /**是否巨量激活 */
    ifJuliangApiApiActivie = "ifJuliangApiApiActivie",
    /**是否巨量次留激活 */
    ifJuliangApiSencondActivie = "ifJuliangApiSencondActivie",
    /**地址 */
    adress = "adress",
    /**平均Ecpm */
    average_ecpm = "average_ecpm"
}

