import { dynamicAtlasManager, macro, v3,game } 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";
import { UIID } from "../../common/config/GameUIConfig";
import { VibrationManager } from "../../../platform/vibration/VibrationManager";
import { AdManager } from "../../../platform/ad/AdManager";

/** 请求玩家游戏数据 */
@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 {
        e.AccountModel.vmAdd()
        e.AccountModelSetting.vmAdd()
        let data =
        {
            lv: 1,
        }
        const localData = oops.storage.get("lv")
        if (localData) {
            data.lv = Number(localData)
        }
        e.AccountModelSetting.vm.effectOn = oops.storage.getBoolean("effectOn", true)
        e.AccountModelSetting.vm.musicOn = oops.storage.getBoolean("musicOn", true)
        e.AccountModelSetting.vm.vibrationOn = oops.storage.getBoolean("vibrationOn", true)
        const localSkin = oops.storage.get("skin")
        if (localSkin) {
            e.AccountModel.skin = Number(localSkin)
        }
        e.AccountModel.reward = oops.storage.getBoolean("reward",false)
        // e.AccountModel.reward = false
        // macro.CLEANUP_IMAGE_CACHE = false;
        // dynamicAtlasManager.enabled = true;
        // oops.log.trace(`奖励结果:${e.AccountModel.reward}`)
        game.setFrameRate(30);
        VibrationManager.getInstance().switchVibration = e.AccountModelSetting.vm.vibrationOn
        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)
    }

}