<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useGatherStore } from '@/stores/gather'
import Panel from '@/components/ds/data/Panel.vue'
import Button from '@/components/ds/core/Button.vue'
import Icon from '@/components/ds/core/Icon.vue'
import Tabs from '@/components/ds/navigation/Tabs.vue'
import EmptyState from '@/components/ds/feedback/EmptyState.vue'

const store = useGatherStore()

const activeTab = ref<'resources' | 'advanced'>('resources')
const showCreateModal = ref(false)
const showImportModal = ref(false)
const newConfigName = ref('')
const newConfigDesc = ref('')
const importConfigName = ref('')

const tabItems = [
  { value: 'resources', label: 'Resource rates', icon: 'pickaxe' },
  { value: 'advanced', label: 'Advanced', icon: 'settings' },
]

// Resource definitions for the main gather tab
const gatherResources = [
  { key: 'Wood', label: 'Wood' },
  { key: 'Stones', label: 'Stones' },
  { key: 'Metal Ore', label: 'Metal ore' },
  { key: 'Sulfur Ore', label: 'Sulfur ore' },
  { key: 'HQM Ore', label: 'HQM ore' },
  { key: 'Cloth', label: 'Cloth' },
  { key: 'Leather', label: 'Leather' },
  { key: 'Animal Fat', label: 'Animal fat' },
  { key: 'Bone Fragments', label: 'Bone fragments' },
]

// Advanced resource categories
const pickupResources = [
  { key: 'Wood', label: 'Wood' },
  { key: 'Stones', label: 'Stones' },
  { key: 'Metal Ore', label: 'Metal ore' },
  { key: 'Sulfur Ore', label: 'Sulfur ore' },
]

const quarryResources = [
  { key: 'HQM Ore', label: 'HQM ore' },
  { key: 'Metal Ore', label: 'Metal ore' },
  { key: 'Sulfur Ore', label: 'Sulfur ore' },
  { key: 'Stones', label: 'Stones' },
]

const excavatorResources = [
  { key: 'HQM Ore', label: 'HQM ore' },
  { key: 'Metal Ore', label: 'Metal ore' },
  { key: 'Sulfur Ore', label: 'Sulfur ore' },
  { key: 'Stones', label: 'Stones' },
]

const surveyResources = [
  { key: 'Metal Ore', label: 'Metal ore' },
  { key: 'Sulfur Ore', label: 'Sulfur ore' },
  { key: 'Stones', label: 'Stones' },
  { key: 'HQM Ore', label: 'HQM ore' },
]

const presets = [
  { label: '1x', value: 1 },
  { label: '2x', value: 2 },
  { label: '3x', value: 3 },
  { label: '5x', value: 5 },
  { label: '10x', value: 10 },
]

onMounted(async () => {
  await store.fetchConfigs()
  if (store.configs.length > 0 && store.configs[0]) {
    await store.loadConfig(store.configs[0].id)
  }
})

// --- Config data helpers ---

function getConfigValue(path: string, defaultValue: any = 1): any {
  if (!store.currentConfig?.config_data) return defaultValue
  const parts = path.split('.')
  let current: any = store.currentConfig.config_data
  for (const part of parts) {
    if (current == null || typeof current !== 'object') return defaultValue
    current = current[part]
  }
  return current ?? defaultValue
}

function setConfigValue(path: string, value: any) {
  if (!store.currentConfig) return
  if (!store.currentConfig.config_data) store.currentConfig.config_data = {}
  const parts = path.split('.')
  let current: any = store.currentConfig.config_data
  for (let i = 0; i < parts.length - 1; i++) {
    const part = parts[i]!
    if (current[part] == null || typeof current[part] !== 'object') {
      current[part] = {}
    }
    current = current[part]
  }
  current[parts[parts.length - 1]!] = value
  store.markDirty()
}

// --- Preset handler ---

function applyPreset(multiplier: number) {
  for (const resource of gatherResources) {
    setConfigValue(`GatherResourceModifiers.${resource.key}`, multiplier)
  }
}

