<template>
  <div class="ds-selector" ref="selectorRef">
    <button class="ds-trigger" aria-label="Réglages d'affichage" @click="isOpen = !isOpen">
      <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/>
      </svg>
    </button>

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

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

        <!-- Chart density -->
        <div class="ds-section">
          <span class="ds-label">Densite graphiques</span>
          <div class="ds-toggle-group">
            <button v-for="d in densities" :key="d.value"
              class="ds-toggle" :class="{ 'ds-toggle--active': currentDensity === d.value }"
              @click="setDensity(d.value)">
              {{ d.label }}
            </button>
          </div>
        </div>

        <!-- Palette -->
        <div class="ds-section">
          <span class="ds-label">Ambiance</span>
          <div class="ds-palette-grid">
            <button v-for="p in palettes" :key="p.name"
              class="ds-palette-btn" :class="{ 'ds-palette-btn--active': currentPalette === p.name }"
              @click="setPalette(p.name)">
              <span class="ds-palette-dots">
                <span class="ds-dot" :style="{ background: p.primary }"></span>
                <span class="ds-dot" :style="{ background: p.accent }"></span>
              </span>
              <span class="ds-palette-name">{{ p.label }}</span>
            </button>
          </div>
        </div>
      </div>
    </Transition>
  </div>
</template>

<script setup lang="ts">
const selectorRef = ref<HTMLElement>()
const isOpen = ref(false)

// ── Font size ──
const fontSizes = [
  { label: 'A-', value: 'small' },
  { label: 'A', value: 'normal' },
  { label: 'A+', value: 'large' },
]
const currentFontSize = ref('normal')

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

// ── Chart density ──
const densities = [
  { label: 'Compact', value: 'compact' },
  { label: 'Normal', value: 'normal' },
  { label: 'Large', value: 'large' },
]
const currentDensity = ref('normal')

function setDensity(d: string) {
  currentDensity.value = d
  if (import.meta.client) {
    localStorage.setItem('sej-density', d)
    const map: Record<string, string> = { compact: '0.85', normal: '1', large: '1.15' }
    document.documentElement.style.setProperty('--chart-scale', map[d] || '1')
  }
}

// ── Palette (color ambiance) ──
const palettes = [
  { name: 'eau', label: 'Eau', primary: '#2563eb', accent: '#059669' },
  { name: 'terre', label: 'Terre', primary: '#92400e', accent: '#d97706' },
  { name: 'foret', label: 'Foret', primary: '#166534', accent: '#65a30d' },
  { name: 'ardoise', label: 'Ardoise', primary: '#475569', accent: '#64748b' },
]
const currentPalette = ref('eau')

function setPalette(name: string) {
  currentPalette.value = name
  const p = palettes.find(x => x.name === name)
  if (!p || !import.meta.client) return
  localStorage.setItem('sej-palette', name)
  const root = document.documentElement.style
  root.setProperty('--color-primary', p.primary)
  root.setProperty('--color-primary-dark', darken(p.primary))
  root.setProperty('--color-secondary', p.accent)
}

function darken(hex: string): string {
  const r = parseInt(hex.slice(1, 3), 16)
  const g = parseInt(hex.slice(3, 5), 16)
  const b = parseInt(hex.slice(5, 7), 16)
  const f = 0.82
  return `#${Math.round(r * f).toString(16).padStart(2, '0')}${Math.round(g * f).toString(16).padStart(2, '0')}${Math.round(b * f).toString(16).padStart(2, '0')}`
}

// ── Init from localStorage ──
onMounted(() => {
  if (!import.meta.client) return
  const savedFont = localStorage.getItem('sej-fontSize')
  if (savedFont) setFontSize(savedFont)
  const savedDensity = localStorage.getItem('sej-density')
  if (savedDensity) setDensity(savedDensity)
  const savedPalette = localStorage.getItem('sej-palette')
  if (savedPalette) setPalette(savedPalette)
})

// ── Click outside to close ──
function onClickOutside(e: MouseEvent) {
  if (selectorRef.value && !selectorRef.value.contains(e.target as Node)) {
    isOpen.value = false
  }
}
onMounted(() => document.addEventListener('click', onClickOutside))
onUnmounted(() => document.removeEventListener('click', onClickOutside))
</script>

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

.ds-trigger {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 6px;
  color: var(--color-text-muted);
  background: none;
  border: none;
  cursor: pointer;
  transition: all 0.15s;
}
.ds-trigger:hover {
  color: var(--color-text);
  background: var(--color-bg);
}

.ds-dropdown {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  width: 240px;
  padding: 0.75rem;
  border-radius: 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: 0 8px 24px rgba(0,0,0,0.12), 0 2px 8px rgba(0,0,0,0.06);
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  z-index: 100;
}

.ds-title {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--color-text-muted);
  margin: 0;
}

.ds-section {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.ds-label {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.ds-toggle-group {
  display: flex;
  gap: 0.2rem;
  background: var(--color-bg);
  border-radius: 6px;
  padding: 2px;
}

.ds-toggle {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.3rem 0.4rem;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--color-text-muted);
  background: none;
  border: none;
  cursor: pointer;
  transition: all 0.15s;
}
.ds-toggle:hover { color: var(--color-text); }
.ds-toggle--active {
  background: var(--color-primary);
  color: white;
  box-shadow: 0 1px 3px rgba(37, 99, 235, 0.25);
}

.ds-palette-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.3rem;
}

.ds-palette-btn {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.4rem;
  border-radius: 6px;
  background: var(--color-bg);
  border: 1.5px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
}
.ds-palette-btn:hover { border-color: var(--color-border); }
.ds-palette-btn--active {
  border-color: var(--color-primary);
  background: white;
}

.ds-palette-dots {
  display: flex;
  gap: 2px;
}
.ds-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}

.ds-palette-name {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--color-text);
}

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