import { sp } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { app } from '../../../App';
import { UIPetAnim } from '../../../consts/GData';
import { v3 } from 'cc';
import { PlayerPetOV } from '../../../consts/API';
import { Label } from 'cc';
import { PlayerPetEvent } from '../../../data/PlayerPetData';
import PetEquipData from '../../../data/PetEquipData';
const { ccclass, property } = _decorator;

@ccclass('PetIcon')
export class PetIcon extends Component {

    @property(sp.Skeleton)
    spine:sp.Skeleton;

    @property(Node)
    starNode:Node;      //星星

    @property(Label)
    levelLabel:Label;   //等级
    

    info:PlayerPetOV;

    onLoad(){
        //监听
        app.event.on(PlayerPetEvent.UPDATE_INFO,this.onUpdateInfo,this)
    }

    protected onDestroy(): void {
        app.event.off(PlayerPetEvent.UPDATE_INFO,this.onUpdateInfo,this)
    }

    //信息更新
    onUpdateInfo(info:PlayerPetOV){
        if(this.info && info.petId == this.info.petId){
            this.onUpdateView(); //刷新页面
        }
    }

    //刷新页面
    onUpdateView(){

        this.starNode.active = false;

        this.spine.skeletonData = app.battleRes.getRoleSpine(this.info.petTbId);
        this.spine.setAnimation(0,UIPetAnim.std,true);

        if(this.info.petStar){
            this.starNode.active = true;
            this.starNode.getComponentInChildren(Label).string = `${this.info.petStar}`;
        }

        //设置宠物等级
        // this.levelLabel.string = `Lv ${PetEquipData.getIns().getForgingBenchPetLevel(this.info.petId)}`
        this.levelLabel.string = `Lv ${this.info.petLevel}`
        
    }

    //设置icon
    set(info:PlayerPetOV){
        this.info = info;
        this.onUpdateView();
    }
    
}


