import { _decorator, Component, Node } from 'cc';
import JNLayerBase from '../../../../extensions/ngame/assets/ngame/ui/base/JNLayerBase';
import JNScrollView from '../../../../extensions/ngame/assets/ngame/util/components/scrollview/JNScrollView';
import { app, TD } from '../../App';
import GOnHookData from '../../data/GOnHookData';
import { TbGOnHookMaps } from '../../config/data/schema';
import { MapSelectShowItem } from './MapSelectShowItem';
import { NodeEventType } from 'cc';
import GOnHookManager from '../../manager/battle/mode/GOnHookManager';
const { ccclass, property } = _decorator;

@ccclass('MapSelectView')
export class MapSelectView extends JNLayerBase {

    //地图列表
    @property(JNScrollView)
    views:JNScrollView;

    //当前地图下标
    index:number = -1;

    onJNLoad(data?: any): void {
        super.onJNLoad(data);

        TD.TbGOnHookMaps.getDataList().forEach((item,index) => {
            if(GOnHookData.getIns().info.onHookMap == item.id){
                this.index = index;
            }
        })

        this.views.refreshData(TD.TbGOnHookMaps.getDataList())

        let items = this.views.getItems<MapSelectShowItem>();

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

        items[this.index].isSelect = true;
        items[this.index].onUpdateView();
        
    }

    //点击地图
    onClickItem(index:number){
        let items = this.views.getItems<MapSelectShowItem>();

        items.forEach(item => {
            item.isSelect = false;
        })
        this.index = index;
        items[this.index].isSelect = true;
        this.views.getItems<MapSelectShowItem>().forEach(item => item.onUpdateView())

    }

    //点击确认
    async onClickConfirm(){

        await GOnHookManager.getIns().setMap(this.views.getItems<MapSelectShowItem>()[this.index].data.id);

        //关闭页面
        app.layer.CloseNode(this.node);

    }

}


