import { _decorator, Component, Label, Node } from 'cc';
import { GUI } from '../UIConfig';
import ChatData from '../../data/ChatData';
import PlayerData from '../../data/PlayerData';
import GBattleModeManager, { BattleMode } from '../../battle/GBattleModeManager';
import { JNGLayerBase } from '../../components/JNComponent';
import { app } from '../../App';
import { GAction } from '../../consts/GAction';
import { GAPI } from '../../consts/GAPI';
import GOnHookManager, { GOnHookManagerEvent } from '../../manager/battle/mode/GOnHookManager';
import { Button } from 'cc';
import GOnHookData, { GOnHookDataEnum } from '../../data/GOnHookData';
import { Sprite } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MainView')
export class MainView extends JNGLayerBase {

    @property(Label)
    onHookLabel:Label;  //挂机文本

    @property(Button)
    nextLevelBtn:Button;   //下一关按钮
    
    onJNLoad(data?: any): void {

        super.onJNLoad(data);

        //发送消息
        ChatData.getIns().onSend({
            message:`加入游戏`
        });

        this.onUpdateView();

        //监听
        app.event.on(GOnHookManagerEvent.UPDATE_ON_HOOK_STATE,this.onUpdateOnHook,this);   
        app.event.on(GOnHookDataEnum.UPDATE,this.onUpdateOnHookInfo,this);   

    }


    onJNClose(): void {
        super.onJNClose();
        //取消监听
        app.event.off(GOnHookManagerEvent.UPDATE_ON_HOOK_STATE,this.onUpdateOnHook,this);   
        app.event.off(GOnHookDataEnum.UPDATE,this.onUpdateOnHookInfo,this);   
    }

    onUpdateOnHook(){
        this.onHookLabel.string = (GOnHookManager.getIns().isOnHook) ? "挂机中" : "挂机";
    }

    onUpdateOnHookInfo(){
        this.nextLevelBtn.node.active = GOnHookData.getIns().isNextLevel;
    }

    //更新UI界面
    onUpdateView(){
        this.onUpdateOnHookInfo();
        this.onUpdateOnHook();
    }

    //打开Demo页面
    onOpenDemo(){
        app.layer.Open(GUI.CampGuardianView);
    }

    //打开聊天页面
    onOpenChat(){
        app.layer.Open(GUI.MainChat);
    }

    //点击打开无限模式
    onOpenOnHook(){
        GBattleModeManager.getIns().Open(BattleMode.CampGuardian,true);
    }

    //点击PVP模式
    onOpenPVP(){
        app.socket.Send(GAction.S_MODE_PVP_JOIN);
    }

    //点击挂机按钮
    onOpenOnHookView(){
        //取消挂机
        GOnHookManager.getIns().isOnHook = false;
        app.layer.Open(GUI.MainOnHookView);
    }

    //点击地图
    onOpenMap(){
        app.layer.Open(GUI.MapSelectView);
    }

    //点击排行榜
    onOpenOnHookRinking(){
        app.layer.Open(GUI.OnHookRinkingView,GOnHookData.getIns().info.onHookMap);
    }

    //敬请期待
    onClickNotOpen(){
        app.layer.Open(GUI.Tips,{text:"敬请期待"})
    }

    //打开Debugger
    onClickDebugger(){
        app.layer.Open(GUI.Debugger);
    }

    //打开宠物装备
    onClickPetEquip(){
        app.layer.Open(GUI.PetEquipView);
    }

    //点击下一关
    async onClickNextLevel(){
        GOnHookManager.getIns().onNextLevel();
    }

    //点击进入副本
    onClickDungeon(){
        app.layer.Open(GUI.DungeonView);
    }

    //点击打开背包
    onClickPacksack(){
        app.layer.Open(GUI.PacksackView);
    }

    //点击商店页面
    onClickShop(){
        app.layer.Open(GUI.ShopView);
    }

}


