<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()
const { locale } = useI18n()

const props = defineProps<{
  show: boolean
}>()

const emit = defineEmits<{
  'update:show': [value: boolean]
  toast: [msg: string, type: 'success' | 'error']
}>()

const localShow = ref(props.show)

watch(() => props.show, (val) => {
  localShow.value = val
})

const selectedTheme = ref(localStorage.getItem('quibot-theme') || 'dark')
const selectedLanguage = ref(localStorage.getItem('quibot-locale') || 'en')
const piUrl = ref(localStorage.getItem('quibot-pi-url') || 'http://raspberrypi.local:8000')

function applyTheme(theme: string) {
  document.documentElement.setAttribute('data-theme', theme)
  localStorage.setItem('quibot-theme', theme)
}

function saveSettings() {
  applyTheme(selectedTheme.value)
  locale.value = selectedLanguage.value as any
  localStorage.setItem('quibot-locale', selectedLanguage.value)
  localStorage.setItem('quibot-pi-url', piUrl.value)
  document.cookie = `quibot-pi-url=${encodeURIComponent(piUrl.value)};path=/`
  emit('toast', t('settings.saved'), 'success')
  emit('update:show', false)
}

function close() {
  localShow.value = false
  emit('update:show', false)
}

function backdropClick(e: MouseEvent) {
  if ((e.target as HTMLElement).classList.contains('settings-backdrop')) {
    close()
  }
}
</script>

<template>
  <Teleport to="body">
    <div v-show="localShow" class="settings-backdrop" @click="backdropClick">
      <div class="settings-modal">
        <div class="settings-header">
          <h2>{{ $t('settings.title') }}</h2>
          <button class="close-btn" @click="close">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M18 6L6 18M6 6l12 12"/></svg>
          </button>
        </div>

        <!-- Theme -->
        <div class="setting-row">
          <span class="setting-label">{{ $t('settings.theme.label') }}</span>
          <div class="theme-options">
            <button
              :class="['theme-option', { active: selectedTheme === 'light' }]"
              @click="selectedTheme = 'light'"
            >
              <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 12.79A9 9 0 1111.21 3a7 7 0 009.79 9.79z"/></svg>
              <span>{{ $t('settings.theme.light') }}</span>
            </button>
            <button
              :class="['theme-option', { active: selectedTheme === 'dark' }]"
              @click="selectedTheme = 'dark'"
            >
              <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="5"/><path d="M12 1v2m0 18v2m11-11h-2M3 12H1m17.07-7.07l-1.41 1.41M6.34 17.66l-1.41 1.41m14.14 0l-1.41-1.41M6.34 6.34L4.93 4.93"/></svg>
              <span>{{ $t('settings.theme.dark') }}</span>
            </button>
          </div>
        </div>

        <!-- Language -->
        <div class="setting-row">
          <span class="setting-label">{{ $t('settings.language.label') }}</span>
          <select v-model="selectedLanguage" class="lang-select">
            <option value="en">English</option>
            <option value="ca">Català</option>
            <option value="es">Español</option>
          </select>
        </div>

        <!-- PI URL -->
        <div class="setting-row">
          <span class="setting-label">{{ $t('settings.piUrl.label') }}</span>
          <input
            v-model="piUrl"
            :placeholder="$t('settings.piUrl.placeholder')"
            type="text"
            class="url-input"
          />
        </div>

        <!-- Save -->
        <button class="btn-save" @click="saveSettings">{{ $t('settings.save') }}</button>
      </div>
    </div>
  </Teleport>
</template>

<style scoped>
.settings-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.settings-backdrop[style*="display: block"],
.settings-backdrop[style*="display:flex"] {
  display: flex !important;
  opacity: 1;
}

.settings-modal {
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 1rem;
  padding: 1.5rem;
  width: 90%;
  max-width: 420px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.settings-header h2 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.close-btn {
  display: grid;
  place-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  background: var(--btn-bg);
  border: 2px solid var(--border-color);
  border-radius: 0.5rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

.close-btn:hover {
  border-color: #ef4444;
  color: #ef4444;
}

.setting-row {
  margin-bottom: 1.25rem;
}

.setting-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.5rem;
}

.theme-options {
  display: flex;
  gap: 0.5rem;
}

.theme-option {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.625rem 1rem;
  font-size: 0.8rem;
  font-weight: 600;
  font-family: inherit;
  color: var(--text-muted);
  background: var(--bg-secondary);
  border: 2px solid var(--border-color);
  border-radius: 0.625rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.theme-option.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--active-bg);
}

.theme-option:not(:disabled):hover {
  border-color: var(--border-subtle);
  color: var(--text-primary);
}

.lang-select,
.url-input {
  width: 100%;
  padding: 0.625rem 0.75rem;
  font-family: inherit;
  font-size: 0.85rem;
  color: var(--text-primary);
  background: var(--bg-secondary);
  border: 2px solid var(--border-color);
  border-radius: 0.625rem;
  outline: none;
  transition: border-color 0.15s ease;
  box-sizing: border-box;
}

.lang-select:focus,
.url-input:focus {
  border-color: var(--accent);
}

.url-input::placeholder {
  color: var(--text-ghost);
}

.btn-save {
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 0.85rem;
  font-weight: 700;
  font-family: inherit;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), #ea580c);
  border: none;
  border-radius: 0.625rem;
  cursor: pointer;
  transition: all 0.2s ease;
  margin-top: 0.5rem;
}

.btn-save:hover {
  box-shadow: 0 4px 16px var(--accent-glow);
  transform: translateY(-1px);
}
</style>
