import { _decorator, Component, instantiate, Node, Prefab, ScrollView } from 'cc';
import { bag_item_data } from '../../data/data';
import { config } from '../config';
import { img_item } from './img_item';
import { control } from './control';
import { sound_item } from './sound_item';
const { ccclass, property } = _decorator;

@ccclass('res_list')
export class res_list extends Component {
    private item_prefab:Prefab = null;
    private m_type:number = 0;
    private m_data:bag_item_data[] = []
    public initView(pf:Prefab,type:number,data:bag_item_data[]){
        this.item_prefab = pf;
        this.m_data = data;
        this.m_type = type;
        this.node.getComponent(ScrollView).content.removeAllChildren()
        for (let index = 0; index < this.m_data.length; index++) {
            const element = this.m_data[index];
            let url = element.url;
            let name = element.name;
            let item = instantiate(this.item_prefab)
            item.parent = this.node.getComponent(ScrollView).content;
            if(this.m_type===config.select_res_btn_type.SOUND_LIST){
                item.getComponent(sound_item).initView(config.select_res_btn_type.SOUND_LIST,element,name,control.mp3_cache.get(name))
            }else{
                item.getComponent(img_item).initView(this.m_type,element,name,control.res_map.get(name))
            }
        }
    }

    public getType(){
        return this.m_type;
    }

    public getData(){
        return this.m_data;
    }
}


