import { _decorator, Color, Component, Label, Node, ProgressBar, Tween, tween } from 'cc';
import { ui_base } from './ui_base';
import { boss_info_data } from '../../../data/data';
import { tools } from '../../tools';
import { gameManager } from '../gameManager';
import { ClientEvent } from '../../clientEvent';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('ui_boss_info')
export class ui_boss_info extends ui_base {
    @property(Node) bg:Node = null;
    @property(Node) bg_bar:Node = null;
    @property(Node) bar:Node = null;
    @property(Node) head:Node = null;
    @property(Node) hp_ani:Node = null;
    private mBossData:boss_info_data = null;
    private bossHp:number = 0;
    protected init(): void {
        this.mBossData = this.mTopData._boss_info_data;
        if(this.mBossData===null){
            return tools.showToast("boss 配置错误！")
        }
        this.loadBg(this.mBossData.bg)
        gameManager.initUiBaseAtt(this.bg_bar,this.mBossData.bg_bar)
        gameManager.initUiBaseAtt(this.bar,this.mBossData.bar)
        gameManager.initUiBaseAtt(this.head,this.mBossData.head)
        this.bossHp = this.mBossData.hp;
        this.bg_bar.getComponent(ProgressBar).progress = 1;
        ClientEvent.on(config.EventRun.ON_BOSS_HURT,this.hurt.bind(this),this)
    }

    protected onDisable(): void {
        ClientEvent.off(config.EventRun.ON_BOSS_HURT,this.hurt.bind(this),this)
    }

    hurt(hurt_num:number){
        if(hurt_num===0){
            tools.showToast("道具用完失败")
            this.onFialEvent()
            return;
        }
        this.bossHp -= hurt_num;
        Tween.stopAllByTarget(this.hp_ani)
        this.hp_ani.getComponent(Label).string = `${hurt_num>0?"-":"+"}${Math.abs(hurt_num)}`
        this.hp_ani.active = true;
        this.hp_ani.getComponent(Label).color = hurt_num>0?Color.RED:Color.GREEN;
        tween(this.hp_ani).delay(2).call(()=>{
            this.hp_ani.active = false;
        }).start()
        this.bg_bar.getComponent(ProgressBar).progress = this.bossHp/this.mBossData.hp;
        if(this.bossHp<=0){
            tools.showToast("成功打死boss")
            this.onFinishEvent()
            ClientEvent.off(config.EventRun.ON_BOSS_HURT,this.hurt.bind(this),this)
        }
    }

}


