import { Main } from "../Main";
import NetManager from "./NetManager";
import { DateController } from "../game_logic/DateController";


export enum UserConfigName {
    train, sign, rewards
}
//用户信息属性
export enum UserAttr {
    equip_train, user_id, stage, get_trains, money, progress_trains
}
export class NetUserModel {
    public m_user_base: Object;
    public m_user_invite: Object;
    public m_user_lottery: object
    private loginSeverTime: number = 0;
    public m_user_treasure: object;
    public m_user_achievement: object;
    public m_user_sign: Object;
    private m_user_arena: Object;
    private localTime: number;
    public nickname: string;
    public openid: string;
    public userid: string;
    public m_token: string;
    public m_configs: Object;
    public m_app_setting: Object;
    public m_user: Object;
    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) {
        this.localTime = Laya.timer.currTimer;
        this.loginSeverTime = t;
        this.m_token = _token;
        this.m_configs = _config;
        this.m_app_setting = _appSetting;
        this.m_user = _user;
        this.m_user_base = _userbase;
        this.m_user_invite = _useInvite;
        this.m_user_lottery = _user_lottery;
        this.m_user_treasure = _user_treasure;
        this.m_user_achievement = _user_achievement;
        this.m_user_sign = _user_sign;
        this.m_user_arena = _user_arena;
        this.userid = this.m_user["user_id"];
        this.openid = this.m_user_base["openid"];
        this.nickname = this.m_user_base["nickname"];
    }
    public set Cfg(cfg) {
        this.m_configs = cfg;
    }
    public get sharetitle(): string {
        if (this.m_app_setting["sharetitle"]) {
            return this.m_app_setting["sharetitle"];
        }
        return "";
    }
    public get shareimage(): string {
        if (this.m_app_setting["shareimage"]) {
            return this.m_app_setting["shareimage"];
        }
        return "";
    }
    public GetCfg(type: UserConfigName): any {
        var _type = UserConfigName[type];
        return this.m_configs[_type];
    }
    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 get GuideLevel(): number {
        if (this.m_app_setting["GuideLevel"]) {
            return Number(this.m_app_setting["GuideLevel"]);
        }
        return 1;
    }

    public get BannerMoveTime(): number {
        if (this.m_app_setting["BannerMoveTime"]) {
            return Number(this.m_app_setting["BannerMoveTime"]);
        }
        return 0;
    }
    public get HaveBanner(): number {
        if (this.m_app_setting["HaveBanner"]) {
            return Number(this.m_app_setting["HaveBanner"]);
        }
        return 0;
    }
    public get IsShare(): number {
        if (this.m_app_setting["IsShare"]) {
            return Number(this.m_app_setting["IsShare"]);
        }
        return 0;
    }
    public get BtnCanMove(): number {
        if (this.m_app_setting["BtnCanMove"]) {
            return Number(this.m_app_setting["BtnCanMove"]);
        }
        return 0;
    }
    public get ISShareThenVideo(): number {
        if (this.m_app_setting["ISShareThenVideo"]) {
            return Number(this.m_app_setting["ISShareThenVideo"]);
        }
        return 0;
    }
    public get OpenSceneValue(): number {
        if (this.m_app_setting["OpenSceneValue"]) {
            return Number(this.m_app_setting["OpenSceneValue"]);
        }
        return 0;
    }

    public get IsOpenTryOut(): number {
        if (this.m_app_setting["IsOpenTryOut"]) {
            var num = Number(this.m_app_setting["IsOpenTryOut"]);
            if (num == 0) return 0;
            if (Math.random() < num) {
                return 1;
            } else {
                return 0;
            }
        }
        return 0;
    }

    // public JundgCfgForCdn(callball: Function) {
    //     this.m_configs = null;
    //     if (this.m_configs) {
    //         callball();
    //     } else {
    //         var cfg = Laya.LocalStorage.getJSON(Main.cfg);
    //         if (cfg) {
    //             if (cfg.VER == this.GetCfgVer) {
    //                 this.m_configs = cfg;
    //                 console.log("cfg存在");
    //                 callball();
    //             } else {
    //                 console.log("cfg存在但是过期了");
    //                 Laya.LocalStorage.removeItem(Main.cfg);
    //                 Laya.loader.load("cfg.json", new Laya.Handler(this, (data) => {
    //                     console.log("cfg下载完成");
    //                     if (data) {
    //                         Laya.LocalStorage.setJSON(Main.cfg, data);
    //                     }
    //                     this.m_configs = data;
    //                     callball();
    //                 }));
    //             }
    //         } else {
    //             Laya.loader.load("cfg.json", new Laya.Handler(this, (data) => {
    //                 console.log("cfg下载完成");
    //                 if (data) {
    //                     Laya.LocalStorage.setJSON(Main.cfg, data);
    //                 }
    //                 this.m_configs = data;
    //                 callball();
    //             }));
    //         }
    //     }
    // }

    // public JundgCfg(ver, callball: Function) {
    //     this.m_configs = null;
    //     if (this.m_configs) {
    //         callball(true);
    //     } else {
    //         var cfg = Laya.LocalStorage.getJSON(Main.CFG);
    //         if (cfg) {
    //             if (cfg.VER == ver) {
    //                 this.Cfg = cfg;

    //                 console.log("已经拥有,不用更新");
    //                 DateController.SetCfg();
    //                 callball(true);
    //             } else {
    //                 console.log("cfg存在但是过期了");
    //                 Laya.LocalStorage.removeItem(Main.CFG);
    //                 NetManager.Instance.PostCfgRequest((data) => {
    //                     data.VER = ver;
    //                     Laya.LocalStorage.setJSON(Main.CFG, data);
    //                     this.Cfg = data;
    //                     DateController.SetCfg();
    //                     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(Main.CFG, data);
    //                 this.Cfg = data;
    //                 DateController.SetCfg();
    //                 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, scene: 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.m_scene = scene ? scene : "";
        this.m_country = country ? country : "";
        this.m_province = province ? province : "";
    }
    public m_city: string;
    public m_device: string;
    public m_share_id: string;
    public m_scene: string;
    public static get Instance(): UserModel {
        if (UserModel.instance) {
            return UserModel.instance;
        }
        else {
            UserModel.instance = new UserModel("11", "游客", "", null, "", "", "", "iphone6s", "", "");
            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;
    }


}
