import { _decorator, Component, EditBox, instantiate, Node } from 'cc';
import { interact_page_data, ui_att_item } from '../../../data/data';
import { receive_res_item } from './receive_res_item';
import { tools } from '../../tools';
import { Attributes } from '../Attributes';
import { config } from '../../config';
import { ClientEvent } from '../../clientEvent';
const { ccclass, property } = _decorator;

@ccclass('res_interact_page')
export class res_interact_page extends Component {
    @property(Node) bg:Node = null;
    @property(Node) btn_interact_left:Node = null;
    @property(Node) btn_interact_right:Node = null;
    @property(Node) img_item_interact_bg:Node = null;
    @property(Node) btn_sure:Node = null;
    @property(Node) img_item_interact:Node = null;
    @property(Node) btn_add:Node = null;
    @property(Node) btn_sub:Node = null;
    @property(Node) content:Node = null;
    @property(EditBox) delete_index_eb:EditBox = null;
    private img_item_interact_list:Node[] =null;
    private m_data:interact_page_data = null;
    protected start(): void {
        this.btn_add.on(Node.EventType.TOUCH_END,()=>{
            let item = instantiate(this.img_item_interact);
            item.parent = this.content;
            item.name = `${this.m_data.img_item_interact_list.length}`
            let data = new ui_att_item
            data.width = 100;
            data.height = 100;
            this.m_data.img_item_interact_list.push(data)
            ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),config.attributes_list_type.top)
            item.getComponent(receive_res_item).initView(data)
            tools.showToast("添加成功！")
         
        })
        this.btn_sub.on(Node.EventType.TOUCH_END,()=>{
            let index = parseInt(this.delete_index_eb.string)
            if(index===0){
                return tools.showToast("不可删除第一个！")
            }
            if(index<this.m_data.img_item_interact_list.length){
                this.removeNodeByName(`${index}`)
                this.m_data.img_item_interact_list.splice(index,1)
                tools.showToast("删除成功！")
                ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),config.attributes_list_type.top)
            }else{
                return tools.showToast("没有找到该道具")
            }

        })
    }
    public initView(data:interact_page_data){
        this.m_data = data;
        this.bg.getComponent(receive_res_item).initView( this.m_data.bg)
        this.btn_interact_left.getComponent(receive_res_item).initView( this.m_data.btn_interact_left)
        this.btn_interact_right.getComponent(receive_res_item).initView( this.m_data.btn_interact_right)
        this.img_item_interact_bg.getComponent(receive_res_item).initView( this.m_data.img_item_interact_bg)
        this.btn_sure.getComponent(receive_res_item).initView( this.m_data.btn_sure)
        this.img_item_interact.name = `${0}`
        this.img_item_interact.getComponent(receive_res_item).initView( this.m_data.img_item_interact_list[0])
        
        for (let index = 1; index < this.m_data.img_item_interact_list.length; index++) {
            let name =  `${index}`;
            let item = null;
            if(this.content.getChildByName(name)!=null){
                item = this.content.getChildByName(name);
            }else{
                item = instantiate(this.img_item_interact);
                item.parent = this.content;
                item.name = `${index}`
            }
            item.getComponent(receive_res_item).initView( this.m_data.img_item_interact_list[index])
        }
     
    }

    

    public removeNodeByName(name:string){
        for (let index = 0; index < this.content.children.length; index++) {
            const element = this.content.children[index];
            if(element.name===name){
                element.destroy()
                element.removeFromParent()
                return 
            }
        }
    }
}


