<template lang="pug">
  v-container
    v-switch(v-model='enable_federation'
      :label="$t('admin.enable_federation')"
      persistent-hint
      inset
      :hint="$t('admin.enable_federation_help')"
    )

    template(v-if='enable_federation')

      v-switch.mt-4(v-model='enable_resources'
        :label="$t('admin.enable_resources')"
        :hint="$t('admin.enable_resources_help')"
        persistent-hint inset)

      v-switch.mt-4(v-model='hide_boosts'
        :label="$t('admin.hide_boost_bookmark')"
        :hint="$t('admin.hide_boost_bookmark_help')"
        persistent-hint inset)

      //- div.mt-4 {{$t('admin.instance_name')}}
      v-text-field.mt-5(v-model='instance_name'
        :label="$t('admin.instance_name')"
        :hint="`${$t('admin.instance_name_help')} (@${instance_name}@${settings.baseurl|url2host})`"
        placeholder='Instance name' persistent-hint
        @blur='save("instance_name", instance_name)')

    v-switch.mt-4(v-model='enable_trusted_instances'
      :label="$t('admin.enable_trusted_instances')"
      persistent-hint inset
      :hint="$t('admin.trusted_instances_help')")

    template(v-if='enable_trusted_instances')
      v-text-field.mt-4(v-model='instance_place'
        :label="$t('admin.instance_place')"
        persistent-hint
        :hint="$t('admin.instance_place_help')"
        @blur='save("instance_place", instance_place)'
      )

      //- div.mt-4 {{$t('admin.add_trusted_instance')}}
      v-text-field.mt-4(v-model='instance_url'
        :full-width='false'
        persistent-hint
        :hint="$t('admin.add_trusted_instance')"
        :label="$t('common.url')"
        append-outer-icon="mdi-send"
        @click:append-outer='createTrustedInstance'
      )

      v-data-table.mt-4(
        :headers='headers'
        :items='settings.trusted_instances'
      )
        //- el-table-column(:label="$t('common.name')")
        //-   template(slot-scope='data')
        //-     span {{data.row.name}}
        //- el-table-column(:label="$t('common.url')")
        //-   template(slot-scope='data')
        //-     span {{data.row.url}}
        //- el-table-column(:label="$t('common.place')")
        //-   template(slot-scope='data')
        //-     span {{data.row.label}}
        //- el-table-column(:label="$t('common.actions')")
        //-   template(slot-scope='data')
        //-     el-button(size='mini'
        //-       type='danger'
        //-       @click='deleteInstance(data.row)') {{$t('admin.delete_user')}}

</template>
<script>
import { mapActions, mapState } from 'vuex'
import { Message, MessageBox } from 'element-ui'
import axios from 'axios'

export default {
  name: 'Federation',
  data ({ $store, $options }) {
    return {
      instance_url: '',
      instance_name: $store.state.settings.instance_name,
      instance_place: $store.state.settings.instance_place,
      url2host: $options.filters.url2host
    }
  },
  computed: {
    ...mapState(['settings']),
    enable_federation: {
      get () { return this.settings.enable_federation },
      set (value) { this.setSetting({ key: 'enable_federation', value }) }
    },
    enable_resources: {
      get () { return this.settings.enable_resources },
      set (value) { this.setSetting({ key: 'enable_resources', value }) }
    },
    hide_boosts: {
      get () { return this.settings.hide_boosts },
      set (value) { this.setSetting({ key: 'hide_boosts', value }) }
    },
    enable_trusted_instances: {
      get () { return this.settings.enable_trusted_instances },
      set (value) { this.setSetting({ key: 'enable_trusted_instances', value }) }
    }
  },
  mounted () {
    console.error(this.$options.filters)
  },
  methods: {
    ...mapActions(['setSetting']),
    async createTrustedInstance () {
      try {
        const instance = await axios.get(`${this.instance_url}/.well-known/nodeinfo/2.1`)
        this.setSetting({
          key: 'trusted_instances',
          value: this.settings.trusted_instances.concat({
            url: this.instance_url,
            name: instance.data.metadata.nodeName,
            label: instance.data.metadata.nodeLabel
          })
        })
        this.instance_url = ''
      } catch (e) {
        Message({
          showClose: true,
          type: 'error',
          message: e
        })
      }
    },
    deleteInstance (instance) {
      MessageBox.confirm(this.$t('admin.delete_trusted_instance_confirm'),
        this.$t('common.confirm'), {
          confirmButtonText: this.$t('common.ok'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'error'
        }
      ).then(() => {
        this.setSetting({
          key: 'trusted_instances',
          value: this.settings.trusted_instances.filter(i => i.url !== instance.url)
        })
      })
    },
    save (key, value) {
      if (this.settings[key] !== value) {
        this.setSetting({ key, value })
      }
    }
  }
}
</script>
