import { _decorator, Component, Node ,Button,director, PageView, NodeEventType, Prefab, instantiate, Animation, Vec3} from 'cc';
import { GameInfo } from './GameInfo';
import { resMgr } from './Frames/ResourcesMgr';
import { dataMgr } from './Frames/DataManager';
import { UIMgr } from './Frames/UIManager';
import { UIBase } from './Game/GameFrameWork/UIBase';
const { ccclass, property } = _decorator;

@ccclass('SelectScene')
export class SelectScene extends UIBase {
//export class SelectScene extends UIBase {
    // protected onStart(){
        //this.onBtnClick("按钮名字",this.onBtnHelp)
    // }
    protected onStart(){

    }
    @property(Node)
    pageView: Node = null;
    @property(Prefab)
    effectPrefab: Prefab = null;
    @property(Node)
    selectLv: Node = null;
    @property(Node)
    selectTheme: Node = null;

    currentPageIndex:number = null;
    totalPages:number = null;

    //解锁的主题数
    unLockedTheme:number = null;

    //锁的图标
    locked: Node = null;

    btnRight: Node = null;
    btnLeft: Node = null;

    start() {
        // console.log("resMgr:" + resMgr)
        // console.log("dataMgr:" + dataMgr.getAllDataByName("GameResPath"));
         //总页数
         this.totalPages = this.pageView.getComponent(PageView).getPages().length;
         //当前索引值
         this.currentPageIndex = this.pageView.getComponent(PageView).getCurrentPageIndex();

        const buttonConfigs = [
            {name: "SelectTheme/UI_SelectTheme/BtnHome" , handler : this.onBtnHome},
            {name: "UIPublic/_BtnHelp" , handler : this.onBtnHelp},
            {name: "SelectTheme/UI_SelectTheme/BtnLeft" , handler : this.onBtnLeft},
            {name: "SelectTheme/UI_SelectTheme/BtnRight" , handler : this.onBtnRight}
        ]

        buttonConfigs.forEach(config =>{
            let button = this.node.getChildByPath(config.name);
            if(button){
                button.on(Button.EventType.CLICK,config.handler,this);
            } else{
                console.warn(`Button not found: ${config.name}`);
            }
            //button.on(Button.EventType.CLICK,config.handler,this);
        })
        // let BtnHome = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnHome");
        // BtnHome.on(Button.EventType.CLICK,this.onBtnHome,this);

        // let BtnHelp = this.node.getChildByPath("UIPublic/BtnHelp");
        // BtnHelp.on(Button.EventType.CLICK,this.onBtnHelp,this);

        // let BtnLeft = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnLeft");
        // BtnLeft.on(Button.EventType.CLICK,this.onBtnLeft,this);

        // let BtnRight = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnRight");
        // BtnRight.on(Button.EventType.CLICK,this.onBtnRight,this);

        let touchArea = this.node.getChildByPath("SelectTheme/TouchArea");
        touchArea.on(NodeEventType.TOUCH_START,this.onTouchStart,this);
        touchArea.on(NodeEventType.TOUCH_END,this.onTouchEnd,this);
        
        this.node.on(Node.EventType.MOUSE_DOWN,this.onMouseTouchStart,this);

        this.unLockedTheme = GameInfo.Instance.UnLockTheme;
        
        this.locked = this.node.getChildByPath('SelectTheme/UI_SelectTheme/Locked');

        this.btnRight = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnRight");
        this.btnLeft = this.node.getChildByPath("SelectTheme/UI_SelectTheme/BtnLeft");
    }   

    //返回开始游戏场景
    onBtnHome(){
        director.loadScene("StartScene")
    }

    //帮助按钮
    onBtnHelp(){
        // if(!this.node.getChildByName('Help').active){
        //     this.node.getChildByName("Help").active = true;
        // }
        UIMgr.openUI("Help");
    }

    onBtnLeft(){
        this.currentPageIndex = Math.max(0, this.currentPageIndex - 1);
        this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);  
    }

    onBtnRight(){
        if (this.currentPageIndex === this.totalPages - 1) {  
            this.currentPageIndex = this.totalPages - 1; // 保持在最后一页  
        } else {
            this.currentPageIndex = Math.min(this.currentPageIndex + 1, this.totalPages -1);  
        }  
            this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
        }

    onTouchStart(){
        GameInfo.Instance.CurTheme = this.currentPageIndex + 1;
        if(this.locked.active){
            console.log("当前主题未解锁");
        }
    }
    onTouchEnd(){
        //当前主题未解锁不可进入选关
        if(this.locked.active){
            return;
        }
        //当前主题解锁了，则可以进入选关
        if(!this.selectLv.active){
            this.selectLv.active = true;
            if(this.selectTheme.active){
                this.selectTheme.active = false;
            }
        }
    }

    //点击特效
    onMouseTouchStart(event){
        const touchPos = event.getUILocation();
        const effectNode = instantiate(this.effectPrefab);
        this.node.addChild(effectNode);
        //effectNode.setWorldPosition(new Vec3(touchPos.x,touchPos.y));
        effectNode.setPosition(new Vec3(touchPos.x - 480,touchPos.y - 320));
        const effectCom = effectNode.getComponent(Animation);
        if(effectCom){
            effectCom.play();
        }
        this.scheduleOnce(()=>{ effectNode.destroy(); },0.5);
    }

    update(deltaTime: number) {
        this.updateButton();
    }

    private updateButton(){
        if(this.btnLeft && this.btnRight){
            if(this.currentPageIndex === this.totalPages - 1 ){
                this.btnLeft.active = true;
                this.btnRight.active = false;
            }
            else if(this.currentPageIndex === 0){
                this.btnLeft.active = false;
                this.btnRight.active = true;
            }else{
                this.btnLeft.active = true;
                this.btnRight.active = true;
            }
        }
        if(this.locked){
            if(this.currentPageIndex < this.unLockedTheme){
                if(this.locked.active){ 
                    this.locked.active = false; 
                }
            } else {
                if(!this.locked.active){ 
                    this.locked.active = true; 
                }
            }
        }
    }
}


