const siteData = window.VIPGO_SITE_DATA || {};

const icons = {
  download: '<svg viewBox="0 0 24 24" role="img" focusable="false"><path d="M12 5c-5.23 0-9.27 4.11-10.58 6.12a1.5 1.5 0 0 0 0 1.76C2.73 14.89 6.77 19 12 19s9.27-4.11 10.58-6.12a1.5 1.5 0 0 0 0-1.76C21.27 9.11 17.23 5 12 5Zm0 11c-3.96 0-7.22-2.97-8.67-4.99C4.78 9.97 8.04 7 12 7s7.22 2.97 8.67 4.99C19.22 13.03 15.96 16 12 16Zm0-7a3 3 0 1 0 0 6 3 3 0 0 0 0-6Zm0 2a1 1 0 1 1 0 2 1 1 0 0 1 0-2Z"/></svg>',
  calculator: '<svg viewBox="0 0 24 24" role="img" focusable="false"><path d="M7 2a3 3 0 0 0-3 3v14a3 3 0 0 0 3 3h10a3 3 0 0 0 3-3V5a3 3 0 0 0-3-3H7Zm0 2h10a1 1 0 0 1 1 1v2H6V5a1 1 0 0 1 1-1Zm-1 5h12v10a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1V9Zm2 2a1 1 0 0 0 0 2h2a1 1 0 1 0 0-2H8Zm5 0a1 1 0 1 0 0 2h3a1 1 0 1 0 0-2h-3ZM8 15a1 1 0 1 0 0 2h2a1 1 0 1 0 0-2H8Zm5 0a1 1 0 1 0 0 2h3a1 1 0 1 0 0-2h-3Z"/></svg>'
};

function renderPageFromData() {
  document.getElementById("introText").innerText = siteData.introText || "";
  document.getElementById("privacy_policy_note").innerText = siteData.copyrightText || "";

  const fileModalData = siteData.fileModal || {};
  document.getElementById("fileModalText").innerText = fileModalData.defaultSubtitle || "";
  document.getElementById("openOnlineText").innerText = fileModalData.openOnlineText || "ონლაინ გახსნა";
  document.getElementById("downloadFileText").innerText = fileModalData.downloadText || "ფაილის გადმოწერა";

  const calcData = siteData.calculator || {};
  document.getElementById("calcTitle").innerText = calcData.title || "";
  document.getElementById("calcButtonText").innerText = calcData.buttonText || "გამოთვლა";
  document.getElementById("calcHelperText").innerText = calcData.helperText || "";

  const calcFields = document.getElementById("calcFields");
  calcFields.innerHTML = "";
  (calcData.fields || []).forEach((field) => {
    const input = document.createElement("input");
    input.type = "number";
    input.id = field.id;
    input.placeholder = field.placeholder || "";
    input.min = field.min || "0";
    input.step = field.step || "0.01";
    input.inputMode = field.inputmode || "decimal";
    calcFields.appendChild(input);
  });

  const buttonsWrap = document.getElementById("actionButtons");
  buttonsWrap.innerHTML = "";
  (siteData.buttons || []).forEach((btn) => {
    const anchor = document.createElement("a");
    anchor.href = "#";
    anchor.className = "vip-btn";

    const icon = document.createElement("span");
    icon.className = "btn-icon";
    icon.setAttribute("aria-hidden", "true");
    icon.innerHTML = icons[btn.icon] || "";
    anchor.appendChild(icon);

    anchor.appendChild(document.createTextNode(btn.label || ""));

    if (btn.type === "file") {
      anchor.addEventListener("click", function(e) {
        e.preventDefault();
        openFileModal(btn.fileTitle || btn.label || "", btn.fileUrl || "#");
      });
    } else if (btn.type === "calculator") {
      anchor.addEventListener("click", function(e) {
        e.preventDefault();
        openCalc();
      });
    }

    buttonsWrap.appendChild(anchor);
  });
}

function openFileModal(title, fileUrl) {
  const modal = document.getElementById("fileModal");
  const titleEl = document.getElementById("fileModalTitle");
  const textEl = document.getElementById("fileModalText");
  const openBtn = document.getElementById("openOnlineBtn");
  const downloadBtn = document.getElementById("downloadFileBtn");
  const fileModalData = siteData.fileModal || {};

  titleEl.innerText = title;
  textEl.innerText = fileModalData.defaultSubtitle || "აირჩიე: გახსნა საიტზე ან გადმოწერა მოწყობილობაზე";
  openBtn.href = fileUrl;
  downloadBtn.href = fileUrl;
  downloadBtn.setAttribute("download", "");

  modal.style.display = "block";
  modal.setAttribute("aria-hidden", "false");
}

function closeFileModal() {
  const modal = document.getElementById("fileModal");
  modal.style.display = "none";
  modal.setAttribute("aria-hidden", "true");
}

function openCalc() {
  const modal = document.getElementById("calcModal");
  modal.style.display = "block";
  modal.setAttribute("aria-hidden", "false");
  setTimeout(() => {
    const firstField = (siteData.calculator && siteData.calculator.fields && siteData.calculator.fields[0] && siteData.calculator.fields[0].id) || "length";
    const firstInput = document.getElementById(firstField);
    if (firstInput) firstInput.focus();
  }, 50);
}

function closeCalc() {
  const modal = document.getElementById("calcModal");
  modal.style.display = "none";
  modal.setAttribute("aria-hidden", "true");
}

function calculate() {
  const calcData = siteData.calculator || {};
  const fields = calcData.fields || [];
  const result = document.getElementById("result");
  const values = fields.map((field) => parseFloat(document.getElementById(field.id).value));

  if (values.some((value) => !value || value <= 0)) {
    result.innerText = calcData.validationText || "შეიყვანე ყველა ველი სწორად.";
    return;
  }

  const divisor = Number(calcData.divisor) || 6000;
  const volumetric = values.reduce((acc, value) => acc * value, 1) / divisor;
  result.innerText = (calcData.resultPrefix || "მოცულობითი წონა: ") + volumetric.toFixed(2) + (calcData.resultSuffix || " კგ");
}

document.addEventListener("DOMContentLoaded", function() {
  renderPageFromData();

  document.getElementById("calcModal").addEventListener("click", function(e) {
    if (e.target === this) closeCalc();
  });

  document.getElementById("fileModal").addEventListener("click", function(e) {
    if (e.target === this) closeFileModal();
  });

  document.addEventListener("keydown", function(event) {
    if (event.key === "Escape") {
      closeCalc();
      closeFileModal();
    }

    if (event.key === "Enter" && document.getElementById("calcModal").style.display === "block") {
      calculate();
    }
  });
});
