import { CMyGroup } from '@src/components/CMyGroup'
import type { PropType } from 'vue';
import { computed, defineComponent, onMounted, ref, toRef } from 'vue'
import { useUserStore } from '@store/UserStore'
import { useI18n } from 'vue-i18n'
import { useQuasar } from 'quasar'
import { costanti } from '@costanti'
import type { IMyGroup } from 'model';
import { ISearchList, IUserFields } from 'model'
import { shared_consts } from '@src/common/shared_vuejs'
import { tools } from '@tools'
import { CUserNonVerif } from '@src/components/CUserNonVerif'


export default defineComponent({
  name: 'CMyGroups',
  components: { CMyGroup, CUserNonVerif },
  emits: ['update:modelValue'],
  props: {
    modelValue: {
      type: Number,
      required: false,
      default: 0,
    },
    finder: {
      type: Boolean,
      required: true,
    },
    showBarSelection: {
      type: Boolean,
      required: false,
      default: true,
    },
    mygrp: {
      type: Object as PropType<IMyGroup | null>,
      required: false,
      default: null,
    },
    mygroupname: {
      type: String,
      required: false,
      default: null,
    },
    visu: {
      type: Number,
      required: false,
      default: 0,
    },
    circuitname: {
      type: String,
      required: false,
      default: '',
    },
  },
  setup(props, { emit }) {
    const userStore = useUserStore()
    const $q = useQuasar()
    const { t } = useI18n()

    const username = ref('')

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

    const listgroupsfiltered = computed(() => {
      let arr: any[] = []
      try {
        if (props.modelValue === costanti.GROUPS) {
          arr = userStore.my.profile.mygroups
        } else if (props.modelValue === costanti.MY_GROUPS || props.modelValue === costanti.USER_GROUPS) {
          arr = userStore.groups.filter((grp: IMyGroup) => userStore.my.profile.mygroups.findIndex((rec: IMyGroup) => rec.groupname === grp.groupname) >= 0)
        } else if (props.modelValue === costanti.MANAGE_GROUPS) {
          arr = userStore.my.profile.manage_mygroups
        } else if (props.modelValue === costanti.ASK_SENT_GROUP) {
          arr = userStore.my.profile.asked_groups
        }
      } catch (e) {
        arr = []
      }

      return arr
    })

    const myoptions = computed(() => {
      const mybutt = []
      mybutt.push({ label: t('mypages.find_group'), value: costanti.FIND_GROUP })
      mybutt.push({ label: t('mypages.follow_groups') + ' (' + numMyGroups.value + ')', value: costanti.MY_GROUPS })
      // mybutt.push({ label: t('mypages.manage_my_groups') + ' (' + numManageGroups.value + ')', value: costanti.MANAGE_GROUPS })

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

      return mybutt
    })

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

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

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

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

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

      if (props.finder)
        loadGroups()

    }

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

    onMounted(mounted)

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