// --- Action handlers ---

async function handleConfigChange(id: string) {
  if (store.isDirty) {
    if (!confirm('Unsaved changes will be lost. Continue?')) return
  }
  await store.loadConfig(id)
}

async function handleCreateConfig() {
  if (!newConfigName.value.trim()) return
  const config = await store.createConfig(
    newConfigName.value.trim(),
    newConfigDesc.value.trim() || undefined,
  )
  if (config) {
    showCreateModal.value = false
    newConfigName.value = ''
    newConfigDesc.value = ''
  }
}

async function handleDeleteConfig() {
  if (!store.currentConfig) return
  if (!confirm(`Delete "${store.currentConfig.config_name}"? This cannot be undone.`)) return
  await store.deleteConfig(store.currentConfig.id)
}

async function handleApply() {
  if (!store.currentConfig) return
  if (!confirm('Apply this gather config to the server? This will overwrite the current GatherManager config.')) return
  if (store.isDirty) {
    await store.saveCurrentConfig()
  }
  await store.applyToServer(store.currentConfig.id)
}

async function handleImport() {
  if (!importConfigName.value.trim()) return
  const config = await store.importFromServer(importConfigName.value.trim())
  if (config) {
    showImportModal.value = false
    importConfigName.value = ''
  }
}
</script>

