import { gameMethod } from "../../common/gameMethod";
import UEBase from "../../frameWork/compment/UEBase";
import { eg } from "../../frameWork/eg";
import GameMath from "../../utils/GameMath";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UESubHp extends UEBase {
    @property(cc.Node) buffTxt: cc.Node = null
    @property(cc.Node) txtHP: cc.Node = null
    @property(cc.Font) baojiFont: cc.Font = null
    @property(cc.Font) lianjiFont: cc.Font = null;
    @property(cc.Font) pugongFont: cc.Font = null;
    @property(cc.Font) huifuFont: cc.Font = null;
    @property(cc.Font) fanjiFont: cc.Font = null;
    @property(cc.Font) jiansheFnt: cc.Font = null;
    @property(cc.Font) gedangFnt: cc.Font = null;
    @property(cc.Font) jinengFnt: cc.Font = null;
    @property(cc.Font) cxshFnt: cc.Font = null;
    @property(cc.SpriteAtlas) buffTxtAtlas: cc.SpriteAtlas = null;

    static readonly BundleKey: string = "battle";
    static readonly PrefabUrl: string = "UESubHp";
    static readonly CLS: string = "UESubHp";

    show(data: [string, number], hpType?: string) {
        let type = data[0];
        if (hpType) {
            if (hpType == 'skill') {
                type = 'jineng';
            } else if (hpType == 'cxsh') {
                type = 'cxsh';
            }
        }
        let effectData = this.getEffect(type, data[1]);
        if (effectData) {
            this.ShowHP(data[0], data[1], effectData.effectDesc);
        } else {
            console.log(`${type} is not exist`);
            this.unuse();
        }
    }


    PauseAction() {
        this.txtHP.pauseAllActions();
    }

    ResumAction() {
        this.txtHP.resumeAllActions();
    }

    ShowBuffTxt(buffType: string) {
        cc.Tween.stopAllByTarget(this.buffTxt)
        this.buffTxt.active = true;
        this.txtHP.active = false;
        this.buffTxt.scale = 1.4;
        this.node.zIndex = 1500;
        this.buffTxt.setPosition(0, 50)
        this.buffTxt.opacity = 255;
        this.buffTxt.getComponent(cc.Sprite).spriteFrame = this.buffTxtAtlas.getSpriteFrame(buffType);
        cc.tween(this.buffTxt)
            .by(0.8, { position: cc.v3(0, 40, 0) })
            .by(0.1, { scale: -1.2, opacity: -255, position: cc.v3(0, -10, 0) })
            .call(() => {
                this.unuse();
            })
            .start();
    }

    ShowHP(type: string, val: number, effectDesc: string) {
        this.buffTxt.active = false;
        this.txtHP.active = true;
        if (gameMethod.isEmpty(this.txtHP)) {
            this.txtHP = this.node.getChildByName("txtHP")
        }
        cc.Tween.stopAllByTarget(this.txtHP)
        this.txtHP.getComponent(cc.Label).string = effectDesc;
        this.txtHP.scale = 1;
        let upTime = 0.35;
        let upLen = 0;
        // if (type == 'baoji') {
        //     this.txtHP.setPosition(0, 25);
        //     upTime = 0.55;
        //     upLen = 80;
        // } else {
        //     this.txtHP.setPosition(0, 30);
        //     upTime = 0.45;
        //     upLen = 60;
        // }
        if (type == 'baoji' || type == 'lianji'
            || type == 'jianshe' || type == 'fanji' || type == 'canbeatback' || type == 'cxsh'
            || type == 'block') {
            if (type == 'baoji') {
                this.txtHP.getComponent(cc.Label).fontSize = 40;
            } else {
                this.txtHP.getComponent(cc.Label).fontSize = 36;
            }
            if (type == 'cxsh') {
                this.txtHP.setPosition(0, 30);
                upTime = 0.45;
                upLen = 60;
            } else {
                this.txtHP.setPosition(0, 25);
                upTime = 0.55;
                upLen = 80;
            }
            this.node.zIndex = 1000;

        } else {
            // if (val > 0) {
            //     //回血
            // } else {

            // }
            this.txtHP.getComponent(cc.Label).fontSize = 32;
            this.node.zIndex = 500;

            this.txtHP.setPosition(0, 30);
            upTime = 0.45;
            upLen = 60;
        }
        this.txtHP.opacity = 255;

        cc.tween(this.txtHP)
            .by(upTime, { position: cc.v3(0, upLen, 0) })
            .by(0.1, { opacity: -255, position: cc.v3(0, 0, 0) })
            .call(() => {
                this.unuse();
            })
            .start();
    }

    // put() 收回对象池时会调用
    unuse() {
        cc.Tween.stopAllByTarget(this.txtHP);
        cc.Tween.stopAllByTarget(this.buffTxt);
        this.txtHP.getComponent(cc.Label).string = "";
        this.txtHP.y = 0;
        this.txtHP.x = 0;
        this.txtHP.scale = 1;
        this.txtHP.opacity = 255;
        eg.poolManager.GetPool("subHPPool").put(this.node);
    }

    // 飘字效果描述
    getEffect(type: string, val: number) {
        let effectDesc: string;
        this.txtHP.getComponent(cc.Label).fontSize = 26;
        let divide = "";
        if (val < 0) {
            divide = "-";
        }
        let value = val || '';
        
        switch (type) {
            case "hp":
                //普通扣血
                effectDesc = divide ? `${this.GetVal(Number(value))}` : `h+${this.GetVal(Number(value))}`;
                this.txtHP.getComponent(cc.Label).font = divide ? this.pugongFont : this.huifuFont;
                break;
            case "xixue":
            case "huifu":
                //吸血
                effectDesc = `h+${this.GetVal(Number(value))}`;
                this.txtHP.getComponent(cc.Label).font = this.huifuFont;
                break;
            case "cxsh":
                //持续伤害
                effectDesc = divide ? `c${this.GetVal(Number(value))}` : `h+${this.GetVal(Number(value))}`;
                this.txtHP.getComponent(cc.Label).font = divide ? this.cxshFnt : this.huifuFont;
                break;
            case "jineng":
                //普通扣血
                effectDesc = divide ? `${this.GetVal(Number(value))}` : `+${this.GetVal(Number(value))}`;
                this.txtHP.getComponent(cc.Label).font = divide ? this.jinengFnt : this.huifuFont;
                break;
            case "block":
                //格挡
                effectDesc = `g${this.GetVal(Number(value))}`;
                this.txtHP.getComponent(cc.Label).font = this.gedangFnt;
                break;
            case "baoji":
                //暴扣扣血
                effectDesc = `b${this.GetVal(Number(value))}`
                this.txtHP.getComponent(cc.Label).font = this.baojiFont;
                break;
            case "fanji"://反击
            case "canbeatback": // 技能反击
                // if (gameMethod.isEmpty(value)) {
                //     effectDesc = `f`
                // } else {
                //     effectDesc = `${this.GetVal(Number(value))}`
                // }
                effectDesc = `f${this.GetVal(Number(value))}`
                this.txtHP.getComponent(cc.Label).font = this.fanjiFont;
                break;
            case "lianji":
                //连击
                effectDesc = `l${this.GetVal(Number(value))}`
                this.txtHP.getComponent(cc.Label).font = this.lianjiFont;
                break;
            case "jianshe":
                //溅射
                effectDesc = `j${this.GetVal(Number(value))}`
                this.txtHP.getComponent(cc.Label).font = this.jiansheFnt;
                break;
            case "shanbi":
                //闪避
                effectDesc = "闪避"
                this.txtHP.getComponent(cc.Label).font = null;
                break;
            default:

                break;
        }
        if (effectDesc) {
            return { effectDesc: effectDesc }
        } else {
            return null;
        }
    }

    GetVal(value) {
        let divide = "";
        if (value < 0) {
            divide = "-";
        }
        return divide + GameMath.showNum(Math.abs(value));
    }
}
