import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { event_delete_collect_widget_score_assigned_widget_item, widget_item_data } from '../../../data/data';
import { collect_event_item } from './collect_event_item';
import { Attributes } from '../Attributes';
import { config } from '../../config';
import { tools } from '../../tools';
const { ccclass, property } = _decorator;

@ccclass('event_delete_collect_widget_score_assigned_widget')
export class event_delete_collect_widget_score_assigned_widget extends Component {
    @property(Node) btn_select_collect_event:Node = null;
    @property(Node) content:Node = null;
    @property(Prefab) item_prefab:Prefab = null;
    private m_data:event_delete_collect_widget_score_assigned_widget_item = null;

    protected start(): void {
        this.btn_select_collect_event.on(Node.EventType.TOUCH_END, ()=>{
            let list = Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.QUESTION_SELECT)
            if(list.length<=0){
                return tools.showToast("当前场景没有添加控件！")
            }
            tools.show_select_widget_list(list,(item:widget_item_data)=>{
                let id = item.att.id
                let index = this.m_data.widget_id_list.indexOf(id)
                let cb = (()=>{
                    this.m_data.widget_id_list.push(id)
                    this.updateStatus()
                })
                
                if(this.m_data.widget_id_list.length==0) {
                    cb()
                } else {
                    if(index>-1) {
                        tools.showToast('已在列表中')
                    } else {
                        cb()
                    }
                }
            })
        })
    }

    initView(data:event_delete_collect_widget_score_assigned_widget_item, self_event_id:number) {
        this.m_data = data
        this.updateStatus()
    }

    updateStatus(){
        this.content.removeAllChildren()
        for (let index = 0; index <  this.m_data.widget_id_list.length; index++) {
            const id =  this.m_data.widget_id_list[index];
            let item = instantiate(this.item_prefab);
            item.parent = this.content;
            item.getComponent(collect_event_item).initView(id,true)
            item.getComponent(collect_event_item).initDeleteCallback((id:number)=>{
                let index = this.m_data.widget_id_list.indexOf(id)
                if(index>-1) {
                    this.m_data.widget_id_list.splice(index,1)
                    this.updateStatus()
                }
            })
        }
    }
}

