import i18n from "../../../i18n/i18n";
import Main from "../../../main/Main";
import GoodItem from "../../common/GoodItem";
import EquipViewAttr from "../../common/EquipViewAttr";
import MainAtterItem from "./MainAtterItem";
import { __EquipData } from "../../data/sdata/SManage";
import FFCalAttr from "../../data/FFCalAttr";

const {ccclass, property} = cc._decorator;

@ccclass
export default class EquipInfo extends cc.Component {

    @property(GoodItem)
    mEquipItem: GoodItem = null;

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

    @property([MainAtterItem])
    mMainNode: Array<MainAtterItem> = [];//主属性节点

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

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

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

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

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

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


    public main:Main;
    public equipItem:GoodItem;
    /**
     * 
     * @param main 
     * @param equip 主装备
     * @param equip1 对比的装备
     */
    public init(main:Main,equipItem:GoodItem,equipItem1:GoodItem){
        if(!equipItem.equip){
            this.node.active = false
            return
        }
        this.node.active = true;
        this.main = main;
        this.equipItem = equipItem;
        this.mName.string = i18n.t(equipItem.equipData.name)
       
        this.mEquipItem.equip = equipItem.equip;
        this.mEquipItem.equipData = equipItem.equipData;
        this.mEquipItem.flushEquip(main);

        let equipData = equipItem.equipData
        let atkNode = this.mMainNode[0]
        let fAttr = FFCalAttr.getEquipAttr(this.main,this.mEquipItem.equip,equipData)
        let fAttr1 = null
        if(equipItem1 && equipItem1.equipData){
            fAttr1 = FFCalAttr.getEquipAttr(this.main,equipItem1.equip,equipItem1.equipData)
            atkNode.setContrase(fAttr.zdl,fAttr1.zdl)
        }else{
            atkNode.setValue(fAttr.zdl);
        }

        atkNode = this.mMainNode[1]
        if(fAttr.atk > 0){
            atkNode.node.active = true;
            if(fAttr1){
                atkNode.setContrase(fAttr.atk,fAttr1.atk)
            }else{
                atkNode.setValue(fAttr.atk);
            }
        }else{
            atkNode.node.active = false;
        }
        atkNode = this.mMainNode[2]
        if(equipData.def > 0){
            atkNode.node.active = true;
            if(fAttr1){
                atkNode.setContrase(fAttr.def,fAttr1.def)
            }else{
                atkNode.setValue(fAttr.def);
            }
        }else{
            atkNode.node.active = false;
        }
        atkNode = this.mMainNode[3]
        if(equipData.hp > 0){
            atkNode.node.active = true;
            if(fAttr1){
                atkNode.setContrase(fAttr.hp,fAttr1.hp)
            }else{
                atkNode.setValue(fAttr.hp);
            }
        }else{
            atkNode.node.active = false;
        }
        atkNode = this.mMainNode[4]
        if(equipData.sp > 0){
            atkNode.node.active = true;
            if(fAttr1){
                atkNode.setContrase(fAttr.sp,fAttr1.sp)
            }else{
                atkNode.setValue(fAttr.sp);
            }
        }else{
            atkNode.node.active = false;
        }

      
        
        let equip = equipItem.equip
        if(equip.attr.length <= 0){
            this.mFAttrNode.active = false;
        }else{
            this.mFAttrNode.destroyAllChildren();
            this.mFAttrNode.active = true;
            for (let i = 0; i < equip.attr.length; i++) {
                const element = equip.attr[i];
                let node = cc.instantiate(this.mFAttrItem);
                let eAttr = node.getComponent(EquipViewAttr);
                eAttr.init(this.main,element);
                node.parent = this.mFAttrNode;
            }
        }

        if(equipData.skill > 0){
            this.mSkillNode.active = true;
            let skillData = this.main.sManage.getSkillById(equipData.skill);
            this.mSkillName.string = i18n.t(skillData.name);
            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;
        }
    }
   

}
