import { v3 } from "cc";
import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { Checkpoint } from "../../checkpoint/Checkpoint";
import { Account } from "../Account";
import { AccountModelComp } from "../model/AccountModelComp";

/** 请求玩家游戏数据 */
@ecs.register('AccountInit')
export class AccountInitComp extends ecs.Comp {
    reset() { }
}

/** 请求玩家游戏数据 */
export class AccountInitSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(AccountInitComp, AccountModelComp);
    }

    entityEnter(e: Account): void {
        let data =
        {
            lv: 1,
        }
        const localData = oops.storage.get("account")
        if (localData) {
            data.lv = Number(localData)
        }

        this.createLevel(e, data);
        e.remove(AccountInitComp);
    }

    /** 创建关卡对象（自定义逻辑） */
    private createLevel(e: Account, data: any) {
        const level = ecs.getEntity<Checkpoint>(Checkpoint);

        // 关卡等级数据
        level.upgrade(data.lv);

        e.checkpoint = level;
        e.addChild(level)
    }

    /** 设置本地存储的用户标识 */
    private setLocalStorage(level: number) {
        oops.storage.set("account", level);
    }
}