import { _decorator, Component, EditBox, Label, Node, Sprite, Toggle } from 'cc';
import { bag_item_data, dai_dao_ju_item, widget_item_data } from '../../../data/data';
import { Attributes } from '../Attributes';
import { tools } from '../../tools';
import { config } from '../../config';
import { gameManager } from '../../run/gameManager';
import { control } from '../control';
const { ccclass, property } = _decorator;

@ccclass('select_task_dai_dao_ju_item')
export class select_task_dai_dao_ju_item extends Component {
    @property(Node) btn_select_widget:Node = null;
    @property(Node) lab_select_widget:Node = null;
    @property(Node) btn_delete:Node = null;
    @property(Node) toggle_is_in_bag:Node = null;
    @property(Node) btn_select_res:Node = null;
    protected m_call_back = null;
    protected m_data:dai_dao_ju_item = null;
    protected m_index:number = 0;
    public initView(data:dai_dao_ju_item,call,index){
        this.m_data = data;
        this.m_call_back = call;
        this.m_index = index;
        this.btn_select_widget.on(Node.EventType.TOUCH_END,()=>{
            let list =  Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.DRAG_TYPE)
            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.toggle_is_in_bag.on(Toggle.EventType.TOGGLE,()=>{
            this.m_data.is_in_bag = this.toggle_is_in_bag.getComponent(Toggle).isChecked;
        })
  
        this.btn_select_res.on(Node.EventType.TOUCH_END,()=>{
            tools.select_res_list((data:bag_item_data)=>{
                this.m_data.info_res.url = data.url;
                this.m_data.info_res.name = data.name;
                this.updateShowStatus()
            })
        })
        this.updateShowStatus()
    }

    public getIndex(){
       return this.m_index;
    }

    updateShowStatus(){
        if(this.m_data.info_res.name.length>0){
            this.node.getComponent(Sprite).spriteFrame = control.res_map.get(this.m_data.info_res.name)
        }
        this.toggle_is_in_bag.getComponent(Toggle).isChecked = this.m_data.is_in_bag;
        this.lab_select_widget.getComponent(Label).string = this.m_data.widget_id!=-1?`当前选择的控件:${this.m_data.widget_id}`:"选择控件"
    }
}


