import { _decorator, Button, Component, Label, Node, ProgressBar } from 'cc';
import { ModulerBase } from '../../GameFrameWork/ModulerBase';
import { GameInfo } from '../../../GameInfo';
import { UIMgr } from '../../../Frames/UIManager';
import { UIType } from '../../GameFrameWork/UIBase';
const { ccclass, property } = _decorator;

@ccclass('Top')
export class Top extends ModulerBase {
    //我方防御塔血条
    private _myHp: Label = null;
    private _myCurHp: number = null;
    private _myTotalHp: number = null;
    private _myProgressBar: ProgressBar = null;

    //敌方防御塔血条
    private _enemyHp: Label = null;
    private _enemyCurHp: number = null;
    //敌人防御塔总血量通过数据表获取
    enemyTotalHp: number = null;
    private _enemyProgressBar: ProgressBar = null;

    //pause按钮
    private _btnPuase: Node = null;

    protected onLoad(): void {
        this._myHp = this.node.getChildByPath("BloodLeft/Label").getComponent(Label);
        this._myTotalHp = GameInfo.Instance.getMyTowerHp();
        this._myProgressBar = this.node.getChildByName("BloodLeft").getComponent(ProgressBar);

        this._enemyHp = this.node.getChildByPath("BloodRight/Label").getComponent(Label);
        this._enemyProgressBar = this.node.getChildByName("BloodRight").getComponent(ProgressBar);
        
        this._btnPuase = this.node.getChildByName("Button");
    }

    init() {
        this._myCurHp = this._myTotalHp;
        this._myHp.string = `${this._myCurHp}/${this._myTotalHp}`;
        this._myProgressBar.progress = this._myCurHp/this._myTotalHp;

        this._enemyCurHp = this.enemyTotalHp;
        this._enemyHp.string = `${this._enemyCurHp}/${this.enemyTotalHp}`;
        this._enemyProgressBar.progress = this._enemyCurHp/this.enemyTotalHp;

        this._btnPuase.getComponent(Button).node.on(Button.EventType.CLICK,this._onBtnPause,this)
    }

    updataMyHp(hp: number){
        this._myCurHp = hp;
        this._myHp.string = `${this._myCurHp}/${this._myTotalHp}`;
        this._myProgressBar.progress = this._myCurHp/this._myTotalHp;
    }

    updataEnemyHp(hp: number){
        this._enemyCurHp = hp;
        this._enemyHp.string = `${this._enemyCurHp}/${this.enemyTotalHp}`;
        this._enemyProgressBar.progress = this._enemyCurHp/this.enemyTotalHp;
    }

    private _onBtnPause(){
        UIMgr.openUI("Pause",UIType.PAGE);
    }
}


