import { _decorator, Button, Component, Node, director, PageView, resources, SpriteAtlas, error, Asset, Sprite, UITransform, SpriteFrame } from 'cc';
import { GameInfo } from './GameInfo';
import { resMgr } from './Frames/ResourcesMgr';
import { gameResMgr } from './Game/GameResMgr';
const { ccclass, property } = _decorator;

@ccclass('SelectLevel')
export class SelectLevel extends Component {
    selectTheme: Node = null;
    pageViewLv: Node = null;
    btnStart: Node = null;
    btnBack: Node = null;
    private _themeBgLeft: Node = null;
    private _themeBgRight: Node = null;
    protected onLoad(): void {
        this.btnStart = this.node.getChildByPath("UI_SelectLv/BtnStart");
        this.btnBack = this.node.getChildByPath("UI_SelectLv/BtnBack");

        this.selectTheme = this.node.parent.getChildByPath("SelectTheme");
        this.pageViewLv = this.node.getChildByName("PageViewLv");

        this._themeBgLeft = this.node.getChildByName("Theme_bgLeft");
        this._themeBgRight = this.node.getChildByName("Theme_bgRight");
    }
    protected onEnable(): void {
        this._loadLvRes();
        this.btnStart.getComponent(Button).interactable = true;

    }
    start() {
        //this._LoadLvRes();
        
        this.btnStart.on(Button.EventType.CLICK,this.onBtnStart,this);
        this.btnBack.on(Button.EventType.CLICK,this.onBtnBack,this);

        this.pageViewLv.on(PageView.EventType.PAGE_TURNING,()=>{
            //滑动过程当中 “开始游戏” 按钮无效
            this.btnStart.getComponent(Button).enabled = false;
        })

        this.pageViewLv.on(PageView.EventType.SCROLL_ENDED,()=>{
            //滑动结束 “开始按钮” 生效
            this.btnStart.getComponent(Button).enabled = true;
        })
        //true=>当前主题都可以玩，“开始按钮”不用禁用
        if(GameInfo.Instance.isAllLvUnlocked()){
            return;
        }

        this.pageViewLv.on(PageView.EventType.SCROLL_ENDED,()=>{
            const index = this.pageViewLv.getComponent(PageView).curPageIdx + 1;
            this.btnStart.getComponent(Button).interactable = (index <= GameInfo.Instance.UnLockLv);
            // if(index > GameInfo.Instance.UnLockLv){
            //     this.btnStart.getComponent(Button).interactable = false;
            // } else {
            //     this.btnStart.getComponent(Button).interactable = true;
            // }
        })
    }

    async onBtnStart(){
        GameInfo.Instance.CurLv = this.pageViewLv.getComponent(PageView).curPageIdx + 1;
        await gameResMgr.loadGameRes()
        director.loadScene("MainScene");
    }
    onBtnBack(){
        //清空pageView的content下的节点
        this.pageViewLv.getComponent(PageView).removeAllPages();
        //选关节点隐藏，选主题节点激活
        if(this.node.active){
            if(!this.selectTheme.active){
                this.selectTheme.active = true;
            }
            this.node.active = false;
        }
    }
    //资源导入
    private _loadLvRes(){
        const theme = GameInfo.Instance.CurTheme;

        for(let i = 1; i <= 12; i++){
            let name: string = "000" + i;
            name = name.slice(name.length - 2);
            name = `Theme${theme}_map${name}`;
            const frame = resMgr.getSpriteFrame(name);
            this.pageViewLv.getComponent(PageView).addPage(this._setNode(frame));
        }

        this._themeBgLeft.getComponent(Sprite).spriteFrame = resMgr.getSpriteFrame("Theme"+ theme + "_bgLeft");
        this._themeBgRight.getComponent(Sprite).spriteFrame = resMgr.getSpriteFrame("Theme" + theme + "_bgRight");

        // resources.load("Res/ThemeMap/Theme0" + theme ,SpriteAtlas,(error:Error | null,asset:SpriteAtlas)=>{
        //     if(error){
        //         console.error("No found Theme0" + theme);
        //     } else {
        //         for (const frame of asset.getSpriteFrames()) {
        //             if(frame.name.includes("_map")){
        //                 this.pageViewLv.getComponent(PageView).addPage(this._setNode(frame));
        //             }
        //         }
        //         this._themeBgLeft.getComponent(Sprite).spriteFrame = asset.getSpriteFrame("Theme"+ theme + "_bgLeft");
        //         this._themeBgRight.getComponent(Sprite).spriteFrame = asset.getSpriteFrame("Theme" + theme + "_bgRight");
        //     }
        // })
    }
    //设置PageView的页面
    private _setNode(frame: SpriteFrame): Node{
        const node = new Node();
        node.addComponent(Sprite);
        node.getComponent(UITransform).setContentSize(500, 300);
        node.getComponent(Sprite).sizeMode = Sprite.SizeMode.CUSTOM;
        node.getComponent(Sprite).type = Sprite.Type.SLICED;
        node.getComponent(Sprite).spriteFrame = frame;
        return node;
    }
    update(deltaTime: number) {
        
    }
}


