
import { AudioMgr, GameViewType } from "../../../main/ViewManage";
import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import GoodItem from "../../common/GoodItem";
import PetIcon from "../../common/PetIcon";
import Equip from "./Equip";
import EquipInfo from "./EquipInfo";
import EquipQH from "./EquipQH";

const { ccclass, property } = cc._decorator;
/**
 * 换装备界面
 */
@ccclass
export default class ChangeEquip extends ViewObject {

    @property(EquipInfo)
    public p1: EquipInfo = null;//左边

    @property(EquipInfo)
    public p2: EquipInfo = null;//右边

    @property(cc.Node)
    leftQHNode: cc.Node = null; // 左边强化按钮

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

    @property(cc.Node)
    mUseNode: cc.Node = null;//装备按钮
    /**
     * 当前操作的伙伴
     */
    public petIcon: PetIcon;
    /**
     * 当前选择的装备
     */
    public goodItem: GoodItem;
    /**
    * 装备上的装备
    */
    public goodItem1: GoodItem;

    public equip: Equip;

    public inGuide = false
    public guideStep: number = 0;
    /**
     * @param pi 穿装备的伙伴
     * @param equipItem 选择的装备
     */
    public init(pi: PetIcon, goodItem: GoodItem, goodItem1: GoodItem) {
        this.petIcon = pi;
        this.goodItem = goodItem;
        this.goodItem1 = goodItem1
        this.p2.init(this.main, this.goodItem1, null)
        this.p1.init(this.main, this.goodItem, this.goodItem1);

        this.mQHNode.active = goodItem1.equipData && goodItem1.equipData.type <= 4;
        this.leftQHNode.active = goodItem.equipData.type <= 4;

        this.node.zIndex = 1

        if (this.inGuide) {
            if (this.guideStep == 0) {
                this.onGuide()
            }
        }

    }

    /**
     * 
     * @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 onclickUse() {
        let msg = {
            petId: this.petIcon.id,
            equipIndex: this.p1.equipItem.equip.__index,
        }
        this.main.gameHttp.sendJson('equip/v1/use', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    this.main.playerEffectByPath(AudioMgr.equip);
                    this.equip.checkPet(this.petIcon)
                    this.equip.mEquipPack.flushEquip();
                    this.exitDistroy()
                    if (this.inGuide) {
                        this.equip.onGuideExit()
                    }
                    this.main.showTips('装备成功');
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
        if (this.guideCallback) {
            this.guideCallback()
        }
    }

    public onclickLeftQH() {
        this.main.viewManage.loadFunc(GameViewType.IntensifyEquip, (viewObject: ViewObject) => {
            let equipQH: EquipQH = viewObject as EquipQH;
            equipQH.init(this.goodItem.equip, this.petIcon, this.equip)
            viewObject.show()
            this.exitDistroy()
        });
    }

    public onclickQH() {
        this.main.viewManage.loadFunc(GameViewType.IntensifyEquip, (viewObject: ViewObject) => {
            let equipQH: EquipQH = viewObject as EquipQH;
            equipQH.init(this.goodItem1.equip, this.petIcon, this.equip)
            viewObject.show()
            this.exitDistroy()
        });
    }


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

    public setGuideCallback(guideCallback: () => void) {
        this.guideCallback = guideCallback
    }
    /**
    * 开始引导穿装备
    */
    private onGuide() {
        let guideMask = this.main.mGuideMask
        let targetNode = this.mUseNode
        guideMask.setTargetNode(targetNode)
        guideMask.show()
        this.setGuideCallback(() => {
            guideMask.close()
        })
    }
}
