/**
 * @description 游戏核心玩法数据
 * @author 邹勇
 */
export class GameData {


    public dataFinish: boolean = false;

    public savePropFinish: boolean = false;

    public savePropsFinish: boolean = false;

    public getPropsFnish: boolean = false;


    public configs: any = {};

    public props: Map<number, number>;


    /**
     * 初始化游戏数据：网络配置信息，用户信息
     * @returns 
     */
    public async init() {
        let data: any = { "versionCode": gData.appData.version };
        let response = await mk.http.sendData('getAllConfigInfo', data);
        if (response.errcode != 0) {
            return;
        }
        this.configs = response.data;

        data = {};
        response = await mk.http.sendData('getInfoCrypt', data);
        if (response.errcode != 0) {
            return;
        }

        if (this.props == null) {
            this.props = new Map<number, number>();
        }

        this.initProps(response.data.gameUserData);

        this.dataFinish = true;
    }

    /**
     * 保存单个数据
     */
    public async setProp(key: GameProp, value: number) {
        let data = {
            key: key + '',
            value: value + ''
        };
        await mk.http.sendData('savePlayerProp', data);
        this.props.set(key, value);
        this.savePropFinish = true;
    }

    /**
     * 保存多个数据
     */
    public async setProps(arr: { key: GameProp, value: number }[]) {
        let needProp = {};
        for (let i = 0; i < arr.length; i++) {
            needProp[arr[i].key + ''] = arr[i].value + "";
        }
        let data = {
            needProp: needProp
        };
        await mk.http.sendData('saveAllPlayerProp', data);

        for (let i = 0; i < arr.length; i++) {
            this.props.set(arr[i].key, arr[i].value);
        }

        this.savePropsFinish = true;
    }

    /** 获取属性 */
    public getProp(key: GameProp): number {
        if (this.props == null) {
            return null;
        }
        return this.props.get(key);
    }

    /**
     * 向服务器请求所有属性后更新
     */
    public async getAllProps() {
        let response = await mk.http.sendData('getAllPlayerProp', {});
        if (response.errcode != 0) {
            return;
        }

        this.initProps(response.data);

        this.getPropsFnish = true;
    }

    private initProps(data) {
        for (let key in data) {
            this.props.set(parseInt(key), JSON.parse(data[key]));
        }
    }

    /**
     * 第二天需要重置的数据
     */
    public resetProps() {
        let arr = [];
        arr.push({ key: GameProp.sign_can, value: 1 });

        this.setProps(arr);
    }
}

/**
 * 所有模块的非校验数据
 */
export enum GameProp {
    /** ------------------ 游戏核心数据 --------------------------- */


    /** ------------------ 转盘数据 ------------------------------ */
    turnable_leftTimes = 20,


    /** ------------------ 存钱罐数据 ----------------------------- */


    /** ------------------ 签到数据 ----------------------------- */
    /** 上次签到进度 */
    sign_last_bar = 120,
    /** 当前能否签到 */
    sign_can = 121,
}
/**
 * 奖励状态
 */
export enum RewardState {
    /** 已领取 */
    none = 1,
    /** 未解锁 */
    lock = 2,
    /** 可领取 */
    unlock = 3
}