'use strict';

(() => {
  const VERSION = 4;
  const DEFAULT_MAX_VISUAL_LEVEL = 20;
  const TYPE_MAX_LEVEL = Object.freeze({ keep: 80 });
  const ASSET_BASE = '../assets/buildings/levels';
  const TYPES = Object.freeze([
    'keep', 'woodcutter', 'quarry', 'farm', 'house', 'barracks', 'warehouse',
    'marketplace', 'workshop', 'stables', 'hospital', 'tavern', 'watchtower', 'decoration'
  ]);
  const TYPE_SET = new Set(TYPES);

  function supports(type) {
    return TYPE_SET.has(String(type || ''));
  }

  function maxVisualLevel(type) {
    return TYPE_MAX_LEVEL[String(type || '')] || DEFAULT_MAX_VISUAL_LEVEL;
  }

  function normalizeLevel(level, type) {
    return Math.max(1, Math.min(maxVisualLevel(type), Math.floor(Number(level) || 1)));
  }

  function levelFile(type, level) {
    return `level_${String(normalizeLevel(level, type)).padStart(2, '0')}.png`;
  }

  function assetFor(type, level = 1) {
    return supports(type) ? `${ASSET_BASE}/${type}/${levelFile(type, level)}` : '';
  }

  function altFor(building) {
    const type = building?.type || 'edifício';
    const level = Math.max(1, Number(building?.level) || 1);
    return `${type} medieval — nível ${level}`;
  }

  function visualPhase(level) {
    return Math.min(5, Math.ceil(normalizeLevel(level) / 4));
  }

  function metrics(type, level = 1) {
    if (!supports(type)) return null;
    const visualLevel = normalizeLevel(level, type);
    const growth = Math.min(type === 'keep' ? 50 : 28, Math.max(0, visualLevel - 1) * (type === 'keep' ? 0.65 : 1.35));
    if (type === 'keep') return { width: 292 + growth, height: 232 + Math.round(growth * 0.62) };
    if (type === 'watchtower') return { width: 235 + growth, height: 225 + Math.round(growth * 0.58) };
    if (type === 'decoration') return { width: 205 + growth, height: 195 + Math.round(growth * 0.55) };
    return { width: 248 + growth, height: 205 + Math.round(growth * 0.55) };
  }

  function imageHtml(building, extraClass = '') {
    if (!building || !supports(building.type)) return '';
    const visualLevel = normalizeLevel(building.level, building.type);
    const phase = visualPhase(visualLevel);
    const src = assetFor(building.type, visualLevel);
    return `<img class="illustrated-building-image building-visual-level-${visualLevel} building-visual-phase-${phase} ${extraClass}" src="${src}" alt="${altFor(building)}" data-building-visual-level="${visualLevel}" draggable="false" />`;
  }

  function isometricArt(building) {
    return imageHtml(building, 'illustrated-building-image-isometric');
  }

  function castleArt(building) {
    const image = imageHtml(building, 'illustrated-building-image-castle');
    if (!image) return '';
    return `<span class="castle-building-art illustrated-building-art">${image}</span>`;
  }

  globalThis.ReinosIllustratedBuildings = Object.freeze({
    VERSION,
    MAX_VISUAL_LEVEL: DEFAULT_MAX_VISUAL_LEVEL,
    DEFAULT_MAX_VISUAL_LEVEL,
    TYPE_MAX_LEVEL,
    maxVisualLevel,
    TYPES,
    supports,
    normalizeLevel,
    visualPhase,
    assetFor,
    metrics,
    isometricArt,
    castleArt
  });
})();
