<template>
  <div>
    <!-- Content -->
    <h4 :class="headingClass">Inhalt</h4>
    <div :class="rowClass"><label :class="labelClass">Quelle</label>
      <select :class="inputClass" v-model.number="widget.textSrc" @change="handleTextSrcChange">
        <optgroup v-for="group in groupedSources(sourceOptions.label)" :key="group.label" :label="group.label">
          <option v-for="opt in group.values" :key="opt" :value="opt">{{ textSources[opt] }}</option>
        </optgroup>
      </select>
    </div>
    <div v-if="widget.textSrc === 0" :class="rowClass">
      <label :class="labelClass">Text</label><input :class="inputClass" type="text" v-model="widget.text">
    </div>
    <template v-else>
      <div :class="rowClass"><label :class="labelClass">Format</label><input :class="inputClass" type="text" v-model="widget.text"></div>
      <div v-if="widget.textSrc < 11" :class="rowClass"><label :class="labelClass">KNX Objekt</label>
        <select :class="inputClass" v-model.number="widget.knxAddr">
          <option :value="0">-- Waehlen --</option>
          <option v-for="addr in store.knxAddresses" :key="`${addr.addr}-${addr.index}`" :value="addr.addr">
            GA {{ addr.addrStr }} (GO{{ addr.index }})
          </option>
        </select>
      </div>
    </template>

    <!-- Typography -->
    <h4 :class="headingClass">Typo</h4>
    <div :class="rowClass"><label :class="labelClass">Schriftgr.</label>
      <select :class="inputClass" v-model.number="widget.fontSize">
        <option v-for="(size, idx) in fontSizes" :key="idx" :value="idx">{{ size }}</option>
      </select>
    </div>

    <!-- Icon -->
    <h4 :class="headingClass">Icon</h4>
    <div :class="rowClass">
      <label :class="labelClass">Icon</label>
      <button :class="iconSelectClass" @click="$emit('open-icon-picker')">
        <span v-if="widget.iconCodepoint" class="material-symbols-outlined text-[20px]">{{ String.fromCodePoint(widget.iconCodepoint) }}</span>
        <span v-else>Kein Icon</span>
      </button>
      <button v-if="widget.iconCodepoint" :class="iconRemoveClass" @click="widget.iconCodepoint = 0">x</button>
    </div>
    <template v-if="widget.iconCodepoint">
      <div :class="rowClass">
        <label :class="labelClass">Position</label>
        <select :class="inputClass" v-model.number="widget.iconPosition">
          <option :value="0">Links</option>
          <option :value="1">Rechts</option>
          <option :value="2">Oben</option>
          <option :value="3">Unten</option>
        </select>
      </div>
      <div :class="rowClass">
        <label :class="labelClass">Icon-Gr.</label>
        <select :class="inputClass" v-model.number="widget.iconSize">
          <option v-for="(size, idx) in fontSizes" :key="idx" :value="idx">{{ size }}</option>
        </select>
      </div>
      <div :class="rowClass">
        <label :class="labelClass">Abstand</label>
        <input :class="inputClass" type="number" v-model.number="widget.iconGap" min="0" max="50">
      </div>
    </template>

    <!-- Style -->
    <h4 :class="headingClass">Stil</h4>
    <div :class="rowClass"><label :class="labelClass">Textfarbe</label><input :class="colorInputClass" type="color" v-model="widget.textColor"></div>
    <div :class="rowClass"><label :class="labelClass">Hintergrund</label><input :class="colorInputClass" type="color" v-model="widget.bgColor"></div>
    <div :class="rowClass"><label :class="labelClass">Deckkraft</label><input :class="inputClass" type="number" min="0" max="255" v-model.number="widget.bgOpacity"></div>
    <div :class="rowClass"><label :class="labelClass">Radius</label><input :class="inputClass" type="number" v-model.number="widget.radius"></div>
    <h4 :class="headingClass">Rand</h4>
    <div :class="rowClass"><label :class="labelClass">Breite</label><input :class="inputClass" type="number" min="0" max="32" v-model.number="widget.borderWidth"></div>
    <div :class="rowClass"><label :class="labelClass">Farbe</label><input :class="colorInputClass" type="color" v-model="widget.borderColor"></div>
    <div :class="rowClass"><label :class="labelClass">Deckkraft</label><input :class="inputClass" type="number" min="0" max="255" v-model.number="widget.borderOpacity"></div>

    <!-- Shadow -->
    <h4 :class="headingClass">Schatten</h4>
    <div :class="rowClass"><label :class="labelClass">Aktiv</label><input class="accent-[var(--accent)]" type="checkbox" v-model="widget.shadow.enabled"></div>
    <div :class="rowClass"><label :class="labelClass">X</label><input :class="inputClass" type="number" v-model.number="widget.shadow.x"></div>
    <div :class="rowClass"><label :class="labelClass">Y</label><input :class="inputClass" type="number" v-model.number="widget.shadow.y"></div>
    <div :class="rowClass"><label :class="labelClass">Blur</label><input :class="inputClass" type="number" v-model.number="widget.shadow.blur"></div>
    <div :class="rowClass"><label :class="labelClass">Farbe</label><input :class="colorInputClass" type="color" v-model="widget.shadow.color"></div>
  </div>
</template>

<script setup>
import { useEditorStore } from '../../../stores/editor';
import { sourceOptions, textSources, textSourceGroups, fontSizes, defaultFormats } from '../../../constants';
import { rowClass, labelClass, inputClass, headingClass, iconSelectClass, iconRemoveClass, colorInputClass } from '../shared/styles';

const props = defineProps({
  widget: { type: Object, required: true }
});

defineEmits(['open-icon-picker']);

const store = useEditorStore();

function groupedSources(options) {
  const allowed = new Set(options || []);
  return textSourceGroups
    .map((group) => ({
      label: group.label,
      values: group.values.filter((value) => allowed.has(value))
    }))
    .filter((group) => group.values.length > 0);
}

function handleTextSrcChange() {
  const newSrc = props.widget.textSrc;
  if (newSrc > 0 && defaultFormats[newSrc]) {
    props.widget.text = defaultFormats[newSrc];
  }
}
</script>
