import { Component, Label, ProgressBar, _decorator } from 'cc'

const { ccclass, property } = _decorator

@ccclass('Progress')
export default class Progress extends Component {
  @property(Label)
  public percentLable: Label

  @property(ProgressBar)
  public bar: ProgressBar

  onLoad() {
    this.bar.node.active = false
    this.bar.progress = 0
  }

  start() {}

  updatePercent(current, filePercent) {
    // this.percentLable.string =  filePercent.toFixed(2);
  }

  updatefileTotal(current: number, filePercent: number) {
    if (!this.bar.node.active) this.bar.node.active = true
    let nowPercent = Math.round((current / filePercent) * 100)
    const curMB = this.getMB(current)
    const totalMB = this.getMB(filePercent)
    // this.percentLable.string = "正在更新 " + nowPercent + "%" + " ( " + curMB + " / "+totalMB +" MB)";
    nowPercent = Math.min(nowPercent, 100)
    this.percentLable.string = `正在更新 ${nowPercent}%`
    this.bar.progress = current / filePercent
  }

  public updateProgress(
    current: number,
    total: number,
    msg: string = '正在加载资源，此过程不消耗流量...',
  ) {
    this.bar.node.active = true
    // this.setMsg(msg+ current + "/" + total);
    this.setMsg(msg)
    this.bar.progress = current / total
  }

  getMB(bytes: number) {
    bytes /= 1024
    bytes /= 1024
    return bytes.toFixed(2)
  }

  public setMsg(msg: string = '游戏加载中，请稍后...') {
    this.percentLable.string = msg
  }
}
