// S.O.L.O. WorkManager v1.0.50 — shop buttons and manufacturer page links
(function(){
  'use strict';

  var SHOP_URL = 'https://software-solutions.lorenzschulz.de/';
  var DEMO_BTN_ID = 'solo-workmanager-demo-shop-button';
  var TOP_BTN_ID = 'solo-workmanager-top-shop-button';
  var CONTACT_ID = 'solo-workmanager-contact-shop-link';
  var ABOUT_ID = 'solo-workmanager-about-shop-link';

  function isEnglish(){ return (localStorage.getItem('wm_language') || 'de') === 'en'; }
  function demoLabel(){ return isEnglish() ? 'Buy full version' : 'Vollversion kaufen'; }
  function topLabel(){ return isEnglish() ? 'Shop' : 'Shop'; }
  function manufacturerLabel(){ return isEnglish() ? 'S.O.L.O. WorkManager manufacturer website' : 'S.O.L.O. WorkManager Herstellerseite'; }
  function isDemo(){
    try { return window.SoloWorkManagerLicense ? window.SoloWorkManagerLicense.isDemo() : true; }
    catch(e){ return true; }
  }
  function openShop(){ window.open(SHOP_URL, '_blank', 'noopener,noreferrer'); }

  function findImportButton(bar){
    return Array.from(bar.querySelectorAll('button')).find(function(b){ return /Import|Importieren/.test(b.textContent || ''); });
  }

  function ensureDemoButton(){
    var existing = document.getElementById(DEMO_BTN_ID);
    if(!isDemo()){
      if(existing) existing.remove();
      return;
    }
    var bar = document.querySelector('.workspace-bar');
    if(!bar) return;
    if(existing){ existing.textContent = demoLabel(); existing.title = demoLabel(); return; }

    var btn = document.createElement('button');
    btn.id = DEMO_BTN_ID;
    btn.type = 'button';
    btn.className = 'btn btn-primary btn-sm solo-workmanager-demo-shop-button';
    btn.textContent = demoLabel();
    btn.title = demoLabel();
    btn.addEventListener('click', openShop);
    var importBtn = findImportButton(bar);
    if(importBtn) bar.insertBefore(btn, importBtn); else bar.appendChild(btn);
  }

  function ensureTopShopButton(){
    var bar = document.querySelector('.workspace-bar');
    if(!bar) return;
    var existing = document.getElementById(TOP_BTN_ID);
    if(existing){ existing.textContent = topLabel(); existing.title = manufacturerLabel(); return; }

    var btn = document.createElement('button');
    btn.id = TOP_BTN_ID;
    btn.type = 'button';
    btn.className = 'btn btn-ghost btn-sm solo-workmanager-top-shop-button';
    btn.textContent = topLabel();
    btn.title = manufacturerLabel();
    btn.addEventListener('click', openShop);

    var importBtn = findImportButton(bar);
    if(importBtn && importBtn.nextSibling) bar.insertBefore(btn, importBtn.nextSibling);
    else if(importBtn) bar.appendChild(btn);
    else bar.appendChild(btn);
  }

  function makeShopLink(id){
    var link = document.createElement('a');
    link.id = id;
    link.href = SHOP_URL;
    link.target = '_blank';
    link.rel = 'noopener noreferrer';
    link.className = 'solo-workmanager-manufacturer-link';
    link.textContent = manufacturerLabel();
    link.addEventListener('click', function(ev){ ev.stopPropagation(); });
    return link;
  }

  function ensureContactLink(){
    Array.from(document.querySelectorAll('.modal')).forEach(function(modal){
      var modalText = modal.textContent || '';
      if(!/Kontakt|Contact|Lorenz Schulz|info@lorenzschulz\.de/.test(modalText)) return;

      var existing = modal.querySelector('#' + CONTACT_ID);
      if(existing){ existing.textContent = manufacturerLabel(); return; }

      var body = modal.querySelector('.modal-body') || modal;
      var link = makeShopLink(CONTACT_ID);
      link.style.marginTop = '4px';

      var mail = body.querySelector('a[href^="mailto:"]');
      if(mail && mail.parentNode) mail.parentNode.insertBefore(link, mail.nextSibling);
      else body.appendChild(link);
    });
  }

  function ensureAboutLink(){
    var cards = Array.from(document.querySelectorAll('.card'));
    cards.forEach(function(card){
      if(!/Über S.O.L.O. WorkManager|About S.O.L.O. WorkManager/.test(card.textContent || '')) return;
      var existing = card.querySelector('#' + ABOUT_ID);
      if(existing){ existing.textContent = manufacturerLabel(); return; }
      var mail = card.querySelector('a[href^="mailto:"]');
      var link = makeShopLink(ABOUT_ID);
      link.style.marginTop = '6px';
      if(mail && mail.parentNode) mail.parentNode.insertBefore(link, mail.nextSibling);
      else card.appendChild(link);
    });
  }

  function run(){ ensureDemoButton(); ensureTopShopButton(); ensureContactLink(); ensureAboutLink(); }
  function start(){
    run();
    var timer = null;
    var obs = new MutationObserver(function(){ clearTimeout(timer); timer = setTimeout(run, 80); });
    obs.observe(document.body, {childList:true, subtree:true, characterData:true});
    window.addEventListener('solo-workmanager-license-changed', function(){ setTimeout(run, 50); });
    window.addEventListener('storage', function(){ setTimeout(run, 50); });
    setInterval(run, 1500);
  }
  if(document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start);
  else start();
})();