<template>
  <div class="gv">
    <!-- Page head -->
    <div class="gv__head">
      <div class="gv__head-id">
        <div class="gv__head-chip">
          <Icon name="pickaxe" :size="20" :stroke-width="2" />
        </div>
        <div>
          <div class="t-eyebrow">Plugin config</div>
          <h1 class="gv__title">Gather rates</h1>
        </div>
      </div>
      <Button size="sm" icon="plus" @click="showCreateModal = true">New config</Button>
    </div>

    <!-- Config action bar -->
    <Panel>
      <div class="gv__bar">
        <select
          v-if="store.configs.length > 0"
          :value="store.currentConfig?.id ?? ''"
          class="gv__config-select"
          @change="handleConfigChange(($event.target as HTMLSelectElement).value)"
        >
          <option v-for="c in store.configs" :key="c.id" :value="c.id">
            {{ c.config_name }}{{ c.is_active ? ' (Active)' : '' }}
          </option>
        </select>
        <span v-else class="gv__no-configs">No configs yet</span>

        <Button
          icon="save"
          size="sm"
          :disabled="!store.currentConfig || !store.isDirty || store.isSaving"
          :loading="store.isSaving"
          @click="store.saveCurrentConfig()"
        >{{ store.isSaving ? 'Saving…' : 'Save' }}</Button>

        <Button
          variant="outline"
          icon="play"
          size="sm"
          :disabled="!store.currentConfig || store.isApplying"
          :loading="store.isApplying"
          @click="handleApply"
        >{{ store.isApplying ? 'Applying…' : 'Apply to server' }}</Button>

        <Button
          variant="secondary"
          icon="download"
          size="sm"
          @click="showImportModal = true"
        >Import from server</Button>

        <Button
          variant="danger-soft"
          icon="trash-2"
          size="sm"
          :disabled="!store.currentConfig"
          class="gv__bar-delete"
          @click="handleDeleteConfig"
        >Delete</Button>
      </div>
    </Panel>

    <!-- Loading -->
    <div v-if="store.isLoading" class="gv__loading">
      <span class="gv__spinner" />
    </div>

    <!-- Empty state -->
    <Panel v-else-if="!store.currentConfig">
      <EmptyState
        icon="pickaxe"
        title="No gather config selected"
        description="Create a new config, import from server, or select one from the dropdown above."
      >
        <template #action>
          <Button icon="plus" @click="showCreateModal = true">Create first config</Button>
        </template>
      </EmptyState>
    </Panel>

    <!-- Config editor -->
    <template v-else>
      <Tabs v-model="activeTab" :items="tabItems" variant="line" />

      <!-- Resource rates tab -->
      <Panel v-if="activeTab === 'resources'" title="Gather resource modifiers">
        <template #actions>
          <div class="gv__presets">
            <span class="gv__presets-label">Presets:</span>
            <button
              v-for="preset in presets"
              :key="preset.value"
              class="gv__preset-btn"
              @click="applyPreset(preset.value)"
            >{{ preset.label }}</button>
          </div>
        </template>

        <div class="gv__rate-list">
          <div
            v-for="resource in gatherResources"
            :key="resource.key"
            class="gv__rate-row"
          >
            <label class="gv__rate-label">{{ resource.label }}</label>
            <input
              type="range"
              :value="getConfigValue(`GatherResourceModifiers.${resource.key}`, 1)"
              min="0.1"
              max="100"
              step="0.1"
              class="gv__slider"
              style="accent-color: var(--accent)"
              @input="setConfigValue(`GatherResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
            />
            <input
              type="number"
              :value="getConfigValue(`GatherResourceModifiers.${resource.key}`, 1)"
              min="0.1"
              max="1000"
              step="0.1"
              class="cc-num-input gv__rate-num"
              @input="setConfigValue(`GatherResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
            />
            <span class="gv__rate-unit">x</span>
          </div>
        </div>
      </Panel>

      <!-- Advanced tab -->
      <template v-else-if="activeTab === 'advanced'">
        <!-- Pickup -->
        <Panel title="Pickup resource modifiers" subtitle="Modify rates for resources picked up from the ground (small rocks, wood piles).">
          <div class="gv__rate-list">
            <div
              v-for="resource in pickupResources"
              :key="resource.key"
              class="gv__rate-row"
            >
              <label class="gv__rate-label">{{ resource.label }}</label>
              <input
                type="range"
                :value="getConfigValue(`PickupResourceModifiers.${resource.key}`, 1)"
                min="0.1"
                max="100"
                step="0.1"
                class="gv__slider"
                style="accent-color: var(--accent)"
                @input="setConfigValue(`PickupResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
              />
              <input
                type="number"
                :value="getConfigValue(`PickupResourceModifiers.${resource.key}`, 1)"
                min="0.1"
                max="1000"
                step="0.1"
                class="cc-num-input gv__rate-num"
                @input="setConfigValue(`PickupResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
              />
              <span class="gv__rate-unit">x</span>
            </div>
          </div>
        </Panel>

        <!-- Quarry -->
        <Panel title="Quarry resource modifiers" subtitle="Scale resource output from mining quarries.">
          <div class="gv__rate-list">
            <div
              v-for="resource in quarryResources"
              :key="resource.key"
              class="gv__rate-row"
            >
              <label class="gv__rate-label">{{ resource.label }}</label>
              <input
                type="range"
                :value="getConfigValue(`QuarryResourceModifiers.${resource.key}`, 1)"
                min="0.1"
                max="100"
                step="0.1"
                class="gv__slider"
                style="accent-color: var(--accent)"
                @input="setConfigValue(`QuarryResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
              />
              <input
                type="number"
                :value="getConfigValue(`QuarryResourceModifiers.${resource.key}`, 1)"
                min="0.1"
                max="1000"
                step="0.1"
                class="cc-num-input gv__rate-num"
                @input="setConfigValue(`QuarryResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
              />
              <span class="gv__rate-unit">x</span>
            </div>
          </div>
        </Panel>

        <!-- Excavator -->
        <Panel title="Excavator resource modifiers" subtitle="Scale resource output from the giant excavator.">
          <div class="gv__rate-list">
            <div
              v-for="resource in excavatorResources"
              :key="resource.key"
              class="gv__rate-row"
            >
              <label class="gv__rate-label">{{ resource.label }}</label>
              <input
                type="range"
                :value="getConfigValue(`ExcavatorResourceModifiers.${resource.key}`, 1)"
                min="0.1"
                max="100"
                step="0.1"
                class="gv__slider"
                style="accent-color: var(--accent)"
                @input="setConfigValue(`ExcavatorResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
              />
              <input
                type="number"
                :value="getConfigValue(`ExcavatorResourceModifiers.${resource.key}`, 1)"
                min="0.1"
                max="1000"
                step="0.1"
                class="cc-num-input gv__rate-num"
                @input="setConfigValue(`ExcavatorResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
              />
              <span class="gv__rate-unit">x</span>
            </div>
          </div>
        </Panel>

        <!-- Survey -->
        <Panel title="Survey charge resource modifiers" subtitle="Modify resource amounts from survey charge grenades.">
          <div class="gv__rate-list">
            <div
              v-for="resource in surveyResources"
              :key="resource.key"
              class="gv__rate-row"
            >
              <label class="gv__rate-label">{{ resource.label }}</label>
              <input
                type="range"
                :value="getConfigValue(`SurveyResourceModifiers.${resource.key}`, 1)"
                min="0.1"
                max="100"
                step="0.1"
                class="gv__slider"
                style="accent-color: var(--accent)"
                @input="setConfigValue(`SurveyResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
              />
              <input
                type="number"
                :value="getConfigValue(`SurveyResourceModifiers.${resource.key}`, 1)"
                min="0.1"
                max="1000"
                step="0.1"
                class="cc-num-input gv__rate-num"
                @input="setConfigValue(`SurveyResourceModifiers.${resource.key}`, Number(($event.target as HTMLInputElement).value))"
              />
              <span class="gv__rate-unit">x</span>
            </div>
          </div>
        </Panel>
      </template>
    </template>

    <!-- Create config modal -->
    <div v-if="showCreateModal" class="gv__modal-backdrop" @click.self="showCreateModal = false">
      <div class="gv__modal">
        <h2 class="gv__modal-title">New gather config</h2>
        <div class="gv__modal-body">
          <div class="gv__field">
            <label class="gv__field-label">Config name</label>
            <input
              v-model="newConfigName"
              type="text"
              class="cc-text-input"
              placeholder="e.g. 3x gather rates"
              @keydown.enter="handleCreateConfig"
            />
          </div>
          <div class="gv__field">
            <label class="gv__field-label">Description (optional)</label>
            <textarea
              v-model="newConfigDesc"
              rows="2"
              class="cc-textarea"
              placeholder="What is this config for?"
            />
          </div>
        </div>
        <div class="gv__modal-footer">
          <Button variant="ghost" @click="showCreateModal = false">Cancel</Button>
          <Button :disabled="!newConfigName.trim()" @click="handleCreateConfig">Create</Button>
        </div>
      </div>
    </div>

    <!-- Import from server modal -->
    <div v-if="showImportModal" class="gv__modal-backdrop" @click.self="showImportModal = false">
      <div class="gv__modal">
        <h2 class="gv__modal-title">Import from server</h2>
        <p class="gv__modal-desc">Import the current GatherManager config from your live server. This will create a new config profile.</p>
        <div class="gv__modal-body">
          <div class="gv__field">
            <label class="gv__field-label">Config name</label>
            <input
              v-model="importConfigName"
              type="text"
              class="cc-text-input"
              placeholder="e.g. Imported server config"
              @keydown.enter="handleImport"
            />
          </div>
        </div>
        <div class="gv__modal-footer">
          <Button variant="ghost" @click="showImportModal = false">Cancel</Button>
          <Button :disabled="!importConfigName.trim()" @click="handleImport">Import</Button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.gv { max-width: 960px; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; }

