import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { tools } from '../../tools';
import { bag_item_data, widget_item_data, zhao_xi_jie_item_data } from '../../../data/data';
import { Attributes } from '../Attributes';
const { ccclass, property } = _decorator;

@ccclass('select_task_zhao_xi_jie_item')
export class select_task_zhao_xi_jie_item extends Component {
    @property(Node) btn_select_widget:Node = null;
    @property(Node) lab_select_widget:Node = null;
    @property(Node) btn_select_res:Node = null;
    @property(Node) lab_select_res:Node = null;
    @property(Node) btn_delete:Node = null;
    @property(Node) btn_add_tip_res:Node = null;
    protected m_call_back = null;
    protected m_data:zhao_xi_jie_item_data = null;
    protected m_index:number = 0;
    public initView(data:zhao_xi_jie_item_data,call,index){
        this.m_data = data;
        this.m_call_back = call;
        this.m_index = index;

        if(this.m_data.tips_res==undefined||this.m_data.tips_res==null){
            this.m_data.tips_res = ""
        }
        if(this.m_data.tips_res_name==undefined||this.m_data.tips_res_name==null){
            this.m_data.tips_res_name = ""
        }
        this.btn_add_tip_res.on(Node.EventType.TOUCH_END,()=>{
            tools.select_res_list((data:bag_item_data)=>{
                this.m_data.tips_res = data.url;
                this.m_data.tips_res_name = data.name;
                this.updateShowStatus()
            })
        })

        this.btn_select_res.on(Node.EventType.TOUCH_END,()=>{
            tools.select_res_list((data:bag_item_data)=>{
                this.m_data.res = data.url;
                this.m_data.res_name = data.name;
                this.updateShowStatus()
            })
        })
        this.btn_select_widget.on(Node.EventType.TOUCH_END,()=>{
            let list =  Attributes.Singleton.get_cur_scene_all_only_widget()
            if(list.length<=0){
                return tools.showToast("当前场景没有添控件！")
            }
            tools.show_select_widget_list(list,(item:widget_item_data)=>{
                this.m_data.widget_id = item.att.id;
                this.updateShowStatus()
            },this.m_data.widget_id)
        })
        this.btn_delete.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_call_back!=null){
                this.m_call_back(this)
            }
        })
        this.updateShowStatus()
    }

    public getIndex(){
       return this.m_index;
    }

    updateShowStatus(){
        tools.loadUrl(this.m_data.res,this.node.getComponent(Sprite))
        this.lab_select_widget.getComponent(Label).string = this.m_data.widget_id!=-1?`当前选择的控件:${this.m_data.widget_id}`:"选择控件"
        this.lab_select_res.getComponent(Label).string = this.m_data.res_name.length>0?this.m_data.res_name:"选择资源"
        tools.loadUrl(this.m_data.tips_res,this.btn_add_tip_res.getComponent(Sprite))
    }
}


