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

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

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

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

    @property(sp.Skeleton)
    spine:sp.Skeleton;  //宠物Spine

    info:PlayerPetOV;

    onLoad(){

        this.onUpdateInit();

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

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


    //初始化
    onUpdateInit(){
        //隐藏
        this.starNode.active = false;
        this.levelLabel.node.active = false;
    }

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

    //刷新页面
    onUpdateView(){
        this.onUpdateInit();

        //显示宠物
        this.spine.skeletonData = app.battleRes.getRoleSpine(this.info.petTbId);
        this.spine.setAnimation(0,UIPetAnim.std,true);

        //等级
        this.levelLabel.node.active = true;
        this.levelLabel.string = `Lv ${this.info.petLevel}`;

        //如果有星则显示星
        if(this.info.petStar){
            this.starNode.active = true;
            this.starNode.getComponentInChildren(Label).string = `${this.info.petStar}`;
        }
    }

    //绑定显示的宠物
    bind(info:PlayerPetOV){
        this.info = info;
        this.onUpdateView();
    }

}


