'use strict';

(() => {
  const VERSION = 2;
  const MAX_LEVEL = 20;
  const ASSET_BASE = '../assets/buildings/levels/barracks';

  function clampLevel(value) {
    return Math.max(1, Math.min(MAX_LEVEL, Math.floor(Number(value) || 1)));
  }

  function assetForLevel(value) {
    const level = clampLevel(value);
    return `${ASSET_BASE}/level_${String(level).padStart(2, '0')}.png`;
  }

  function altForLevel(value) {
    return `Quartel medieval — nível ${clampLevel(value)}`;
  }

  function castleArt(building, fallbackIcon = '⚔️') {
    if (!building || building.type !== 'barracks') {
      return `<span class="castle-building-art"><span class="castle-building-icon">${fallbackIcon}</span></span>`;
    }
    const level = clampLevel(building.level);
    const growth = (0.82 + (level - 1) * 0.014).toFixed(3);
    return `<span class="castle-building-art barracks-visual-art" style="--barracks-growth:${growth}">
      <img class="barracks-level-image" src="${assetForLevel(level)}" alt="${altForLevel(level)}" draggable="false" />
    </span>`;
  }


  function isometricMetrics(value) {
    const level = clampLevel(value);
    const progress = (level - 1) / (MAX_LEVEL - 1);
    return {
      width: Math.round(244 + progress * 54),
      height: Math.round(194 + progress * 42),
      imageScale: (0.98 + progress * 0.08).toFixed(3)
    };
  }

  function isometricArt(building) {
    if (!building || building.type !== 'barracks') return '';
    const level = clampLevel(building.level);
    const metrics = isometricMetrics(level);
    return `<img class="barracks-level-image barracks-isometric-image"
      src="${assetForLevel(level)}"
      alt="${altForLevel(level)}"
      draggable="false"
      style="--barracks-iso-scale:${metrics.imageScale}" />`;
  }

  function previewHtml(value) {
    const level = clampLevel(value);
    return `<div class="barracks-building-preview">
      <img class="barracks-level-image" src="${assetForLevel(level)}" alt="${altForLevel(level)}" draggable="false" />
    </div>`;
  }

  globalThis.ReinosBarracksVisuals = Object.freeze({
    VERSION,
    MAX_LEVEL,
    clampLevel,
    assetForLevel,
    castleArt,
    isometricMetrics,
    isometricArt,
    previewHtml
  });
})();
