import { _decorator, Component, Node } from 'cc';
import I18n from '../i18n/I18n';
import GlobalData from './GlobalData';
import { Loading } from './Loading';
import { LocalStorageMgr } from './LocalStorageMgr';
const { ccclass, property } = _decorator;

@ccclass('InitData')
export class InitData extends Component {

    @property(Loading)
    loading: Loading = null;
    @property(Node)
    bg: Node = null;

    /** 单例模式 */
    private static _ins: InitData;
    constructor() {
        super();
        InitData._ins = this;
    }
    public static get ins(): InitData {
        if (!InitData._ins) {
            InitData._ins = new InitData();
        }
        return InitData._ins;
    }

    async onLoad() {
        mtec.cc.adaptBackgroundNode(this.bg);
        // let lang = 
        // I18n.init(lang);
    }

    initUserData() {

        let isnew = LocalStorageMgr.getItem(LocalStorageMgr.isCommer_key)//
        console.log('是否是新手：', isnew);

        //  初始化用户
        if (isnew) {
            LocalStorageMgr.clear();
            console.log("初始化用户");
        }

        //  获取道具个数
        GlobalData.refrashPropNum = this.getOrSetLocalData(LocalStorageMgr.refrashPropNum_key, GlobalData.refrashPropNum);
        GlobalData.hummerPropNum = this.getOrSetLocalData(LocalStorageMgr.hummerPropNum_key, GlobalData.hummerPropNum)
        //  分数记录
        GlobalData.curScore = this.getOrSetLocalData(LocalStorageMgr.curScore_key, GlobalData.curScore);
        GlobalData.historyHighScore = this.getOrSetLocalData(LocalStorageMgr.historyHighScore_key, GlobalData.historyHighScore);
        GlobalData.todayHighScore = this.getOrSetLocalData(LocalStorageMgr.todayHighScore_key, GlobalData.todayHighScore);
        // console.log(GlobalData.curScore, GlobalData.historyHighScore, GlobalData.todayHighScore);


        //  是否新手
        GlobalData.isCommer = this.getOrSetLocalData(LocalStorageMgr.isCommer_key, GlobalData.isCommer);
        console.log("是否是新手", GlobalData.isCommer);

        //  引导记录
        GlobalData.guideRecord = this.getOrSetLocalData(LocalStorageMgr.guideRecord_key, GlobalData.guideRecord);

        //  上次游戏保存的地图
        GlobalData.lastGameBoard = this.getOrSetLocalData(LocalStorageMgr.lastGameBoardData_key, GlobalData.lastGameBoard);

        //  获取上次登录时间
        GlobalData.lastLoginDate = this.getOrSetLocalData(LocalStorageMgr.lastLoginDate_key, GlobalData.lastLoginDate);

        this.loading.enterGame('main')
    }

    public getOrSetLocalData(storage_key: string, storage: any) {
        let ishave = LocalStorageMgr.hasItem(storage_key);
        if (!ishave) {
            LocalStorageMgr.setItem(storage_key, storage);
        }
        return LocalStorageMgr.getItem<typeof storage>(storage_key);
    }


    start() {

    }

    update(deltaTime: number) {

    }
}

