import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { show_select_more_scene_page_item } from './show_select_more_scene_page_item';
import { scene_item_data } from '../../data/data';
const { ccclass, property } = _decorator;

@ccclass('show_select_more_scene_page')
export class show_select_more_scene_page extends Component {
    @property(Node) content: Node = null;
    @property(Node) btn_sure: Node = null;
    @property(Node) btn_cancel: Node = null;
    @property(Prefab) item_prefab: Prefab = null;
    private m_callback: Function = null;
    private cur_select_index:number = -1;
    start() {
        this.btn_sure.on(Node.EventType.TOUCH_END, ()=> {
            if(this.m_callback!=null) {
                this.m_callback(this.cur_select_index)
            }
            this.close()
        },this)
        this.btn_cancel.on(Node.EventType.TOUCH_END, ()=> {
            this.close()
        },this)
    }

    show(data_list:scene_item_data[], cur_index:number, callback:Function) {
        this.m_callback = callback
        this.cur_select_index = cur_index
        for(let index = 0; index < data_list.length; index++) {
            // let element:scene_item_data = data_list[index]
            let item = instantiate(this.item_prefab)
            item.parent = this.content
            let name = '第' + (index+1) + '页'
            item.getComponent(show_select_more_scene_page_item).initView(name, index, this.onItemSelect.bind(this))
        }

        this.updaetSelectStatus()
    }

    onItemSelect(item:show_select_more_scene_page_item){
        if(item.getSelect()){
        }else{
            this.cur_select_index = item.getIndex()
            this.updaetSelectStatus()
        }
        this.updaetSelectStatus()
    }

    updaetSelectStatus(){
        for (let index = 0; index < this.content.children.length; index++) {
            const element = this.content.children[index];
            element.getComponent(show_select_more_scene_page_item).setIsSelect(element.getComponent(show_select_more_scene_page_item).getIndex()==this.cur_select_index)
            element.getComponent(show_select_more_scene_page_item).updatSelectStatus()
        }
    }

    close() {
        this.node.removeFromParent()
    }
}

