import { CMyCircuit } from '@/components/CMyCircuit'
import { computed, defineComponent, onMounted, PropType, ref, toRef } from 'vue'
import { useUserStore } from '@store/UserStore'
import { useCircuitStore } from '@store/CircuitStore'
import { useI18n } from '@/boot/i18n'
import { useQuasar } from 'quasar'
import { costanti } from '@costanti'
import { ICircuit, ISearchList, IUserFields } from 'model'
import { shared_consts } from '@/common/shared_vuejs'
import { tools } from '@store/Modules/tools'
import { CUserNonVerif } from '@/components/CUserNonVerif'
import { CMyCircuits } from '@/components/CMyCircuits'


export default defineComponent({
  name: 'CMyCircuits',
  components: { CMyCircuit, CUserNonVerif },
  emits: ['update:modelValue'],
  props: {
    modelValue: {
      type: Number,
      required: false,
      default: 0,
    },
    finder: {
      type: Boolean,
      required: true,
    },
    showfinder: {
      type: Boolean,
      required: false,
      default: true,
    },
    circuit: {
      type: Object as PropType<ICircuit | null>,
      required: false,
      default: null,
    },
    circuitname: {
      type: String,
      required: false,
      default: null,
    },
    visu: {
      type: Number,
      required: false,
      default: 0,
    },
  },
  setup(props, { emit }) {
    const userStore = useUserStore()
    const circuitStore = useCircuitStore()
    const $q = useQuasar()
    const { t } = useI18n()

    const username = ref('')

    const filtroutente = ref(<any[]>[])

    const listcircuitsfiltered = computed(() => {
      console.log('list modif')
      return updateListCircuit()
    })

    const myoptions = computed(() => {
      const mybutt = []
      mybutt.push({ label: t('mypages.follow_circuits') + ' (' + numMyCircuits.value + ')', value: costanti.MY_CIRCUITS })
      mybutt.push({ label: t('mypages.find_circuit'), value: costanti.FIND_CIRCUIT })


      if (numAskSentCircuits.value > 0 || props.modelValue === costanti.ASK_SENT_CIRCUIT)
        mybutt.push({
          label: t('mypages.request_sent') + ' (' + numAskSentCircuits.value + ')',
          value: costanti.ASK_SENT_CIRCUIT
        })


      return mybutt
    })


    const numMyCircuits = computed(() => {
      const arr = userStore.my.profile.mycircuits
      return (arr) ? arr.length : 0
    })

    const numAskSentCircuits = computed(() => {
      const arr = userStore.my.profile.asked_circuits
      return (arr) ? arr.length : 0
    })

    async function loadCircuits() {
      // Carica il profilo di quest'utente
      if (username.value) {
        filtroutente.value = await tools.loadCircuits()
      }
    }

    function updateListCircuit() {
      let arr: any[] = []
      try {
        if (props.modelValue === costanti.CIRCUITS) {
          arr = circuitStore.listcircuits
        } else if (props.modelValue === costanti.MY_CIRCUITS ) {
          arr = circuitStore.listcircuits.filter((circ: any) => userStore.my.profile.mycircuits.findIndex((rec: any) => circ.name === rec.circuitname) >= 0)
        } else if (props.modelValue === costanti.ASK_SENT_CIRCUIT) {
          arr = userStore.my.profile.asked_circuits
        }
      } catch (e) {
        arr = []
      }

      return arr
    }


    function mounted() {
      username.value = userStore.my.username
      loadCircuits()
    }

    function updateValue(val: number) {
      emit('update:modelValue', val)
    }

    onMounted(mounted)

    return {
      tools,
      costanti,
      shared_consts,
      filtroutente,
      listcircuitsfiltered,
      updateValue,
      myoptions,
      userStore,
      circuitStore,
    }
  }
})
