import { GameViewType } from "../../../main/ViewManage";
import ViewObject from "../../../main/ViewObject";
import RedPoint from "../../data/RedPoint";
import Equip from "../../home/equip/Equip";
import SetLeader from "../../home/equip/SetLeader";
import FPanel from "./FPanel";

/**
 * 头像
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FPanelIcon extends FPanel {

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

    @property(cc.Node)
    mIconX: cc.Node = null;

    @property(cc.Node)
    mLeader: cc.Node = null;//队长标识

    public inGuide = false//当前是否处于引导中
    public guideStep: number = 0;
    /**
     * 引导中的点击回调
     */
    private guideCallback: () => void

    onLoad() {
        this.mIconX.active = false;
    }

    public setGuideCallback(guideCallback: () => void) {
        this.guideCallback = guideCallback
    }

    /**
     * 更新当前面板
     */
    public updatePanel() {
        if (this.sprite) {
            this.mHPBar.progress = this.sprite.hp / this.sprite.attrData.hp;
            if (this.mLabel) {
                this.mLabel.string = this.sprite.hp + '/' + this.sprite.attrData.hp;
            }
            if (this.sprite.hp <= 0) {
                this.mIconX.active = true;
            } else {
                this.mIconX.active = false;
            }
        }
    }
    public setCancel() {

    }
    public setClose() {
    }
    /**
     * 设置头像
     */
    public setIcon() {
        let ffAttr = this.sprite.attrData

        let headIcon = ffAttr.skin
        if (headIcon == '30000') {
            headIcon = '' + ffAttr.id
        }

        cc.resources.load('icon/role_head/' + headIcon, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
            if (err) {
                cc.error(err);
            } else {
                this.mIcon.spriteFrame = spriteFrame;
            }
        });
        let main = this.sprite.ff.main
        let player = main.player
        if (this.sprite.attrData.id == player.role.leader) {
            this.mLeader.active = true;
        } else {
            this.mLeader.active = false;
        }
    }
    public onclick() {
        let res = this.sprite.map.ff.fres
        let nodes = res.mHudNode.children
        for (let i = 0; i < nodes.length; i++) {
            const element = nodes[i];
            RedPoint.removeRed(element)
        }

        let main = this.sprite.map.ff.main
        this.sprite.ff.pauseSprite(true)
        main.viewManage.loadFunc(GameViewType.equip, (viewObject: ViewObject) => {
            viewObject.show();
            let topNode = this.sprite.ff.main.topNode;
            topNode.active = true;
            let equip: Equip = viewObject as Equip
            equip.inGuide = this.inGuide
            equip.guideStep = this.guideStep;
            this.inGuide = false
            equip.openId = this.sprite.attrData.id
            let setLeader = equip.node.getComponent(SetLeader)
            setLeader.mSetButton.active = false
            equip.setCloseCallback(() => {
                if (this.sprite.isValid && this.sprite.ff.isValid) {
                    this.sprite.ff.pauseSprite(false)
                    this.sprite.map.ff.updateRole()
                }
                topNode.active = false;
            })
        });
        if (this.guideCallback) {
            this.guideCallback()
        }
    }

}
