import { _decorator, assetManager, Component, ImageAsset, Node, Size, Sprite, SpriteFrame, Texture2D, UITransform, Vec3 } from 'cc';
import { ui_att_item } from '../../../data/data';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('view_ui_att_item')
export class view_ui_att_item extends Component {
    private m_data:ui_att_item = null;
    getData(new_size:Size=null){
        if(this.m_data===null){
            this.m_data = new ui_att_item;
            let size = this.node.getComponent(UITransform).contentSize;
            let pos = this.node.position;
            this.m_data.width = new_size?new_size.width:size.width;
            this.m_data.height = new_size?new_size.height:size.height;
            this.m_data.x = pos.x;
            this.m_data.y = pos.y;
        }else{
            this.m_data.width = new_size?new_size.width:this.m_data.width;
            this.m_data.height = new_size?new_size.height:this.m_data.height;
        }
        return this.m_data
    }
    updateAtt(data:ui_att_item,check_type:number=config.update_type.update_all){
        if(data!=null){
            if(check_type===config.update_type.update_all||check_type===config.update_type.update_img){
                if(data.res!=""&&data.res.length>0){
                    this.updateRes(data.res)
                }
            }
            this.m_data = data;
            if(check_type===config.update_type.update_all||check_type===config.update_type.update_info){
                let size = new Size(this.m_data.width,this.m_data.height)
                let pos = new Vec3(this.m_data.x,this.m_data.y)
                this.node.getComponent(UITransform).contentSize = size;
                this.node.position = pos;
            }
        }
 
    }
    updateRes(res_path){
        assetManager.loadRemote<ImageAsset>(res_path, (err, imageAsset2)=>{
            if (!err && imageAsset2) {        
                const texture = new Texture2D();
                texture.image = imageAsset2;
                let spFrame2 = new SpriteFrame();     
                spFrame2.texture = texture;
                this.getComponent(Sprite).spriteFrame = spFrame2;
            }
        }); 
    }



}


