import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { Attributes } from './Attributes';
import { widget_item } from './widget_item';
import { widget_item_data } from '../../data/data';
import { ClientEvent } from '../clientEvent';
import { config } from '../config';
const { ccclass, property } = _decorator;

@ccclass('show_widget_list')
export class show_widget_list extends Component {
    @property(Node) content:Node = null;
    @property(Node) btn_close:Node = null;
    @property(Prefab) item_prefab:Prefab = null;
    private call_back = null;
    @property(Node) btn_show_all:Node =null;
    @property(Node) btn_hide_all:Node = null;
    public show(list){
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let item = instantiate(this.item_prefab)
            item.parent = this.content;
            item.getComponent(widget_item).initHideDragOtherList()
            item.getComponent(widget_item).initView(element,this.onItemClick.bind(this),null)
        }
        this.btn_close.on(Node.EventType.TOUCH_END,()=>{
            this.close()
        })

        this.btn_show_all.on(Node.EventType.TOUCH_END,()=>{
            this.onShowAll()
        })
        this.btn_hide_all.on(Node.EventType.TOUCH_END,()=>{
            this.onHideAll()
        })
    }
    
    onShowAll(){
        for (let index = 0; index < this.content.children.length; index++) {
            const item = this.content.children[index];
            if(item.getComponent(widget_item).getShowWidgetData()!=null){
                let type = item.getComponent(widget_item).getShowWidgetData().type;
                if(type!=config.Widget_Type_List.TEXT_SOUND){
                    item.getComponent(widget_item).setSelectStatus()
                }
            }
  
        }
    }
    onHideAll(){
        for (let index = 0; index < this.content.children.length; index++) {
            const item = this.content.children[index];
            if(item.getComponent(widget_item).getShowWidgetData()!=null){
                let type = item.getComponent(widget_item).getShowWidgetData().type;
                if(type!=config.Widget_Type_List.TEXT_SOUND){
                    item.getComponent(widget_item).setUnSelectStatus()
                }
            }
        }
    }
    onItemClick(data:widget_item_data){
        ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,data.att,config.attributes_list_type.active)
    }

    close(){
        this.node.destroy()
    }
}


