import i18n from "../../../i18n/i18n";
import { GameViewType } from "../../../main/ViewManage";
import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import GoodAbout from "../../common/GoodAbout";
import GoodItem from "../../common/GoodItem";
import PetIcon from "../../common/PetIcon";
import FFCalAttr, { FFAttr } from "../../data/FFCalAttr";
import RedPoint from "../../data/RedPoint";
import ChangeEquip from "./ChangeEquip";
import EquipItem from "./EquipItem";
import EquipPack from "./EquipPack";
import EquipQH from "./EquipQH";
import PetInfo from "./PetInfo";

/**
 * 宠物装备查看界面
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class Equip extends ViewObject {

    @property(cc.Node)
    mPetList: cc.Node = null;//宠物列表父节点

    @property(cc.Prefab)
    mPetPrefab: cc.Prefab = null;//宠物头像

    @property(cc.Node)
    mModel: cc.Node = null;//宠物模型显示的位置

    @property([EquipItem])
    mEquips: Array<EquipItem> = [];//6件装备

    @property(cc.Prefab)
    mEquipPrefab: cc.Prefab = 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)
    mPagSize: cc.Label = null;//背包容量

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

    @property(EquipPack)
    mEquipPack: EquipPack = null;//装备背包

    @property(cc.Node)
    mBtExit: cc.Node = null;//退出按钮

    public curPi: PetIcon;
    public curPage: number = 1;

    public inGuide = false;//是否引导中
    public guideStep: number = 0;    //指引步骤
    /**
     * 打开伙伴的id
     * -1 打开默认的
     */
    public openId = -1;

    public httpEvent(reveData: ReveData) {
        let data = reveData.data;
        if (data._equip != undefined) {
            this.mEquipPack.setEquipType(this.curPage);
        }
    }

    public onLoad() {
        let player = this.main.player;
        let petMap = player.pet;

        let zdl = 0
        for (const id in petMap) {
            let pet = petMap[id];
            let node = cc.instantiate(this.mPetPrefab);
            let petIcon = node.getComponent(PetIcon);
            petIcon.id = parseInt(id);
            petIcon.data = pet;
            let ffAttr = FFCalAttr.getAttr(this.main, petIcon.data);
            petIcon.flushIcon(ffAttr)
            node.parent = this.mPetList;

            petIcon.setCallback((pi: PetIcon) => {
                this.checkPet(pi);
            });
            zdl += ffAttr.zdl
        }

        let msg = {
            zdl: zdl,
        }
        this.main.gameHttp.sendJson('role/v1/zdl', msg, (state, reve: ReveData) => {
        });

    }
    public setCloseCallback(callback: () => void) {
        this.closeCallback = callback
    }
    public start() {
        let nodes = this.mPetList.children;
        for (let i = 0; i < nodes.length; i++) {
            const element = nodes[i];
            let petIcon = element.getComponent(PetIcon);
            if (this.openId == -1) {
                this.openId = petIcon.data.id
            }
            if (petIcon.data.id == this.openId) {
                petIcon.onclick();
            } else {
                if (RedPoint.petRedPoint(this.main, petIcon.data)) {
                    RedPoint.addRed(this.main, element)
                } else {
                    RedPoint.removeRed(element)
                }
            }
        }

        if (this.inGuide) {
            if (this.guideStep == 0) {
                this.onGuide()
            } else if (this.guideStep == 1) {
                this.onGuide_1();
            }
        }
    }
    public flushPagSize() {
        let equipPag = this.main.player.equip;
        this.mPagSize.string = i18n.t('背包容量') + ':' + equipPag.length + '/500';
    }

    public checkPet(pi: PetIcon) {
        this.flushPagSize()
        this.curPi = pi;
        let sManage = this.main.sManage;
        let pet = sManage.getRoleById(pi.id);
        this.mModel.destroyAllChildren();
        let ffAttr = FFCalAttr.getAttr(this.main, pi.data);
        cc.resources.load('prefab/role/' + pet.act, cc.Prefab, (err, prefab: cc.Prefab) => {
            if (err) {
                cc.error(err);
            } else {
                let node = cc.instantiate(prefab);
                node.parent = this.mModel;
                let spriteNode = node.getChildByName('juese01');
                let spineNode0 = spriteNode.getChildByName('spineRight');
                let spine = spineNode0.getComponent(sp.Skeleton);
                let mWeapon1: sp.spine.Bone = spine.findBone('control');
                if (mWeapon1) {
                    mWeapon1.data.rotation = 0;
                    mWeapon1.rotation = 0;
                }
                let smoke = spriteNode.children[0]
                smoke.active = false
                // spine.setSkin(ffAttr.skin)
                // Equip.chaneWeapon(spine, ffAttr.weapon)
            }
        });
        for (let i = 0; i < this.mEquips.length; i++) {
            const element = this.mEquips[i];
            element.setNull();
            element.setCallback(() => {
                this.mEquipPack.setEquipType(i + 1);
            })
        }
        this.flushEquip(pi);
        this.flushAttr(ffAttr)
        pi.flushIcon(ffAttr)
    }

    /**
     * 切换武器
     * @param spine 
     * @param attrName 
     */
    public static chaneWeapon(skeleton, attrName: string) {
        return
        let slotName1 = '1';
        let slotName2 = '2';
        let targetSkinName = 'default';
        let targetAttaName1 = attrName + '/1';
        let targetAttaName2 = attrName + '/2';

        const skeletonData = skeleton.skeletonData.getRuntimeData();
        const skin = skeletonData.findSkin(targetSkinName);

        let slot1: sp.spine.Slot = skeleton.findSlot(slotName1);
        let slot2: sp.spine.Slot = skeleton.findSlot(slotName2);

        const slotIndex1 = skeletonData.findSlotIndex(slotName1);
        const atta1 = skin.getAttachment(slotIndex1, targetAttaName1);

        const slotIndex2 = skeletonData.findSlotIndex(slotName2);
        const atta2 = skin.getAttachment(slotIndex2, targetAttaName2);
        if (slot1) {
            slot1.setAttachment(atta1);
        }
        if (slot2) {
            slot2.setAttachment(atta2);
        }
    }

    public flushEquip(pi: PetIcon) {
        let equips = pi.data.equips;
        for (let i = 0; i < equips.length; i++) {
            const element = equips[i];
            let equipData = this.main.sManage.getEquipById(element.id);
            let siteIndex = equipData.type - 1
            let mEquip = this.mEquips[siteIndex];
            mEquip.initEquip(this.main, element);

            mEquip.setCallback((pp: GoodItem) => {
                this.openGoodAbout(pp);
            });
        }
        this.mEquipPack.init(pi);
    }
    public flushAttr(ffAttr: FFAttr) {
        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;

    }
    /**
     * 打开详细说明
     */
    public openInfo() {
        this.main.viewManage.loadFunc(GameViewType.petInfo, (viewObject: ViewObject) => {
            let petInfo = viewObject as PetInfo;
            petInfo.init(this.curPi)
            viewObject.show();
        });
    }

    /**
     * 打开换装界面
     * @param equipItem 
     */
    public openChangeEquip(equipItem: GoodItem) {
        this.main.viewManage.loadFunc(GameViewType.changeEquip, (viewObject: ViewObject) => {
            let changeEquip = viewObject as ChangeEquip;
            changeEquip.equip = this
            let type = equipItem.equipData.type
            changeEquip.inGuide = this.inGuide
            changeEquip.guideStep = this.guideStep;
            this.inGuide = false
            changeEquip.init(this.curPi, equipItem, this.mEquips[type - 1]);
            viewObject.show();
        });
    }
    /**
     * 查看物品
     * @param goodItem 
     */
    private openGoodAbout(goodItem: GoodItem) {
        this.main.viewManage.loadFunc(GameViewType.goodAbout, (viewObject: ViewObject) => {
            let goodAbout = viewObject as GoodAbout;
            goodAbout.inGuide = this.inGuide;
            goodAbout.guideStep = this.guideStep;
            goodAbout.init(goodItem)
            viewObject.show();

            goodAbout.setQHCallback((gdItem: GoodItem) => {
                goodAbout.exitDistroy()
                this.openIntensifyEquip(gdItem)
            })

            if (goodItem.equipData.type == 5) {//服装可以卸下
                goodAbout.mUnInstallNode.active = true
                goodAbout.setUnCallback((gdItem: GoodItem) => {
                    goodAbout.exitDistroy()
                    this.uninstallEquip(gdItem)
                })
            }
        });
    }
    /**
     * 装备强化
     */
    public openIntensifyEquip(goodItem: GoodItem) {
        this.main.viewManage.loadFunc(GameViewType.IntensifyEquip, (viewObject: ViewObject) => {
            let equipQH: EquipQH = viewObject as EquipQH;
            equipQH.inGuide = this.inGuide;
            equipQH.guideStep = this.guideStep;
            equipQH.init(goodItem.equip, this.curPi, this)
            viewObject.show();
            if (this.inGuide) {
                if (this.guideStep == 1) {
                    equipQH.setCloseCallback(() => {
                        this.onGuideExit();
                    })
                }
            }
        });
    }
    private uninstallEquip(goodItem: GoodItem) {
        let msg = {
            petId: this.curPi.id,
            equipId: goodItem.equip.id,
        }
        this.main.gameHttp.sendJson('equip/v1/uninstall', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    this.checkPet(this.curPi)
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }



    /**
    * 开始引导穿装备
    */
    private onGuide() {
        let guideMask = this.main.mGuideMask
        let targetNode = this.mEquipPack.mPagContent.children[0]
        guideMask.setTargetNode(targetNode)
        guideMask.show()
        let equipItem = targetNode.getComponent(GoodItem)
        equipItem.inGuide = true
        equipItem.setGuideCallback(() => {
            guideMask.close()
        })
    }

    /**
     * 强化指引
     */

    private onGuide_1() {
        let guideMask = this.main.mGuideMask;
        let targetNode = this.mEquips[0].node;
        guideMask.setTargetNode(targetNode)
        guideMask.show()
        let equipItem = targetNode.getComponent(GoodItem)
        equipItem.inGuide = true
        equipItem.setGuideCallback(() => {
            guideMask.close()
        })
    }


    public onGuideExit() {
        let guideMask = this.main.mGuideMask
        let targetNode = this.mBtExit
        guideMask.setTargetNode(targetNode)
        guideMask.show()

        let closeCallback = this.closeCallback

        this.setCloseCallback(() => {
            guideMask.close()
            if (closeCallback) {
                closeCallback()
            }
        })

    }

}
