import i18n from "../../../i18n/i18n";
import ViewObject from "../../../main/ViewObject";
import EquipViewAttr from "../../common/EquipViewAttr";
import PetIcon from "../../common/PetIcon";
import FFCalAttr, { FFAttr } from "../../data/FFCalAttr";

/**
 * 宠物属性详情
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class PetInfo extends ViewObject {

    @property(cc.Label)
    mZdl: cc.Label = null;

    @property(cc.Label)
    mAtk: cc.Label = null;

    @property(cc.Label)
    mDef: cc.Label = null;

    @property(cc.Label)
    mHp: cc.Label = null;

    @property(cc.Label)
    mSp: cc.Label = null;

    @property(cc.Node)
    mFAttrNode: cc.Node = null;//副属性节点

    @property(cc.Prefab)
    mFAttrItem: cc.Prefab = null;

    @property(cc.Node)
    mSkillNode: cc.Node = null;//技能节点

    @property(cc.Label)
    mSkillAbout: cc.Label = null;

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

    /**
     * 
     * @param prev 父界面
     */
    public show(prev?: ViewObject) {
        if (prev) {
            this.prev = prev;
            this.prev.__close();
        }
        this.main.viewManage.popView1(this.node);
        if (this.main && this.main.gameHttp) {
            this.main.gameHttp.pushEvent(this);
        }
    }



    public init(pi: PetIcon) {
        this.node.zIndex = 1
        let ffAttr: FFAttr = FFCalAttr.getAttr(this.main, pi.data)

        this.mZdl.string = '' + ffAttr.zdl;

        this.mAtk.string = '' + ffAttr.atk;
        this.mDef.string = '' + ffAttr.def;
        this.mHp.string = '' + ffAttr.hp;
        this.mSp.string = '' + ffAttr.sp;


        if (ffAttr.attrs.length <= 0) {
            this.mFAttrNode.active = false;
        } else {
            let nodes = this.mFAttrNode.children;
            nodes.forEach(node => {
                if (node.name != 'fsx_name') {
                    node.destroy();
                }
            });

            this.mFAttrNode.active = true;
            for (let i = 0; i < ffAttr.attrs.length; i++) {
                const element = ffAttr.attrs[i];
                let node = cc.instantiate(this.mFAttrItem);
                let eAttr = node.getComponent(EquipViewAttr);
                eAttr.init(this.main, element);
                node.parent = this.mFAttrNode;
            }
        }
        if (ffAttr.weaponSkill > 0) {
            this.mSkillNode.active = true;
            let skillData = this.main.sManage.getSkillById(ffAttr.weaponSkill);
            this.mSkillAbout.string = i18n.t(skillData.about,
                {
                    'VAL1': skillData.value1,
                    'VAL2': skillData.value2,
                    'VAL3': skillData.value3,
                }
            );

            cc.resources.load('icon/skill/' + skillData.icon, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
                if (err) {
                    cc.error(err);
                } else {
                    this.mSkillIcon.spriteFrame = spriteFrame;
                }
            });
        } else {
            this.mSkillNode.active = false;
        }

    }

}
