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

@ccclass('event_collect_event_score')
export class event_collect_event_score extends Component {
    private m_data:event_collect_event_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.getBeActionEventList()
            if(list.length<=0){
                return tools.showToast("没有可添加的事件")
            }
            tools.select_collect_event(list,(event_list:number[])=>{
                this.m_data.event_id_list =event_list;
                this.updateStatus()
            },this._self_event_id,this.m_data.event_id_list)
          
        })
    }

    public initView(data:event_collect_event_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.event_id_list.length; index++) {
            const id =  this.m_data.event_id_list[index];
            let item = instantiate(this.item_prefab);
            item.parent = this.content;
            item.getComponent(collect_event_item).initView(id)
        }
    }

}

