import { FFAttr } from "../data/FFCalAttr";
import RedPoint from "../data/RedPoint";
import { PetAttr } from "../data/udata/Player";

/**
 * 宠物头像
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class PetIcon extends cc.Component {

    @property(cc.Sprite)
    mIcon: cc.Sprite = null;

    @property(cc.Node)
    mCheck: cc.Node = null;

    @property(cc.Node)
    mLeaderIcon: cc.Node = null;//队长标识

    public callback:(petIcon:PetIcon)=>void;

    public id:number;
    public data:PetAttr;

    onLoad(){
        this.mCheck.active = false
        this.mLeaderIcon.active = false
    }

    public flushIcon(ffAttr:FFAttr){
        let headIcon = ffAttr.skin
        if(headIcon == '30000'){
            headIcon = ''+this.data.id
        }

        cc.resources.load('icon/role_head/'+headIcon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mIcon.spriteFrame = spriteFrame;
            }
        } );
    }


    public setCallback(callback:(petIcon:PetIcon)=>void){
        this.callback = callback;
    }
    public onclick(){
        if(this.mCheck.active){
            return;
        }
        let nodes = this.node.parent.children;
        for (let i = 0; i < nodes.length; i++) {
            const node = nodes[i];
            if(node == this.node){
                this.mCheck.active = true;
            }else{
                let tmp = node.getComponent(PetIcon);
                tmp.mCheck.active = false;
            }
        }
        if(this.callback){
            RedPoint.removeRed(this.node)
            this.callback(this);
        }
    }

    

}
