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

@ccclass('event_collect_widget_score')
export class event_collect_widget_score extends Component {
    private m_data:event_collect_widget_score_item = null;
    @property(Node) btn_select_collect_event:Node = null;
    @property(EditBox) editbox_total_score:EditBox = null;
    @property(EditBox) editbox_max_score:EditBox = null;
    @property(Node) content:Node = null;
    @property(Prefab) item_prefab:Prefab = null;
    private _self_event_id:number = 0;
    start() {
        this.editbox_total_score.node.on(EditBox.EventType.EDITING_DID_ENDED, ()=>{
            if(this.editbox_total_score.string.length<=0) {
                this.editbox_total_score.string = '0'
            }
            this.m_data.total_score = parseInt(this.editbox_total_score.string)
            this.updateScoreStatus()
        })
        this.editbox_max_score.node.on(EditBox.EventType.EDITING_DID_ENDED, ()=>{
            if(this.editbox_max_score.string.length<=0) {
                this.editbox_max_score.string = '0'
            }
            this.m_data.max_score = parseInt(this.editbox_max_score.string)
            this.updateScoreStatus()
        })
        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()
                    }
                }
            })
          
        })
    }

    public initView(data:event_collect_widget_score_item,self_event_id:number){
        this.m_data = data;
        this._self_event_id = self_event_id;
        this.updateScoreStatus()
        this.updateStatus()
    }

    updateScoreStatus() {
        if(this.m_data.total_score==undefined||this.m_data.total_score==null) {
            this.m_data.total_score = 0
        }
        this.editbox_total_score.string = this.m_data.total_score.toString()

        if(this.m_data.max_score==undefined||this.m_data.max_score==null) {
            this.m_data.max_score = 0
        }
        this.editbox_max_score.string = this.m_data.max_score.toString()
    }

    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()
                }
            })
        }
    }
}

