import { _decorator, Component, instantiate, Node, Prefab, ScrollView } from 'cc';
import { config } from '../config';
import { widget_item } from './widget_item';
const { ccclass, property } = _decorator;

@ccclass('widget_list')
export class widget_list extends Component {
    @property(Prefab) item_prefab:Prefab = null;
    protected start(): void {
        this.initView()
    }
    public initView(){
        config.init()
        this.node.getComponent(ScrollView).content.removeAllChildren()
        config.Widget_Type.forEach((v,k)=>{
            let item:Node = instantiate(this.item_prefab)
            item.parent = this.node.getComponent(ScrollView).content
            item.getComponent(widget_item).initWidgetType()
            item.getComponent(widget_item).initView(v)
        })
    }

    public getWidgetDataByType(type:number){
        let content = this.node.getComponent(ScrollView).content
        for (let index = 0; index < content.children.length; index++) {
            const element = content.children[index];
            if(element.getComponent(widget_item).getData().type==type){
                return element.getComponent(widget_item).getData()
            }
        }
    }

    public getWidgetByType(type:number){
        let content = this.node.getComponent(ScrollView).content
        for (let index = 0; index < content.children.length; index++) {
            const element = content.children[index];
            let data = element.getComponent(widget_item).getData()
            if(data.type==type){
                return element
            }
        }
    }
}


