<i18n>
{
  "en": {
    "name": "Name",
    "description": "Description",
    "timeout": "Timeout",
    "expertMode": "Expert Mode",
    "script": "iPXE Script",
    "titleNew": "Create config",
    "titleExisting": "Edit config",
    "entries": "Entries",
    "customName": "Custom Name",
    "keyBind": "Key Bind",
    "defaultEntry": "Mark this entry as default",
    "entry": "Entry",
    "spacer": "[Spacer]"
  },
  "de": {
    "name": "Name",
    "description": "Beschreibung",
    "timeout": "Timeout",
    "expertMode": "Expertenmodus",
    "script": "iPXE Skript",
    "titleNew": "Konfiguration erstellen",
    "titleExisting": "Konfiguration bearbeiten",
    "entries": "Einträge",
    "customName": "Alternativer Name",
    "keyBind": "Tastenbelegung",
    "defaultEntry": "Diesen Eintrag als Standard markieren",
    "entry": "Eintrag",
    "spacer": "[Abstandshalter]"
  }
}
</i18n>

<template>
  <v-card style="overflow: hidden">
    <v-card-title primary-title class="dialog-title elevation-3">
      <div class="headline">{{ dialog.info.id ? $t('titleExisting') : $t('titleNew') }}</div>
    </v-card-title>
    <v-card-text style="height: 100%">
      <v-layout wrap>
        <v-flex>
          <v-text-field prepend-icon="label" :label="$t('name')" color="primary" v-model="name"></v-text-field>
        </v-flex>
        <v-flex class="text-xs-center">
          <div v-if="!expertMode" style="display: inline-block; max-width: 180px">
            <v-text-field prepend-icon="timer" :label="$t('timeout')" color="primary" v-model="timeout" mask="######" suffix="ms"></v-text-field>
          </div>
        </v-flex>
        <v-flex class="text-xs-center">
          <div style="display: inline-block"><v-switch v-model="expertMode" color="primary" :label="$t('expertMode')"></v-switch></div>
        </v-flex>
      </v-layout>
      <v-textarea prepend-icon="description" rows="1" :label="$t('description')" color="primary" v-model="description"></v-textarea>
      <div v-if="expertMode">
        <div class="body-1 script-heading"><v-icon>code</v-icon><span>{{ $t('script') }}</span></div>
        <codemirror class="script-editor" ref="editor" v-model="script"></codemirror>
      </div>
      <div v-else class="text-xs-right">
        <v-subheader>{{ $t('entries') }}</v-subheader>
        <v-list>
          <draggable v-model="items" :options="{handle:'.handle'}">
            <v-list-tile v-for="item in items" :key="item.entry.id" @click.stop>
              <v-list-tile-action class="handle">
                <v-icon>drag_handle</v-icon>
              </v-list-tile-action>
              <v-list-tile-action>
                <v-tooltip top open-delay="800">
                  <v-radio-group slot="activator" v-model="defaultEntry">
                    <v-radio v-if="item.entry.id" color="primary" :value="item.entry.id"></v-radio>
                  </v-radio-group>
                <span>{{ $t('defaultEntry') }}</span></v-tooltip>
              </v-list-tile-action>
              <v-list-tile-content class="item-content">
                <v-select return-object style="max-width: 280px" item-text="name" menu-props="offsetY" :label="$t('entry')" color="primary" v-model="item.entry"
                :items="item.entry.id ? [item.entry, ...availableEntries] : availableEntries"></v-select>
                <v-text-field v-if="item.entry.id" class="custom-name-input" :label="$t('customName')" color="primary" v-model="item.customName"></v-text-field>
                <v-text-field v-if="item.entry.id" style="max-width: 60px" prepend-inner-icon="keyboard" color="primary" v-model="item.keyBind" maxlength="1"></v-text-field>
              </v-list-tile-content>
              <v-list-tile-action>
                <v-btn @click="removeItem(item)" icon><v-icon>clear</v-icon></v-btn>
              </v-list-tile-action>
            </v-list-tile>
          </draggable>
        </v-list>
        <v-btn @click="addItem" color="success" fab small><v-icon dark>add</v-icon></v-btn>
      </div>
    </v-card-text>
    <v-divider></v-divider>
    <v-card-actions>
      <v-spacer></v-spacer>
      <v-btn flat="flat" @click="setDialog({ show: false })">{{ $t('cancel') }}</v-btn>
      <v-btn :color="dialog.info.id ? 'primary' : 'success'" @click="saveConfig">{{ dialog.info.id ? $t('save') : $t('create') }}</v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
import axios from 'axios'
import draggable from 'vuedraggable'
import ComponentSearchTable from '@/components/ComponentSearchTable'
import { mapState } from 'vuex'

export default {
  name: 'ConfiguratorModuleConfig',
  components: {
    draggable,
    ComponentSearchTable
  },
  data () {
    return {
      name: '',
      description: '',
      defaultEntry: null,
      timeout: '',
      script: '',
      expertMode: false,
      items: [],
      interval: {
        id: null,
        counter: 0
      }
    }
  },
  computed: {
    ...mapState('configurator', ['dialog', 'entries']),
    availableEntries () {
      var selectedEntryIds = this.items.map(x => x.entry.id)
      return this.entries.filter(x => !selectedEntryIds.includes(x.id))
    }
  },
  watch: {
    dialog: {
      immediate: true,
      deep: true,
      async handler (value) {
        if (value.type === 'config' && value.show) {
          this.name = value.info.name || ''
          this.description = value.info.description || ''
          this.defaultEntry = value.info.defaultEntry
          this.timeout = value.info.timeout
          this.script = value.info.script || ''
          if (this.script) this.expertMode = true
          else this.expertMode = false
          this.items = []
          var result = await axios.get('/api/configurator/configs/' + value.info.id + '/entries')
          this.items = result.data.map(x => ({
            entry: { id: x.id, name: x.name, script: x.script },
            customName: x.config_x_entry.customName,
            keyBind: x.config_x_entry.keyBind
          }))
          this.interval.id = setInterval(this.refreshEditor, 50)
          this.interval.counter = 0
        }
      }
    }
  },
  methods: {
    setDialog (data) {
      this.$store.commit('configurator/setDialog', data)
    },
    addItem () {
      this.items.push({ entry: {} })
    },
    removeItem (item) {
      this.items.splice(this.items.indexOf(item), 1)
    },
    async saveConfig () {
      let url = '/api/configurator/configs'
      if (this.dialog.info.id !== undefined) url += '/' + this.dialog.info.id
      await axios.post(url, {
        data: {
          name: this.name,
          description: this.description,
          defaultEntry: this.defaultEntry,
          timeout: this.timeout,
          script: this.script
        },
        entries: this.items.map(x => ({
          id: x.entry.id,
          customName: x.customName,
          keyBind: x.keyBind
        }))
      })
      this.$store.dispatch('configurator/loadData')
      this.setDialog({ show: false })
    },
    refreshEditor () {
      this.interval.counter++
      if (this.$refs.editor) this.$refs.editor.codemirror.refresh()
      if (this.interval.counter >= 15) clearInterval(this.interval.id)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.dialog-title {
  z-index: 1;
}
.item-content {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
}
.custom-name-input {
  margin-left: 20px;
  margin-right: 20px;
}

.script-heading {
  display: flex;
  align-items: center;
}

.script-heading > span {
  margin-left: 9px;
}

.script-editor {
  margin-top: 8px;
}

.script-editor >>> .CodeMirror {
  height: calc(90vh - 340px);
}

</style>
