import Utils from "../../core/utils/Utils";
import { ITEM_TYPE } from '../../core/common/GameConst';

let savepars = [];
function save(target: any, propertyKey: string) {
    savepars.push(propertyKey);
}

export type SignInfo = {
    sign_index: number;
    sign_time: number;
    sign_beisu: number;
}

//用户数据模型
export default class UserModel {
    public js_code: string = ""
    @save public nickName: string = "";
    @save public avatarUrl: string = "";
    @save public openid: string = "";

    @save public magnet = 0;//道具 磁铁
    @save public frozen = 0;//道具 冰冻时间
    @save public hint = 0;
    @save public time = 0;

    @save public soundVolume: number = 1;
    @save public vibration: number = 1;

    @save public lvboxDrawInfo = {};        //等级宝箱领取状态
    @save public starboxDrawInfo = {};      //等级宝箱领取状态

    //每10关
    public hasLvBox() {
        let key = Math.floor(this.lv / 10)
        return (key != 0) && !this.lvboxDrawInfo[key];
    }

    public hasStarBox() {
        let key = Math.floor(this.star / 1000)
        return (key != 0) && !this.starboxDrawInfo[key];
    }

    public useMagnet = false;
    public useTime = false;

    public addItem(type: ITEM_TYPE, num: number = 1) {
        switch (type) {
            case ITEM_TYPE.Coin:
                this.coin += num;
                break;
            case ITEM_TYPE.Magnet:
                this.magnet += num;
                break;
            case ITEM_TYPE.Hint:
                this.hint += num;
                break;
            case ITEM_TYPE.Frozen:
                this.frozen += num;
                break;
            case ITEM_TYPE.Time:
                this.time += num;
                break;
            default:
                debugger;
                break;
        }
    }

    @save life = 5;
    @save coin = 500;
    @save lv = 1;
    @save star = 0;

    @save openIndex = 0;

    @save life_cost_time = 0;

    self_rank: number = -1;//自己排行榜位置

    @save public invite_draw_info = {};

    //检查每日邀请是否领取奖励
    checkDayInviteIsDraw(cn: number) {
        if (!this.invite_draw_info["day"]) this.invite_draw_info["day"] = {};
        return this.invite_draw_info["day"][cn] == 1;
    }

    inviteDayDraw(cn: number) {
        if (!this.invite_draw_info["day"]) this.invite_draw_info["day"] = {};
        return this.invite_draw_info["day"][cn] = 1;
    }

    //检查新邀请是否领取奖励
    checkNewInviteIsDraw(cn: number) {
        if (!this.invite_draw_info["new"]) this.invite_draw_info["new"] = {};
        return this.invite_draw_info["new"][cn] == 1;
    }

    inviteNewDraw(cn: number) {
        if (!this.invite_draw_info["new"]) this.invite_draw_info["new"] = {};
        return this.invite_draw_info["new"][cn] = 1;
    }


    @save public storeup = false;//是否搜藏游戏
    public launch_from_my_minigame = false;//本次启动是否从我的小程序启动
    public query = null;//启动查询参数

    @save public signinfo: SignInfo = { sign_index: 0, sign_time: 0, sign_beisu: 1 }


    //在线奖励
    @save public lastlogintime: number = 0;//最近一次登陆时间
    @save public onlineTime: number = 0;//  秒 在线时间  update++
    @save public drawed_online_reward_ids = [];//已经领取了在线奖励ID


    public serverTime = 0;
    public getUploadData(): object {
        var data = {}
        this.serverTime = Date.now();
        for (var i = 0; i < savepars.length; ++i) {
            data[savepars[i]] = this[savepars[i]];
        }

        return data;
    }
    public setData(data: any) {
        //初始化下数据       
        if (!data) {
            return;
        }
        for (var i = 0; i < savepars.length; ++i) {
            const element = data[savepars[i]];
            if (element != null || element != undefined) {
                if (Object.prototype.toString.call(element) == "[object Object]") {
                    for (const key in element) {
                        this[savepars[i]][key] = element[key];
                    }
                }
                else
                    this[savepars[i]] = element;
            }
        }
    }
}