import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { ClientEvent } from '../../clientEvent';
import { attributes_data, event_item_show_question_select, event_item_show_question_select_item, widget_item_data } from '../../../data/data';
import { Attributes } from '../Attributes';
import { tools } from '../../tools';
import { config } from '../../config';
import { show_question_select_item } from './show_question_select_item';
const { ccclass, property } = _decorator;

@ccclass('event_show_question_select')
export class event_show_question_select extends Component {
    @property(Node) content:Node = null;
    @property(Node) btn_select_widget:Node = null;
    @property(Node) lab_select_widget:Node = null;
    @property(Prefab) prefab_item:Prefab = null;
    private cur_widget_item_data:widget_item_data = null;
    private m_data:event_item_show_question_select = null;
    public initView(data:event_item_show_question_select){
        this.m_data = data;
        this.btn_select_widget.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)=>{
                this.m_data.binding_widget_id = item.att.id;
                this.cur_widget_item_data = item;
                this.updateStatus()
            },this.m_data.binding_widget_id)
        })
        this.updateStatus()
        ClientEvent.on(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
    }

    protected onDestroy(): void {
        ClientEvent.off(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
    }
    UpdateAttributesToView(data:attributes_data,update_type:string){
        if(this.m_data.binding_widget_id===data.id){
            if(update_type===config.attributes_list_type.delete){
                this.m_data.binding_widget_id = -1;
            }else if(update_type===config.attributes_list_type.question_select){
                this.cur_widget_item_data = Attributes.Singleton.get_cur_scene_widget_by_id(this.m_data.binding_widget_id);
                this.updateStatus()
            }
        
      
        }
    }
    updateStatus(){
        if(this.m_data.binding_widget_id===-1){
            this.content.active = false;
            this.lab_select_widget.getComponent(Label).string = "选择问题选择控件"
        }else{
            this.content.active = true;
            this.lab_select_widget.getComponent(Label).string = "选择问题选择控件id："+this.m_data.binding_widget_id;
            if(this.cur_widget_item_data==null){
                this.cur_widget_item_data = Attributes.Singleton.get_cur_scene_widget_by_id(this.m_data.binding_widget_id);
            }
            this.initListView()
            
        }


    }

    initListView(){
        this.content.removeAllChildren()
        let list = this.cur_widget_item_data.att.question_select.btn_question_list;
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let item = instantiate(this.prefab_item)
            item.parent = this.content;
            let _event_item_show_question_select_item:event_item_show_question_select_item = null;
            if( this.m_data.question_list[index]==null||this.m_data.question_list[index]==undefined){
                _event_item_show_question_select_item = new event_item_show_question_select_item()
                _event_item_show_question_select_item.binding_event_id = -1;
                _event_item_show_question_select_item.isRight = false;
                this.m_data.question_list.push(_event_item_show_question_select_item)
            }else{
                _event_item_show_question_select_item =  this.m_data.question_list[index]
            }
          
            item.getComponent(show_question_select_item).initView(element,_event_item_show_question_select_item)
        }
    }
}


