<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useFurnaceSplitterStore } from '@/stores/furnacesplitter'
import { useThemeGame } from '@/composables/useThemeGame'
import { useGameProfile } from '@/config/gameProfiles'
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 Switch from '@/components/ds/forms/Switch.vue'
import EmptyState from '@/components/ds/feedback/EmptyState.vue'

const store = useFurnaceSplitterStore()
const { activeGame } = useThemeGame()
const gameProfile = computed(() => useGameProfile(activeGame.value === 'all' ? 'rust' : activeGame.value))

const showCreateModal = ref(false)
const showImportModal = ref(false)
const newConfigName = ref('')
const newConfigDesc = ref('')
const importConfigName = ref('')

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 = false): 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()
}

// Furnace types with display names
const furnaceTypes = [
  { key: 'furnace', label: 'Small furnace', description: 'Standard furnace for smelting ores' },
  { key: 'furnace.large', label: 'Large furnace', description: 'Large furnace with more slots' },
  { key: 'campfire', label: 'Campfire', description: 'Basic campfire for cooking' },
  { key: 'refinery_small_deployed', label: 'Small oil refinery', description: 'Refines crude oil into low grade fuel' },
  { key: 'skull_fire_pit', label: 'Skull fire pit', description: 'Decorative fire pit for cooking' },
  { key: 'hobobarrel_static', label: 'Hobo barrel', description: 'Barrel fire for cooking' },
  { key: 'electricfurnace.deployed', label: 'Electric furnace', description: 'Electricity-powered furnace' },
]

// --- 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 FurnaceSplitter config to the server? This will overwrite the current 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 = ''
  }
}

// Helper: coerce getConfigValue result to boolean for Switch
function getBool(path: string, def: boolean): boolean {
  return !!getConfigValue(path, def)
}
</script>

<template>
  <div class="fsv">
    <!-- uMod-only guard: Furnace Splitter is an Oxide/uMod plugin -->
    <Panel v-if="gameProfile.mods !== 'umod'">
      <EmptyState
        icon="flame"
        title="Rust / uMod only"
        description="Furnace Splitter is only available for Rust (uMod/Oxide) servers."
      />
    </Panel>

    <template v-else>
    <!-- Page head -->
    <div class="fsv__head">
      <div class="fsv__head-id">
        <div class="fsv__head-chip">
          <Icon name="flame" :size="20" :stroke-width="2" />
        </div>
        <div>
          <div class="t-eyebrow">Plugin config</div>
          <h1 class="fsv__title">Furnace splitter</h1>
        </div>
      </div>
      <Button size="sm" icon="plus" @click="showCreateModal = true">New config</Button>
    </div>

    <!-- Config action bar -->
    <Panel>
      <div class="fsv__bar">
        <select
          v-if="store.configs.length > 0"
          :value="store.currentConfig?.id ?? ''"
          class="fsv__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="fsv__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="fsv__bar-delete"
          @click="handleDeleteConfig"
        >Delete</Button>
      </div>
    </Panel>

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

    <!-- Empty state -->
    <Panel v-else-if="!store.currentConfig">
      <EmptyState
        icon="flame"
        title="No FurnaceSplitter 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>
      <!-- Splitter settings -->
      <Panel title="Splitter settings">
        <div class="fsv__toggles">
          <div class="fsv__toggle-row">
            <div>
              <div class="fsv__toggle-label">Enabled</div>
              <div class="fsv__toggle-sub">Globally enable or disable furnace splitting</div>
            </div>
            <Switch
              :model-value="getBool('Enabled', true)"
              @update:model-value="v => setConfigValue('Enabled', v)"
            />
          </div>
        </div>
      </Panel>

      <!-- Per-furnace type settings -->
      <Panel title="Furnace type settings">
        <div class="fsv__furnace-list">
          <div
            v-for="furnace in furnaceTypes"
            :key="furnace.key"
            class="fsv__furnace-card"
          >
            <div class="fsv__furnace-head">
              <div>
                <div class="fsv__furnace-name">{{ furnace.label }}</div>
                <div class="fsv__furnace-desc">{{ furnace.description }}</div>
              </div>
              <Switch
                :model-value="getBool(`Furnaces.${furnace.key}.Enabled`, true)"
                @update:model-value="v => setConfigValue(`Furnaces.${furnace.key}.Enabled`, v)"
              />
            </div>

            <div class="fsv__furnace-fields">
              <div class="fsv__field">
                <label class="fsv__field-label">Default split stacks</label>
                <input
                  type="number"
                  class="cc-num-input"
                  :value="getConfigValue(`Furnaces.${furnace.key}.DefaultSplitCount`, 0)"
                  min="0"
                  placeholder="0 = fill all slots"
                  @input="setConfigValue(`Furnaces.${furnace.key}.DefaultSplitCount`, Number(($event.target as HTMLInputElement).value))"
                />
              </div>
              <div class="fsv__field">
                <label class="fsv__field-label">Fuel multiplier</label>
                <input
                  type="number"
                  step="0.1"
                  class="cc-num-input fsv__mono"
                  :value="getConfigValue(`Furnaces.${furnace.key}.FuelMultiplier`, 1.0)"
                  min="0"
                  @input="setConfigValue(`Furnaces.${furnace.key}.FuelMultiplier`, Number(($event.target as HTMLInputElement).value))"
                />
              </div>
            </div>
          </div>
        </div>
      </Panel>

      <!-- Permission -->
      <Panel title="Permission">
        <p class="fsv__perm-text">
          The permission <code class="fsv__code">furnacesplitter.use</code> controls which players can use the furnace splitting feature. Assign this permission via your Oxide permission system.
        </p>
      </Panel>
    </template>

    <!-- Create config modal -->
    <div v-if="showCreateModal" class="fsv__modal-backdrop" @click.self="showCreateModal = false">
      <div class="fsv__modal">
        <h2 class="fsv__modal-title">New FurnaceSplitter config</h2>
        <div class="fsv__modal-body">
          <div class="fsv__field">
            <label class="fsv__field-label">Config name</label>
            <input
              v-model="newConfigName"
              type="text"
              class="cc-text-input"
              placeholder="e.g. Default furnace settings"
              @keydown.enter="handleCreateConfig"
            />
          </div>
          <div class="fsv__field">
            <label class="fsv__field-label">Description (optional)</label>
            <textarea
              v-model="newConfigDesc"
              rows="2"
              class="cc-textarea"
              placeholder="What is this config for?"
            />
          </div>
        </div>
        <div class="fsv__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="fsv__modal-backdrop" @click.self="showImportModal = false">
      <div class="fsv__modal">
        <h2 class="fsv__modal-title">Import from server</h2>
        <p class="fsv__modal-desc">Import the current FurnaceSplitter config from your live server. This will create a new config profile.</p>
        <div class="fsv__modal-body">
          <div class="fsv__field">
            <label class="fsv__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="fsv__modal-footer">
          <Button variant="ghost" @click="showImportModal = false">Cancel</Button>
          <Button :disabled="!importConfigName.trim()" @click="handleImport">Import</Button>
        </div>
      </div>
    </div>
    </template>
  </div>
