<template>
  <div class="settings-selector" ref="selectorRef">
    <button
      class="settings-trigger"
      aria-label="Réglages d'affichage"
      @click="isOpen = !isOpen"
    >
      <div class="i-lucide-settings h-5 w-5" />
    </button>

    <Transition name="settings-dropdown">
      <div v-if="isOpen" class="settings-dropdown">
        <h4 class="settings-title">Affichage</h4>

        <!-- Palette grid : 4 saisons -->
        <div class="settings-section">
          <span class="settings-label">Ambiance</span>
          <div class="settings-palette-grid">
            <button
              v-for="name in paletteNames"
              :key="name"
              class="settings-palette-btn"
              :class="{
                'settings-palette-btn--active': paletteStore.currentPalette === name,
                'settings-palette-btn--light': paletteStore.palettes[name].isLight,
              }"
              :title="paletteStore.palettes[name].label"
              @click="paletteStore.setPalette(name)"
            >
              <span class="settings-palette-preview">
                <span class="settings-palette-dot" :style="{ background: `hsl(${paletteStore.palettes[name].primary})` }" />
                <span class="settings-palette-dot" :style="{ background: `hsl(${paletteStore.palettes[name].accent})` }" />
              </span>
              <span class="settings-palette-info">
                <span class="settings-palette-name">{{ paletteStore.palettes[name].label }}</span>
                <span class="settings-palette-mode">{{ paletteStore.palettes[name].isLight ? 'Clair' : 'Sombre' }}</span>
              </span>
            </button>
          </div>
        </div>

        <!-- Font size -->
        <div class="settings-section">
          <span class="settings-label">Taille texte</span>
          <div class="settings-toggle-group">
            <button
              v-for="size in fontSizes"
              :key="size.value"
              class="settings-toggle"
              :class="{ 'settings-toggle--active': currentFontSize === size.value }"
              @click="setFontSize(size.value)"
            >
              {{ size.label }}
            </button>
          </div>
        </div>
      </div>
    </Transition>
  </div>
</template>

<script setup lang="ts">
import { onClickOutside } from '@vueuse/core'
import type { PaletteName } from '~/stores/palette'

const paletteStore = usePaletteStore()
const selectorRef = ref<HTMLElement>()
const isOpen = ref(false)

const paletteNames: PaletteName[] = ['automne', 'hiver', 'printemps', 'ete']

const currentFontSize = ref(
  (import.meta.client && localStorage.getItem('fontSize')) || 'normal',
)

const fontSizes = [
  { label: 'A-', value: 'small' },
  { label: 'A', value: 'normal' },
  { label: 'A+', value: 'large' },
]

function setFontSize(size: string) {
  currentFontSize.value = size
  if (import.meta.client) {
    localStorage.setItem('fontSize', size)
    const root = document.documentElement
    const map: Record<string, string> = { small: '14px', normal: '16px', large: '18px' }
    root.style.fontSize = map[size] || '16px'
  }
}

// Apply font size on mount
onMounted(() => {
  const saved = localStorage.getItem('fontSize')
  if (saved) setFontSize(saved)
})

onClickOutside(selectorRef, () => { isOpen.value = false })
</script>

<style scoped>
.settings-selector {
  position: relative;
}

.settings-trigger {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 0.5rem;
  color: hsl(var(--color-text) / 0.7);
  transition: all 0.2s;
}
.settings-trigger:hover {
  color: hsl(var(--color-text));
  background: hsl(var(--color-text) / 0.1);
}

.settings-dropdown {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  width: 260px;
  padding: 0.75rem;
  border-radius: 0.75rem;
  background: hsl(var(--color-surface));
  backdrop-filter: blur(16px);
  border: 1px solid hsl(var(--color-text) / 0.08);
  box-shadow: 0 8px 32px hsl(0 0% 0% / 0.3);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  z-index: 50;
}

.settings-title {
  font-family: var(--font-display);
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: hsl(var(--color-text) / 0.4);
  margin: 0;
}

.settings-section {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.settings-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: hsl(var(--color-text) / 0.5);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.settings-toggle-group {
  display: flex;
  gap: 0.25rem;
  background: hsl(var(--color-text) / 0.04);
  border-radius: 0.5rem;
  padding: 0.125rem;
}

.settings-toggle {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.375rem 0.5rem;
  border-radius: 0.375rem;
  font-size: 0.75rem;
  font-weight: 500;
  color: hsl(var(--color-text) / 0.5);
  transition: all 0.15s;
}
.settings-toggle:hover {
  color: hsl(var(--color-text) / 0.8);
}
.settings-toggle--active {
  background: hsl(var(--color-primary));
  color: white;
  box-shadow: 0 1px 4px hsl(var(--color-primary) / 0.3);
}

.settings-palette-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.375rem;
}

.settings-palette-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  border-radius: 0.5rem;
  transition: all 0.2s;
  color: hsl(var(--color-text) / 0.6);
  background: hsl(var(--color-text) / 0.02);
}
.settings-palette-btn:hover {
  background: hsl(var(--color-text) / 0.06);
  color: hsl(var(--color-text) / 0.9);
}
.settings-palette-btn--active {
  background: hsl(var(--color-text) / 0.1);
  color: hsl(var(--color-text));
  box-shadow: inset 0 0 0 1.5px hsl(var(--color-primary) / 0.5);
}

.settings-palette-preview {
  display: flex;
  gap: 0.125rem;
  flex-shrink: 0;
}

.settings-palette-dot {
  width: 0.75rem;
  height: 0.75rem;
  border-radius: 50%;
  box-shadow: 0 1px 3px hsl(0 0% 0% / 0.25);
}

.settings-palette-info {
  display: flex;
  flex-direction: column;
  gap: 0;
  min-width: 0;
}

.settings-palette-name {
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.2;
}

.settings-palette-mode {
  font-size: 0.6rem;
  opacity: 0.5;
  line-height: 1.2;
}

.settings-dropdown-enter-active {
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
.settings-dropdown-leave-active {
  transition: all 0.15s ease;
}
.settings-dropdown-enter-from,
.settings-dropdown-leave-to {
  opacity: 0;
  transform: translateY(-4px) scale(0.96);
}
</style>
