import { _decorator, assetManager, Component, ImageAsset, Label, Node, Rect, Sprite, SpriteFrame, Texture2D, UITransform, Vec2 } from 'cc';
import { attributes_data, bag_item_data, event_item_change_one_item_status, widget_item_data } from '../../../data/data';
import { ClientEvent } from '../../clientEvent';
import { config } from '../../config';
import { base_res } from '../base_res';
import { img_item } from '../img_item';
import { Attributes } from '../Attributes';
import { tools } from '../../tools';
const { ccclass, property } = _decorator;

@ccclass('event_change_one_item_status')
export class event_change_one_item_status extends Component {
    private m_data:event_item_change_one_item_status = null;
    @property(Node) btn_select_widget:Node = null;
    @property(Node) lab_select_widget:Node = null;
    @property(Node) res_node:Node = null;
    @property(Node) lab_res_name:Node = null;
    @property(Node) rect_res:Node = null;
    @property(Node) res_sf:Node = null;
    protected start(): void {
        this.res_sf.on(Node.EventType.TOUCH_END, ()=> {
            tools.select_res_list((data:bag_item_data)=> {
                // console.log('data=',data)
                this.m_data.att.res = data.url
                this.m_data.att.res_name = data.name
                if(data.url.length>0) {
                    assetManager.loadRemote<ImageAsset>(this.m_data.att.res, (err, imageAsset2)=>{
                        if (!err && imageAsset2) {        
                            const texture = new Texture2D();
                            texture.image = imageAsset2;
                            let spFrame2 = new SpriteFrame();     
                            spFrame2.texture = texture;
                            this.res_sf.getComponent(Sprite).spriteFrame = spFrame2
                        }
                    }); 
                }
                this.lab_res_name.getComponent(Label).string = this.m_data.att.res_name
            })
        },this)
    }
    public initView(data:event_item_change_one_item_status){
        this.m_data = data;
        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.binding_widget_id = item.att.id;
                this.updateStatus()
            },this.m_data.binding_widget_id)
        })
        this.updateStatus()
        ClientEvent.on(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
        ClientEvent.on(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
    }

    protected onDestroy(): void {
        ClientEvent.off(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
        ClientEvent.off(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
    }
    UpdateAttributesToView(data:attributes_data,update_type:string){
        if(this.m_data.binding_widget_id===data.id&&update_type===config.attributes_list_type.delete){
            this.m_data.binding_widget_id = -1;
            this.updateStatus()
        }
    }
    updateStatus(){
        if(this.m_data.binding_widget_id===-1){
            this.res_node.active = false;
            this.lab_select_widget.getComponent(Label).string = "选择要改变状态的控件"
        }else{
            this.res_node.active = true;
            this.lab_select_widget.getComponent(Label).string = "当前要改变状态的控件id："+this.m_data.binding_widget_id;
            if(this.m_data.att.res.length>0){
                assetManager.loadRemote<ImageAsset>(this.m_data.att.res, (err, imageAsset2)=>{
                    if (!err && imageAsset2) {        
                        const texture = new Texture2D();
                        texture.image = imageAsset2;
                        let spFrame2 = new SpriteFrame();     
                        spFrame2.texture = texture;
                        this.res_sf.getComponent(Sprite).spriteFrame = spFrame2
                    }
                }); 
            }
            this.lab_res_name.getComponent(Label).string = this.m_data.att.res_name.length>0?this.m_data.att.res_name:"无"
        }
    }

    public getRect(){
        let size = this.rect_res.getComponent(UITransform).contentSize;
        let pos = this.rect_res.parent.getComponent(UITransform).convertToWorldSpaceAR(this.rect_res.position);
        let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
        return rect;
    }

    DragResEndOnCheck(v2:Vec2,node:Node){
        if(this.getRect().contains(v2)){
            let type = node.getComponent(base_res).getType()
            if(type===config.select_res_btn_type.SOUND_LIST){

            }else{
                console.log("event_change_one_item_status-DragResEndOnCheck")
                let temp_data = node.getComponent(img_item).getData()
                this.m_data.att.res = temp_data.url;
                this.m_data.att.res_name = temp_data.name;
                this.updateStatus()
            }
        }
    }
}


