import { world } from '@minecraft/server'


function replaceAtSymbols(input) {
  return input.replace(/@+/g, (match) => {
      if (match.length === 1) {
          return match; // Keep single '@' unchanged
      }
      return `@x${match.length}`;
  });
}


/**
 * 
 * @param {string} inputString 
 * @param {number} targetLength 
 * @returns 
 */
export function convertValueToUi(inputString, targetLength) {
  const currentLength = inputString.length;

  if (currentLength >= targetLength) {
    return inputString;
  }

  const numberOfZerosToAdd = targetLength - currentLength;
  const zeros = '0'.repeat(numberOfZerosToAdd);

  return zeros + inputString;
}

/**
 * 
 * @param {string} inputString 
 * @param {number} targetLength 
 * @returns 
 */
export function convertTextToUi(inputString, targetLength) {
  const currentLength = inputString.length;

  if (currentLength >= targetLength) {
    return inputString;
  }

  const numberOfEmptiesToAdd = targetLength - currentLength;
  const empties = "@".repeat(numberOfEmptiesToAdd);

  return inputString + empties;
}


export class UiManager {
  /**
   * 
   * @param {Player} player 
   */
  constructor(player) {
    this.player = player;
    this.initialize()
  }

  initialize() {
    this.hudDisplay = 1;
    this.healthLevel = 0;
    this.weightUi = "000000";
    this.handsFull = 0;
    this.loadingUi = "000";
    this.visorHud = 1;
    this.timerIcon = 0;
    this.timeDisplay = "7:45 AM "
    this.middleText = convertTextToUi('@', 100)
    this.toolTipText = convertTextToUi('@', 100)
    this.updateHealth()
    this.updateTitle()
    this.generationPercent = "000"
  }
  
  updateTitle() {
    const { timeDisplay, hudDisplay, healthLevel, loadingUi, weightUi, visorHud, handsFull, timerIcon, middleText, toolTipText, generationPercent } = this;

    let loadingDisplay = convertValueToUi(String(loadingUi), 3) || "000"
    const weightDisplay = convertValueToUi(String(weightUi), 6) || "000000"
    const middleTextDisplay = convertTextToUi(String(middleText), 100) || "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
    const toolTipTextDisplay = convertTextToUi(String(toolTipText), 100) || "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
    let generationDisplay = convertValueToUi(String(Math.round(generationPercent)), 3) || "000"
    if (generationDisplay == undefined || generationDisplay == "undefined") {
      generationDisplay = "000"
    }
    if (loadingDisplay == undefined || loadingDisplay == "undefined") {
      loadingDisplay = "000"
    }
    const timerIconDisplay = timerIcon || 0
    const timeDisplayDisplay = timeDisplay || "7:45 AM "
    const title = `;${hudDisplay};${healthLevel};${loadingDisplay};${weightDisplay};${visorHud};${handsFull};${timerIconDisplay};${timeDisplayDisplay};${generationDisplay};${middleTextDisplay};${toolTipTextDisplay};`;
    const debugActionbar = replaceAtSymbols(title)

    // this.player.onScreenDisplay.setActionBar(debugActionbar)
    this.player.onScreenDisplay.setTitle(title)
  }

  /**
   * 
   * @param {number} value 
   */
  updateHud(value) {
    this.hudDisplay = value
    this.updateTitle()
  }
  /**
   * 
   * @param {number} value 
   */
  updateLoading(value) {
    this.loadingUi = value
    this.updateTitle()
  }
  /**
   * 
   * @param {number} value 
   */
  updateGenerationPercent(value) {
    this.generationPercent = value
    this.updateTitle()
  }
  /**
   * 
   * @param {number} value 
   */
  updateWeight(value) {
    this.weightUi = value
    this.updateTitle()
  }
  /**
   * 
   * @param {number} value 
   */
  updateTimeDisplay(value) {
    this.timeDisplay = value
    this.updateTitle()
  }
  /**
   * 
   * @param {number} value 
   */
  updateTimerIcon(value) {
    this.timerIcon = value
    this.updateTitle()
  }
  /**
   * 
   * @param {number} value 
   */
  updateHandsFull(value) {
    this.handsFull = value
    this.updateTitle()
  }
  /**
   * 
   * @param {string} value 
   */
  updateTooltipText(value) {
    this.toolTipText = value
    this.updateTitle()
  }
  /**
   * 
   * @param {string} value 
   */
  updateMiddleText(value) {
    this.middleText = value
    this.updateTitle()
  }
  /**
   * 
   * @param {number | undefined} newValue 
   */
  updateHealth(newValue = undefined) {
    let unclampedHealthLevel = 0
    if (newValue == undefined) {
      newValue = this.player.getComponent('minecraft:health').currentValue
    }
    unclampedHealthLevel = 5 - Math.ceil((newValue / 100) * 4);
    this.healthLevel = Math.min(Math.max(unclampedHealthLevel, 1), 4);

    if (newValue <= 10) {
      this.visorHud = 2
    }
    if (newValue >= 20) {
      this.visorHud = 1
    }
    this.updateTitle()
  }
}
