import { PlayerData } from "./PlayerData";
import { ServerManager, JtSniper } from "./ServerManager";
import { Global } from "../Global";


export  class AccountManager  {
    public static ins:AccountManager;
    constructor(_code:string,_userinfo:string,_nickname:string,_avatarurl:string,_gender:number,_province:string,_city:string,_country:string,_device:string,_shareid:string,_scene:string,_v:string) {
        this.createTime = Laya.timer.currTimer;
        AccountManager.ins = this;
        this.code = _code;
        this.userInfo = _userinfo;
        this.nickName = _nickname;
        this.avatarUrl = _avatarurl;
        this.gender = _gender;
        this.province = _province;
        this.city = _city;
        this.country = _country;
        this.device = _device;
        this.share_id = _shareid;
        this.scene = _scene;
        this.v = _v;
     }
    public static get Instance(){
        if(AccountManager.ins == null){
            new AccountManager("222222", "你找谁？","1233", "123", 1, "中国", "上海", "上海", "iphone6s", "123", "123", "123")
        }
        return AccountManager.ins;
    }
    public code:string;
    public userInfo:string;
    public nickName:string;
    public avatarUrl:string;
    public gender:number; //性别 0：未知、1：男、2：女
    public province:string;
    public city:string;
    public country:string;
    public device: string;
    public share_id: string;
    public scene: string;
    public v: string;
    public SFSFSF: number;
    private createTime;
    
    public curplayerData:PlayerData;

    public ReSetting(data, cb: Function = null)  {
        var zhengshu = data["token"];
        var netset = data["app_setting"];
        var user = data["user"];
        var jichu = data["user_base"];
        var qiandao = data["user_sign"];
        var chengjiu = data["user_achievement"];
        var invite = data["user_invite"];
        var lottery = data["user_lottery"];
        if (this.curplayerData == null)  {
            this.curplayerData = new PlayerData(zhengshu, null, netset, user, jichu, invite, null, qiandao, chengjiu,lottery);
            this.JundgCfg(data.table_version, cb);
        }
        else  {
            if (netset)  {
                this.curplayerData.netzheshe = netset;
            }
            if (user)  {
                this.curplayerData.account = user;
            }
            if (jichu)  {
                this.curplayerData.accountssss = jichu;
            }
            if (qiandao)  {
                this.curplayerData.qiandao = qiandao;
            }
            if (chengjiu)  {
                this.curplayerData.renwu = chengjiu;
            }
        }
    }

    public JundgCfg(ver, callball: Function) {

        var cfg = Laya.LocalStorage.getJSON(Global.CFG);
        if (cfg) {
            if (cfg.VER == ver) {
                // this.Cfg = cfg;
                console.log("已经拥有,不用更新");
                this.SetCfg(cfg);
                //DataController.SetCfg(cfg);
                if (callball) callball(true);
            } else {
                console.log("cfg存在但是过期了");
                Laya.LocalStorage.removeItem(Global.CFG);

                ServerManager.Instance.SendHttp(JtSniper.Config, null, Laya.Handler.create(this, (data) =>  {
                    data.configs.VER = ver;
                    Laya.LocalStorage.setJSON(Global.CFG, data.configs);

                    this.SetCfg(data.configs);
                    if (callball) callball(true);
                }), Laya.Handler.create(this, (data) => {
                    console.log("配置获取失败" + data);
                    if (callball) callball(false);
                    return;
                }));

            }
        } else {
            console.log("本地没有cfg");
            //this.RessetConfig();
            ServerManager.Instance.SendHttp(JtSniper.Config, null, Laya.Handler.create(this, (data) =>  {
                data.configs.VER = ver;
                Laya.LocalStorage.setJSON(Global.CFG, data.configs);
                this.SetCfg(data.configs);
                if (callball) callball(true);
            }), Laya.Handler.create(this, (data) => {
                console.log("配置获取失败" + data);
            }));
        }
    }

    SetCfg(data)  {
        this.curplayerData.peizhis = data;
        // this.curplayerData.GetJiangLiLieBiao();
        // this.curplayerData.GetShiTiLieBiao();
        // this.curplayerData.GetChengJiuLieBiao();
    }

    public RessetConfig()  {
        ServerManager.Instance.SendHttp(JtSniper.Config, null, Laya.Handler.create(this, (data) =>  {
            this.curplayerData.peizhis = data["configs"];
            // this.curplayerData.GetJiangLiLieBiao();
            // this.curplayerData.GetShiTiLieBiao();
            // this.curplayerData.GetChengJiuLieBiao();
        }), null);
    }

    public get ServerTime(): number {
        let time = Laya.timer.currTimer;
        let stime = this.SFSFSF + Math.ceil((time - this.createTime) / 1000);
        return stime;
    }
    
}