import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { ModulerBase } from '../../GameFrameWork/ModulerBase';
import { GameInfo } from '../../../GameInfo';
import { resMgr } from '../../../Frames/ResourcesMgr';
import { messageMgr } from '../../../Frames/MessageMgr';
import { RoleData } from '../../../DataItem/ItemData';
import { dataMgr } from '../../../Frames/DataManager';
const { ccclass, property } = _decorator;

@ccclass('Bottom')
export class Bottom extends ModulerBase {
    private _oreSpeed: Node = null;
    private _characterSlot: Node = null;

    // 每多少秒生成多少个矿石
    private _speed: number = 0;
    // 初始矿石数量
    oreCount: number = 10;
    // 记录时间间隔
    private _elapsedTime: number = 0;
    private _roleDts: RoleData[] = null;
    protected onLoad(): void {
        this._roleDts = dataMgr.getAllDataByName("RoleCardData");
        this._oreSpeed = this.node.getChildByPath("OreSpeed/_oreSpeed");
        this._characterSlot = this.node.getChildByName("CharacterSlot");
    }

    onStart() {
        this.getLabel("_lvNumber").string = String(GameInfo.Instance.getCurlv());
        this.getLabel("_oreGrade").string = `等级：${GameInfo.Instance.getOreGrade()}`;
        this.getLabel("_ownNumber").string = String(GameInfo.Instance.getOwnDiamondNum());
        this.getLabel("_needNumber").string = String(GameInfo.Instance.getNeedDiamondNum());
        this.getSprite("_skillImg").spriteFrame = resMgr.getSpriteFrame(GameInfo.Instance.getSkill());
        this._updateLabel();
        this._speed = GameInfo.Instance.getOreSpeed();
        this._setRoleImg();
        //注册消息
        messageMgr.addEvent("addOreCount", this.onEnemyDeath, this)
    }

    protected update(dt: number): void {
        //累加时间
        this._elapsedTime += dt;
        //每秒更新
        if (this._elapsedTime >= 1) {
            //增加矿石数量
            this.oreCount += this._speed / 60;
            //向下取整
            this.oreCount = Math.ceil(this.oreCount);
            this._updateLabel();
            //重置时间间隔
            this._elapsedTime = 0;
        }
    }

    private _setRoleImg() {
        for (let i = 0; i < this._characterSlot.children.length; i++) {
            const role: Node = this._characterSlot.children[i];
            const sprite: Node = role.getChildByName("Sprite");
            const imgName: string = GameInfo.Instance.getRoleImgNames()[i];
            sprite.getComponent(Sprite).spriteFrame = resMgr.getSpriteFrame(imgName);
            for (const roleDt of this._roleDts) {
                if (roleDt.imgName === imgName) {
                    const count: Node = role.getChildByName("Count");
                    if (count.active) {
                        count.getComponent(Label).string = String(roleDt.consume);
                    } else {
                        count.active = true;
                        count.getComponent(Label).string = String(roleDt.consume);
                    }
                }
            }
        }
    }

    onEnemyDeath() {
        this.oreCount += 3;
        this._updateLabel();
    }
    onReduce(count: number) {
        if (this.oreCount <= 0) {
            this.oreCount = 0;
        } else {
            this.oreCount -= count;
        }
        this._updateLabel();
    }
    private _updateLabel() {
        if (this._oreSpeed) {
            this.getLabel("_oreSpeed").string = String(this.oreCount);
        }
    }
}


