<script>
  import { get } from 'svelte/store';
  import { page } from '$app/state';
  import {
    locale,
    loadTranslations,
    setLocale,
    setRoute,
    SUPPORTED_LOCALES,
    t,
  } from '$lib/translations';

  async function toggleLocale() {
    const current = get(locale);
    const next =
      current === SUPPORTED_LOCALES.EN_US
        ? SUPPORTED_LOCALES.PT_BR
        : SUPPORTED_LOCALES.EN_US;
    const route = page.url.pathname;
    await loadTranslations(next, route);
    await setLocale(next);
    await setRoute(route);
  }

  const isEn = $derived($locale === SUPPORTED_LOCALES.EN_US);
</script>

<div class="lang-float">
  <button
    type="button"
    class="lang-toggle"
    class:lang-toggle--en={isEn}
    role="switch"
    aria-checked={isEn}
    aria-label={$t('header.langToggleAria')}
    title={isEn ? 'English' : 'Português'}
    onclick={toggleLocale}
  >
    <span class="lang-toggle__inner">
      <svg
        class="lang-toggle__icon"
        width="22"
        height="22"
        viewBox="0 0 24 24"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
        aria-hidden="true"
      >
        <circle
          cx="12"
          cy="12"
          r="9.25"
          stroke="currentColor"
          stroke-width="1.75"
        />
        <ellipse
          cx="12"
          cy="12"
          rx="3.25"
          ry="9.25"
          stroke="currentColor"
          stroke-width="1.75"
        />
        <path
          d="M3.25 12h17.5"
          stroke="currentColor"
          stroke-width="1.75"
          stroke-linecap="round"
        />
      </svg>
      <span class="lang-toggle__code" aria-hidden="true"
        >{isEn ? 'EN' : 'PT'}</span
      >
    </span>
  </button>
</div>

<style>
  .lang-float {
    position: fixed;
    z-index: 40;
    left: max(12px, env(safe-area-inset-right, 0px));
    top: auto;
    bottom: max(16px, env(safe-area-inset-bottom, 0px));
    pointer-events: none;
    padding: 0;
    filter: drop-shadow(0 2px 10px rgba(0, 0, 0, 0.18));
  }

  @media (min-width: 901px) {
    .lang-float {
      top: 30px;
      bottom: auto;
    }
  }

  .lang-toggle {
    pointer-events: auto;
    box-sizing: border-box;
    min-width: 54px;
    min-height: 54px;
    padding: 7px 8px 8px;
    border: none;
    border-radius: 9999px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    cursor: pointer;
    color: #ffffff;
    /* WCAG-friendly vs white icon: dark green = PT */
    background: #0b5c3a;
    border: 2px solid rgba(255, 255, 255, 0.35);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.2),
      0 0 0 1px rgba(0, 0, 0, 0.12);
    transition:
      background-color 0.22s ease,
      border-color 0.22s ease,
      box-shadow 0.22s ease;
    -webkit-tap-highlight-color: transparent;
  }

  .lang-toggle__inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
  }

  .lang-toggle__code {
    font-size: 8px;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    line-height: 1;
    color: rgba(255, 255, 255, 0.5);
    transition: color 0.2s ease;
  }

  .lang-toggle:hover .lang-toggle__code {
    color: rgba(255, 255, 255, 0.72);
  }

  /* English: deep blue, same white icon contrast */
  .lang-toggle--en {
    background: #124080;
    border-color: rgba(255, 255, 255, 0.38);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.18),
      0 0 0 1px rgba(0, 0, 0, 0.14);
  }

  .lang-toggle:focus {
    outline: none;
  }

  .lang-toggle:focus-visible {
    outline: 3px solid #ffffff;
    outline-offset: 3px;
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.2),
      0 0 0 2px rgba(0, 0, 0, 0.35);
  }

  .lang-toggle:hover {
    filter: brightness(1.06);
  }

  .lang-toggle:active {
    filter: brightness(0.94);
  }

  .lang-toggle__icon {
    flex-shrink: 0;
  }

  @media (max-width: 900px) {
    .lang-toggle {
      min-width: 58px;
      min-height: 58px;
      padding: 8px 9px 9px;
    }

    .lang-toggle__icon {
      width: 24px;
      height: 24px;
    }

    .lang-toggle__code {
      font-size: 9px;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .lang-toggle {
      transition: none;
    }
  }
</style>
