import { info } from "console";
import { Framework } from "../../framework/Framework";
import { ObjectUtil } from "../../framework/util/ObjectUtil";
import { StringUtil } from "../../framework/util/StringUtil";
import { GameEvent } from "./GameEvent";

export const DataKey = {
    Role: "Role",                           //角色
    GameInstall: "GameInstall",             //设置
    UserID: 'UserID',                       //用户ID
};

// 用户状态
export interface UserStatus{
    'xp': number,                                   // 基因实验室经验
    'vip': number,                                  // VIP等级
    'vip_xp':number,                                // vip经验点(可由充值或其他方式增加)
    'level': number,                                // 等级
    'gold': number,             // 金币
    'cash': number,             // 蓝钻
    'diamond':number,        // 红钻
    'food': number,             // 能量点
}


class Data {
    private _class_id = StringUtil.getUUID(32);
    //需要保存的数据
    save_data = new Map<string, any>();

    UserID: any = null;

    info: any = null;
    mark: any = null;
    //货币
    status: UserStatus = null;
    food_timer:number = 0;

    init(data: any) {
        if(!data) return;
        Framework.storage.setUser(data._id);
        if(data.info){
            this.info = data.info;
        }
        if(data.mark){
            this.mark = data.mark;
        }
        if(data.status){
            this.status = data.status;
        }

        if(data.fool_timer){
            this.food_timer = data.food_timer
        }
        //将服务器的数据替换到本地
        // let save = new Map<string, string>();
        // for (let key in data) {
        //     this[key] = data[key]
        //     Framework.storage.set(key, this[key]);
        // }
        //读取本地数据
        //this.era = Framework.storage.get(DataKey.Era, GameConf.Macro.default_era);


    }

    // 存储本地
    save_locally(key: string, value?, defaultValue?: any) {
        if (key != 'UserID') {
            key += this.UserID
        }
        if (value) {
            this[key] = value
            Framework.storage.set(key, value);
            this._saveData(key, value)
        } else {
            this[key] = Framework.storage.get(key, defaultValue);
            return this[key];
        }
    }

    _saveData(key: string, value: any) {
        this.save_data.delete(key);
        if (typeof value === 'object') {
            try {
                if (value instanceof Map) {
                    let temp = ObjectUtil.mapToObject(value)
                    value = JSON.stringify(temp);
                } else {
                    value = JSON.stringify(value);
                }
            } catch (e) {
                console.error(`解析失败,str = ${value}`);
                return;
            }
        } else {
            value = value + "";
        }
        this.save_data.set(key, value);
        Framework.storage.set('save_data', this.save_data);
    }

    private _mapToObject(map: any) {
        let obj = {};
        for (let [k, v] of map) {
            obj[k] = v;
        }
        return obj;
    }

}

export let UserData = new Data;