import { _decorator, assetManager, Color, Component, ImageAsset, Label, Node, Sprite, SpriteFrame, Texture2D } from 'cc';
import { bag_item_data } from '../data/data';
const { ccclass, property } = _decorator;

@ccclass('select_res_list_item')
export class select_res_list_item extends Component {
    @property(Node) lab_name:Node = null;
    @property(Node) icon:Node = null;
    private m_data:bag_item_data = null;
    private m_call_back = null;
    public initView(data:bag_item_data,call_back,isImg:boolean = true){
        this.m_data = data;
        this.m_call_back = call_back;
        this.lab_name.getComponent(Label).string = data.name;
        this.node.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_call_back!=null){
                this.m_call_back(this)
            }
        })
        if(isImg){
            assetManager.loadRemote<ImageAsset>(this.m_data.url, (err, imageAsset2)=>{
                if (!err && imageAsset2) {        
                    const texture = new Texture2D();
                    texture.image = imageAsset2;
                    let spFrame2 = new SpriteFrame();     
                    spFrame2.texture = texture;
                    this.icon.getComponent(Sprite).spriteFrame = spFrame2;
                }
            }); 
        }

    }

    public selectStatus(){
        this.node.getComponent(Sprite).color = Color.GREEN
    }

    public unSelectStatus(){
        this.node.getComponent(Sprite).color = Color.WHITE
    }
    public getData(){
        return this.m_data;
    }
}


