import { StorageKey } from "./StorageData";

/**
 * @description 登录数据
 * @author 邹勇
 */
export class LoginData {

    /** 是否登录 */
    public isLogin: boolean = false;

    public sessionKey: string = '';

    /** 玩家唯一id */
    public uin: string = '';

    /** 玩家临时id */
    public tempUin: string = '';

    public loginTicket: string = '';

    public isFirstIn: boolean;

    public isNew: boolean;

    /** 是否授权 */
    public isAuth: boolean;



    /**
     * 登录流程 initApp-->initAd-->connect-->wxLogin-->checkLogin-->getAccountInfo
     */
    public async login() {
        this.isLogin = true;
        g.appData.init();
        g.adData.init();

        //有登录记录直接跳转到登录流程最后一步
        if (this.sessionKey != '' && this.uin != '') {
            this.getAccountInfo();
            return;
        }

        let ranKey = mk.encrypt.randomString();
        let data: any = {
            "mac": g.appData.mac,
            "psk": ranKey,
            "appId": g.appData.appId
        }

        //connect
        let response = await mk.http.sendRequest('connectTest', 'POST', JSON.stringify(data));
        if (response.errcode != 0) {
            return;
        }
        data = mk.encrypt.decrypt(response.encrypt, ranKey, g.appData.appId);
        this.tempUin = JSON.parse(data).tmp_uin;

        //wxLogin
        let encode;
        if (g.wechatData.code != null) {//微信登录
            encode = mk.encrypt.encrypt(JSON.stringify({ "code": g.wechatData.code }), this.sessionKey, g.appData.appId);
            data = {
                "uin": this.uin,
                "encrypt": encode
            };
        }
        else {//游客登录
            encode = mk.encrypt.encrypt(JSON.stringify({ "code": g.wechatData.code }), ranKey, g.appData.appId);
            data = {
                "uin": this.tempUin,
                "encrypt": encode
            };
        }
        response = await mk.http.sendRequest('wxlogin', 'POST', JSON.stringify(data), false, false);
        if (response.errcode != 0) {
            return;
        }
        if (g.wechatData.code != null) {
            data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, g.appData.appId);
        }
        else {
            data = mk.encrypt.decrypt(response.encrypt, ranKey, g.appData.appId);
        }
        data = JSON.parse(data);
        this.loginTicket = data.login_ticket;
        mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);

        let isFirstIn = mk.storage.getStorage('isFirstIn');
        if (isFirstIn == null) {
            this.isFirstIn = true;
            mk.storage.setStorage(StorageKey.isFirstIn, 1);
        }

        if (this.uin != data.uin) {
            this.uin = data.uin;
            this.isNew = false;
        }
        else {
            this.isNew = true;
        }
        mk.storage.setStorage('uin', this.uin);

        //checkLogin
        let tmp_key = '';
        if (g.wechatData.code != null) {
            tmp_key = this.sessionKey;
        }
        else {
            tmp_key = mk.encrypt.randomString();
        }

        data = {
            "tmp_key": tmp_key,
            "uin": this.uin,
            "login_ticket": this.loginTicket,
            "appId": g.appData.appId
        }
        response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
        if (response.errcode != 0) {
            return;
        }
        if (g.wechatData.code != null) {
            data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, g.appData.appId);
        }
        else {
            data = mk.encrypt.decrypt(response.encrypt, tmp_key, g.appData.appId);
        }

        data = JSON.parse(data);
        this.sessionKey = data.session_key;

        mk.storage.setStorage('sessionKey', this.sessionKey);

        if (g.wechatData.code != null) {
            if (this.isNew) {//玩过此游戏并且本地数据是本机的
                this.getAccountInfo();
            }
            else {//玩过此游戏 但 本地数据不是本账户的 清空数据 重新登录游戏
                this.isNew = false;
                g.wechatData.code = null;
                this.reload();
            }
        }
        else {
            this.getAccountInfo();
        }
    }

    private async getAccountInfo() {
        let nickname = mk.storage.getStorage(StorageKey.nickname);
        if (nickname) {
            g.wechatData.nickName = nickname;
        }
        else {
            let aa = {
                "uin": this.uin,
                "login_ticket": this.loginTicket
            };

            let data: any = {
                "uin": this.uin,
                "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, g.appData.appId)
            };

            let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
            if (response.errcode != 0) {
                return;
            }
            response = mk.http.checkData(response, true);
            data = response.data;
            this.isAuth = data.is_auth;
            if (this.isAuth) {
                g.wechatData.nickName = data.nickname;
                g.wechatData.avatar = data.headimgurl;
                mk.storage.setStorage(StorageKey.isAuth, 1);
                mk.storage.setStorage(StorageKey.nickname, g.wechatData.nickName, false);
                mk.storage.setStorage(StorageKey.avatar, g.wechatData.avatar, false);
            }
        }

        //获取游戏数据
        g.gameData.init();
    }



    /**
     * 本地数据不是本账户的 清空数据 重新登录游戏
     */
    private reload() {

    }
}