
import NetManager, { PROTOCOLS } from "./NetManager";
import { UserInfoManager } from "../GameLogic/UserInfoManager";
import { DispatcherManager, EventType } from "../GameLogic/DispatcherManager";
import GameSetting from "./GameSetting";
import { PlayerManager } from "../GameLogic/PlayerManager";



export enum UserConfigName {
    train, sign, rewards
}
//用户信息属性
export enum UserAttr {
    //land解锁土块块数   vmoney_total虚拟金币总量
    user_id, diamond, energy, list_kun, stage, vmoney, equip_kun, unlock_kun, buyed_kun,money,progress_themes,equip_themes,get_themes,attrs
}
export class NetUserModel {
    public m_base: Object;
    public m_invite: Object; //邀请
    public m_lottery: any //抽奖
    private loginSeverTime: number = 0; //登录时间
    public m_treasure: object;
    public m_achievement: object;// 
    public m_sign: Object;
    private m_arena: Object;
    private localTime: number;
    public m_nickname: string;
    public m_avatar: string = "";
    public m_openid: string;
    public m_userid: string;
    public m_token: string;
    public m_shop: object;
    public m_user: Object;
    public is_new: number;
    public user_daily_task: any;
    public user_video: any;
    constructor(_token: string, _config: Object, _appSetting: Object, _user: Object, _userbase: Object, _useInvite: Object,
        _user_lottery: Object, _user_treasure: Object, t, _user_achievement, _user_sign, _user_arena, is_new, user_shop, user_daily_task, user_video) {
        this.localTime = Laya.timer.currTimer;
        this.loginSeverTime = t;
        this.m_token = _token;
        this.m_user = _user;
        this.m_base = _userbase;
        this.m_invite = _useInvite;
        this.m_lottery = _user_lottery;
        this.m_treasure = _user_treasure;
        this.m_achievement = _user_achievement;
        this.m_sign = _user_sign;
        this.m_arena = _user_arena;
        this.m_shop = user_shop;
        this.user_video = user_video;
        this.user_daily_task = user_daily_task;
        this.m_userid = this.m_user["user_id"];
        this.m_openid = this.m_base["openid"];
        this.m_nickname = this.m_base["nickname"];
        this.is_new = Number(is_new);
    }

    public GetUserAttr(_attr: UserAttr) {
        var attr = UserAttr[_attr];
        return this.m_user[attr];
    }
    public get ServerTime(): number {
        let time = Laya.timer.currTimer;
        let stime = this.loginSeverTime + Math.ceil((time - this.localTime) / 1000);
        return stime;
    }
    
    public HasTheme(_id: number): boolean {
        var data = this.GetUserAttr(UserAttr.get_themes);
        for (var i = 0; i < data.length; i++) {
            var hd = data[i];
            if (_id == hd) {
                return true;
            }
        }
        return false;
    }
    //获取皮肤的进度
    public GetThemeProgress(_id: number): number {
        var config = this.GetUserAttr(UserAttr.progress_themes);
        var progress = config[_id] ? config[_id] : 0;
        return progress;
    }
    public JundgCfg(ver, callball: Function) {

        var cfg = Laya.LocalStorage.getJSON(GameSetting.CFG);
        if (cfg) {
            if (cfg.VER == ver) {
                // this.Cfg = cfg;
                console.log("已经拥有,不用更新");
                UserInfoManager.SetCfg(cfg);
                callball(true);
            } else {
                console.log("cfg存在但是过期了");
                Laya.LocalStorage.removeItem(GameSetting.CFG);
                NetManager.Instance.PostCfgRequest((data) => {
                    data.VER = ver;
                    Laya.LocalStorage.setJSON(GameSetting.CFG, data);
                    //this.Cfg = data;
                    UserInfoManager.SetCfg(data);
                    callball(true);
                }, (data) => {
                    console.log("配置获取失败" + data);
                    callball(false);
                    return;
                });
            }
        } else {
            console.log("本地没有cfg");
            NetManager.Instance.PostCfgRequest((data) => {
                console.log("cfg" + data);
                data.VER = ver;
                Laya.LocalStorage.setJSON(GameSetting.CFG, data);
                //this.Cfg = data;
                UserInfoManager.SetCfg(data);
                callball(true);
            }, (data) => {
                console.log("配置获取失败" + data);
            });
        }
    }


}

export default class UserModel {
    public get CurModel(): NetUserModel {
        return this.curModel;
    }
    private static instance;
    public m_code: string;
    public m_gender: number;
    public m_country: string;
    constructor(code: string, nickname: string, avatar: string, gender: number, country: string, province: string, city: string, device: string, share_id: string, channel: string) {
        UserModel.instance = this;
        this.m_code = code;
        this.m_nickname = nickname ? nickname : "";
        this.m_avatar = avatar ? avatar : "";
        this.m_gender = gender ? gender : 0;
        this.m_city = city ? city : "";
        this.m_device = device ? device : "";
        this.m_share_id = share_id ? share_id : "";
        this.channel = channel ? channel : "";
        this.m_country = country ? country : "";
        this.m_province = province ? province : "";
    }
    public m_city: string;
    public m_device: string;
    public m_share_id: string;
    public channel: string;
    public static get Instance(): UserModel {
        if (UserModel.instance) {
            return UserModel.instance;
        }
        else {
            UserModel.instance = new UserModel("11", "", "", 0, "", "", "", "", "", "");
            return UserModel.instance;
        }
    }
    public m_province: string;
    public m_nickname: string;
    public m_avatar: string;
    private curModel: NetUserModel;
    public SetUserModel(_model: NetUserModel) {
        this.curModel = _model;
    }

    public RefreshModel(data) {
        if (data["user"])
            this.curModel.m_user = data["user"];
        if (data["user_invite"])
            this.curModel.m_invite = data["user_invite"];
        if (data["user_lottery"])
            this.curModel.m_lottery = data["user_lottery"];
        if (data["user_achievement"]) {
            this.curModel.m_achievement = data["user_achievement"];
            // DispatcherManager.event(EventType.Event.UpdateAchievementEvent);
        }

        if (data["user_sign"])
            this.curModel.m_sign = data["user_sign"];
        if (data["user_shop"])
            this.curModel.m_shop = data["user_shop"];
        if (data["user_daily_task"])
            this.curModel.user_daily_task = data["user_daily_task"];
        if (data["user_video"])
            this.curModel.user_video = data["user_video"]
        //  PlayerManager.Instance.Unlock_kun = (Number(UserModel.Instance.CurModel.GetUserAttr(UserAttr.unlock_kun)));
    }


}
