import {
  computed,
  defineComponent, onBeforeMount, onBeforeUnmount, onMounted, ref, toRef, toRefs, watch,
} from 'vue'

import { tools } from '@tools'
import { costanti } from '@costanti'
import { useGlobalStore } from '@store/globalStore'
import { useUserStore } from '@store/UserStore'
import { useI18n } from 'vue-i18n'

export default defineComponent({
  name: 'CTestServer',
  props: {
  },
  components: {
  },
  setup(props, { attrs, slots, emit }) {
    const { t } = useI18n()
    const globalStore = useGlobalStore()
    const userStore = useUserStore()

    const testServer = ref({})

    const myrecfiltertoggle = ref(0)

    const SERVER_TEST = ref([
      { key: 1, label: 'Test', host: 'https://test.freeplanet.app:3001' },
      { key: 2, label: 'Produzione', host: tools.getServerHost() },
      { key: 3, label: 'Locale', host: 'https://localhost:3000' },
      { key: 4, label: 'Host IP', host: globalStore.site.host_ip },
      { key: 5, label: 'Host', host: globalStore.site.host },
      { key: 6, label: 'Host Api', host: globalStore.site.host_api },
      { key: 7, label: 'Host Api IP', host: globalStore.site.host_api_ip },
    ])

    watch(() =>
      myrecfiltertoggle.value, (value: any, oldValue: any) => {
        if (value) {
          const trovato = SERVER_TEST.value.find((rec: any) => rec.key === value)
          if (trovato) {
            globalStore.serverHost = trovato.host
            ricarica()
          }
        }
      },
    )
    const getoptions = computed(() => {
      const arr = []
      for (const rec of SERVER_TEST.value) {
        arr.push({ label: rec.label, value: rec.key })
      }

      return arr
    })

    function mounted() {
      ricarica()

      globalStore.getStatSite()
    }

    async function ricarica() {
      testServer.value = {}
      testServer.value = await globalStore.loadPageTest()
    }

    onMounted(mounted)

    return {
      t,
      tools,
      getoptions,
      costanti,
      testServer,
      ricarica,
      myrecfiltertoggle,
    }
  },
})