/* Page head */
.gv__head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
.gv__head-id { display: flex; align-items: center; gap: 12px; }
.gv__head-chip {
  width: 40px; height: 40px; flex: none; border-radius: var(--radius-md);
  display: flex; align-items: center; justify-content: center;
  color: var(--accent); background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px var(--accent-border);
}
.gv__title {
  font-size: var(--text-2xl); font-weight: 700; letter-spacing: -0.02em;
  color: var(--text-primary); margin-top: 3px;
}

/* Config action bar */
.gv__bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.gv__config-select {
  appearance: none; height: var(--control-h-md); padding: 0 11px;
  background: var(--surface-inset); color: var(--text-primary); border: 0;
  border-radius: var(--radius-md); box-shadow: var(--ring-default);
  font-family: var(--font-sans); font-size: var(--text-sm); cursor: pointer;
  min-width: 200px;
}
.gv__config-select:focus-visible { outline: none; box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.gv__no-configs { font-size: var(--text-sm); color: var(--text-muted); }
.gv__bar-delete { margin-left: auto; }

/* Loading */
.gv__loading { display: flex; align-items: center; justify-content: center; padding: 60px; }
.gv__spinner {
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--accent);
  border-top-color: transparent; animation: gv-spin 0.6s linear infinite;
}
@keyframes gv-spin { to { transform: rotate(360deg); } }

