import { _decorator, Component, Node } from 'cc';
import { JNGLayerBase } from '../../../components/JNComponent';
import JNScrollView from '../../../../../extensions/ngame/assets/ngame/util/components/scrollview/JNScrollView';
import GOnHookData from '../../../data/GOnHookData';
import { app, TD } from '../../../App';
import { NodeEventType } from 'cc';
import { TablePetIconSelectScroll } from '../../Consts/Pet/table/TablePetIconSelectScroll';
import { Label } from 'cc';
import GOnHookManager from '../../../manager/battle/mode/GOnHookManager';
import { GUI } from '../../UIConfig';
import { Toggle } from 'cc';
const { ccclass, property } = _decorator;

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

    //野怪列表
    @property(JNScrollView)
    views:JNScrollView;

    //挂机按钮
    @property(Label)
    onHookLabel:Label;

    //是否自动下一关
    @property(Toggle)
    toggle:Toggle;
    
    onJNLoad(){

        super.onJNLoad();

        this.onUpdateView();

        //向子节点添加点击事件
        this.views.addItemEvent(NodeEventType.TOUCH_START,this.onClickItem.bind(this));

    }

    onUpdateView(){

        //显示当前地图可出现的所有宠物
        let mapInfo = TD.TbGOnHookMaps.get(GOnHookData.getIns().info.onHookMap);
        let pets = mapInfo.petIds.map(petId => TD.TbGRole.get(petId));
        this.views.refreshData(pets);
        
        this.onHookLabel.string = (!GOnHookManager.getIns().isOnHook) ? "挂机" : "取消挂机"
        
    }

    //点击Item
    onClickItem(index:number){

        let datas = this.views.getItems<TablePetIconSelectScroll>();
        datas[index].select.isSelect = !datas[index].select.isSelect;

    }

    //点击挂机
    onClickOnHook(){
        GOnHookManager.getIns().onHookCatchPets = this.views.getItems<TablePetIconSelectScroll>().filter(item => item.select.isSelect).map(item => item.data)
        GOnHookManager.getIns().isOnHook = !GOnHookManager.getIns().isOnHook;
        GOnHookManager.getIns().isAutoNext = this.toggle.isChecked;
        app.layer.Open(GUI.Tips,{text:`${(GOnHookManager.getIns().isOnHook) ? "挂机" : "取消挂机"} 设置成功`})
        this.close();
    }


}