</template>

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

/* Page head */
.fsv__head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
.fsv__head-id { display: flex; align-items: center; gap: 12px; }
.fsv__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);
}
.fsv__title {
  font-size: var(--text-2xl); font-weight: 700; letter-spacing: -0.02em;
  color: var(--text-primary); margin-top: 3px;
}

/* Config action bar */
.fsv__bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.fsv__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;
}
.fsv__config-select:focus-visible { outline: none; box-shadow: inset 0 0 0 1px var(--accent), var(--glow-accent-sm); }
.fsv__no-configs { font-size: var(--text-sm); color: var(--text-muted); }
.fsv__bar-delete { margin-left: auto; }

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

/* Toggles */
.fsv__toggles { display: flex; flex-direction: column; }
.fsv__toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--border-subtle);
}
.fsv__toggle-row:last-child { border-bottom: 0; padding-bottom: 0; }
.fsv__toggle-row:first-child { padding-top: 0; }
.fsv__toggle-label { font-size: var(--text-sm); font-weight: 500; color: var(--text-primary); }
.fsv__toggle-sub { font-size: var(--text-xs); color: var(--text-tertiary); margin-top: 2px; }

/* Furnace cards */
.fsv__furnace-list { display: flex; flex-direction: column; gap: 12px; }
.fsv__furnace-card {
  background: var(--surface-raised-2); border-radius: var(--radius-md);
  box-shadow: var(--ring-default); padding: 14px; display: flex; flex-direction: column; gap: 12px;
}
.fsv__furnace-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.fsv__furnace-name { font-size: var(--text-sm); font-weight: 500; color: var(--text-primary); }
.fsv__furnace-desc { font-size: var(--text-xs); color: var(--text-tertiary); margin-top: 2px; }
.fsv__furnace-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 500px) { .fsv__furnace-fields { grid-template-columns: 1fr; } }

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

/* Permission text */
.fsv__perm-text { font-size: var(--text-sm); color: var(--text-secondary); line-height: 1.6; }
.fsv__code {
  font-family: var(--font-mono); font-size: var(--text-xs); font-variant-numeric: tabular-nums;
  background: var(--surface-raised-2); color: var(--text-primary); padding: 1px 6px;
  border-radius: var(--radius-sm); box-shadow: var(--ring-default);
}
.fsv__mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* 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); }

/* Modal */
.fsv__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;
}
.fsv__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;
}
.fsv__modal-title { font-size: var(--text-lg); font-weight: 700; color: var(--text-primary); }
.fsv__modal-desc { font-size: var(--text-sm); color: var(--text-tertiary); line-height: 1.5; }
.fsv__modal-body { display: flex; flex-direction: column; gap: 12px; }
.fsv__modal-footer { display: flex; justify-content: flex-end; gap: 8px; }
</style>
