import i18n from "../../i18n/i18n";
import ViewObject from "../../main/ViewObject";
import GoodItem from "./GoodItem";
import EquipViewAttr from "./EquipViewAttr";
import FFCalAttr from "../data/FFCalAttr";

/**
 * 道具说明
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class GoodAbout extends ViewObject {

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

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

    @property(GoodItem)
    mGoodItem: GoodItem = null;

    @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)
    baseAttNode: cc.Node = null;//基础属性节点

    @property(cc.Node)
    addAttNode: cc.Node = null;//附加属性节点

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

    @property(cc.Node)
    descNode: cc.Node = null;//说明节点

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

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

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

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

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

    @property(cc.Node)
    mEquipNode: cc.Node = null;//装备节点

    @property(cc.Node)
    mGoodNode: cc.Node = null;//道具节点

    @property(cc.Node)
    mUseNode: cc.Node = null;//使用节点

    @property(cc.Node)
    mQHNode: cc.Node = null;//强化按钮节点

    @property(cc.Node)
    mUnInstallNode: cc.Node = null;//卸下按钮节点
    /**
     * 装备强化回调
     */
    public qhCallback: (goodItem: GoodItem) => void;
    /**
     * 卸下
     */
    public unCallback: (goodItem: GoodItem) => void;

    public goodItem: GoodItem;
    public inGuide = false;//是否引导中
    public guideStep: number = 0;    //指引步骤
    public showQH: boolean = true;

    public onLoad() {
        this.node.zIndex = 1
    }
    /**
     * 
     * @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(goodItem: GoodItem, showQH: boolean = true) {
        this.showQH = showQH;
        if (goodItem.equip) {
            this.mEquipNode.active = true;
            this.mGoodNode.active = false;
            this.initEquip(goodItem)
        } else {
            this.mEquipNode.active = false;
            this.mGoodNode.active = true;
            this.initGood(goodItem)
        }
        if (this.inGuide) {
            if (this.guideStep == 1) {
                this.onGuide_1();
            }
        }
    }
    public initGood(goodItem: GoodItem) {
        this.mGoodItem.initGood(this.main, goodItem.data)
        this.mName.string = i18n.t(goodItem.good.name);
        this.mAbout.string = i18n.t(goodItem.good.about)

        this.mUseNode.active = goodItem.good.type == 3

    }
    public initEquip(goodItem: GoodItem) {
        this.goodItem = goodItem;
        let equip = this.goodItem.equip;

        this.mGoodItem.initEquip(this.main, equip)

        let equipData = this.goodItem.equipData;
        this.mName.string = i18n.t(equipData.name);
        this.mAbout.string = i18n.t(equipData.about);

        this.goodItem.equip = equip;
        this.goodItem.equipData = equipData;
        this.goodItem.flushEquip(this.main);

        this.mQHNode.active = equipData.type <= 4 && this.showQH;

        let fAttr = FFCalAttr.getEquipAttr(this.main, equip, equipData)

        this.mZdl.string = '' + fAttr.zdl


        if (equipData.atk > 0) {
            this.mAtk.string = '' + fAttr.atk;
            this.mMainNode[0].active = true;
        } else {
            this.mMainNode[0].active = false;
        }
        if (equipData.def > 0) {
            this.mDef.string = '' + fAttr.def;
            this.mMainNode[1].active = true;
        } else {
            this.mMainNode[1].active = false;
        }
        if (equipData.hp > 0) {
            this.mHp.string = '' + fAttr.hp;
            this.mMainNode[2].active = true;
        } else {
            this.mMainNode[2].active = false;
        }
        if (equipData.sp > 0) {
            this.mSp.string = '' + fAttr.sp;
            this.mMainNode[3].active = true;
        } else {
            this.mMainNode[3].active = false;
        }

        if (equipData.atk <= 0 && equipData.def <= 0 && equipData.hp <= 0 && equipData.sp <= 0) {
            this.baseAttNode.active = false;
        }


        if (equip.attr.length <= 0) {
            this.mFAttrNode.active = false;
            this.addAttNode.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 < 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;
        }

        if (equipData.about != "") {
            this.lbdesc.string = i18n.t(equipData.about);
        } else {
            this.descNode.active = false;
        }
    }
    public setQHCallback(qhCallback: (goodItem: GoodItem) => void) {
        this.qhCallback = qhCallback
    }
    /**
     * 点击强化按钮
     */
    public onclickQH() {
        if (this.qhCallback) {
            this.qhCallback(this.goodItem)
        }
        if (this.guideCallback) {
            this.guideCallback();
        }
    }
    public setUnCallback(unCallback: (goodItem: GoodItem) => void) {
        this.unCallback = unCallback
    }
    /**
     * 点击强化按钮
     */
    public onclickUn() {
        if (this.unCallback) {
            this.unCallback(this.goodItem)
        }
    }

    /**
         * 引导中的点击回调
         */
    private guideCallback: () => void

    public setGuideCallback(guideCallback: () => void) {
        this.guideCallback = guideCallback
    }
    /**
    * 开始引导强化装备
    */
    private onGuide_1() {
        let guideMask = this.main.mGuideMask;
        let targetNode = this.mQHNode;
        guideMask.setTargetNode(targetNode);
        guideMask.show();
        this.setGuideCallback(() => {
            guideMask.close()
        })
    }
}