/* Presets */
.gv__presets { display: flex; align-items: center; gap: 6px; }
.gv__presets-label { font-size: var(--text-xs); color: var(--text-tertiary); }
.gv__preset-btn {
  height: 26px; padding: 0 10px; border: 0; border-radius: var(--radius-sm);
  background: var(--surface-raised-2); color: var(--text-secondary);
  font-size: var(--text-xs); font-weight: 600; cursor: pointer;
  box-shadow: var(--ring-default); transition: var(--transition-colors);
}
.gv__preset-btn:hover { background: var(--surface-active); color: var(--text-primary); }

/* Rate rows */
.gv__rate-list { display: flex; flex-direction: column; gap: 12px; }
.gv__rate-row { display: flex; align-items: center; gap: 12px; }
.gv__rate-label { font-size: var(--text-sm); color: var(--text-primary); width: 120px; flex: none; }
.gv__slider { flex: 1; cursor: pointer; }
.gv__rate-num {
  width: 72px; height: var(--control-h-sm); flex: none;
  text-align: center; font-family: var(--font-mono); font-variant-numeric: tabular-nums;
}
.gv__rate-unit { font-size: var(--text-xs); color: var(--text-tertiary); width: 12px; flex: none; }

/* Shared token inputs */
.cc-textarea {
  width: 100%; background: var(--surface-inset); color: var(--text-primary);
  border: 0; border-radius: var(--radius-md); box-shadow: var(--ring-default);
  padding: 9px 11px; font-family: var(--font-sans); font-size: var(--text-sm);
  resize: none; outline: 0; line-height: 1.5;
}
.cc-textarea:focus-visible { box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.cc-text-input {
  width: 100%; height: var(--control-h-md); background: var(--surface-inset); color: var(--text-primary);
  border: 0; border-radius: var(--radius-md); box-shadow: var(--ring-default);
  padding: 0 11px; font-family: var(--font-sans); font-size: var(--text-sm); outline: 0;
}
.cc-text-input:focus-visible { box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.cc-num-input {
  width: 100%; height: var(--control-h-md); background: var(--surface-inset); color: var(--text-primary);
  border: 0; border-radius: var(--radius-md); box-shadow: var(--ring-default);
  padding: 0 11px; font-family: var(--font-sans); font-size: var(--text-sm); outline: 0;
}
.cc-num-input:focus-visible { box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }

/* Fields */
.gv__field { display: flex; flex-direction: column; gap: 6px; }
.gv__field-label { font-size: var(--text-xs); font-weight: 600; color: var(--text-secondary); }

/* Modal */
.gv__modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.gv__modal {
  background: var(--surface-base); border-radius: var(--radius-lg); box-shadow: var(--ring-default);
  width: 100%; max-width: 440px; padding: 24px; display: flex; flex-direction: column; gap: 16px;
}
.gv__modal-title { font-size: var(--text-lg); font-weight: 700; color: var(--text-primary); }
.gv__modal-desc { font-size: var(--text-sm); color: var(--text-tertiary); line-height: 1.5; }
.gv__modal-body { display: flex; flex-direction: column; gap: 12px; }
.gv__modal-footer { display: flex; justify-content: flex-end; gap: 8px; }
</style>
