import { CMyFieldDb } from '@/components/CMyFieldDb'
import { CMyFieldRec } from '@/components/CMyFieldRec'
import { CTitleBanner } from '@/components/CTitleBanner'
import { CProfile } from '@/components/CProfile'
import { CLabel } from '@/components/CLabel'
import { CCopyBtn } from '@/components/CCopyBtn'
import { CSkill } from '@/components/CSkill'
import { CDateTime } from '@/components/CDateTime'
import { CMyGroup } from '@/components/CMyGroup'
import { CMyCircuit } from '@/components/CMyCircuit'
import { CContactUser } from '@src/components/CContactUser'
import { CNotifAtTop } from '@src/components/CNotifAtTop'
import { CTimeAgo } from '@src/components/CTimeAgo'
import { CSendCoins } from '@/components/CSendCoins'
import { CMyUser } from '@/components/CMyUser'
import { CCheckIfIsLogged } from '@/components/CCheckIfIsLogged'
import { CUserNonVerif } from '@/components/CUserNonVerif'
import { tools } from '@store/Modules/tools'
import { computed, defineComponent, onMounted, ref, watch } from 'vue'
import { useUserStore } from '@store/UserStore'
import { useRoute, useRouter } from 'vue-router'
import { useGlobalStore } from '@store/globalStore'
import { useI18n } from '@/boot/i18n'
import { toolsext } from '@store/Modules/toolsext'
import { useQuasar } from 'quasar'
import { costanti } from '@costanti'
import { IBookmark, ICircuit, IFavorite, IMyCircuit, IMyGroup, IUserFields } from 'model'
import { shared_consts } from '@/common/shared_vuejs'
import { static_data } from '@/db/static_data'
import { fieldsTable } from '@store/Modules/fieldsTable'
import { useNotifStore } from '@store/NotifStore'
import MixinUsers from '@/mixins/mixin-users'


export default defineComponent({
  name: 'CMyActivities',
  components: {
    CProfile, CTitleBanner, CMyFieldDb, CSkill, CDateTime, CCopyBtn, CUserNonVerif, CMyFieldRec, CMyUser,
    CMyGroup, CLabel, CMyCircuit, CSendCoins, CNotifAtTop, CCheckIfIsLogged,
    CTimeAgo, CContactUser
  },
  props: {
  },
  setup(props) {
    const userStore = useUserStore()
    const globalStore = useGlobalStore()
    const $route = useRoute()
    const $q = useQuasar()
    const { t } = useI18n()

    const site = ref(globalStore.site)

    const { getRefLink } = MixinUsers()

    const animation = ref('fade')
    const mytab = ref('my')

    const username = computed(() => $route.params.username ? $route.params.username.toString() : userStore.my.username)
    const idnotif = computed(() => $route.query.idnotif ? $route.query.idnotif.toString() : '')

    const filtroutente = ref(<any[]>[])
    const showPic = ref(false)
    const caricato = ref(false)

    const myuser = ref(<IUserFields | null>null)

    const actualcard = ref('mygoods')

    const notifStore = useNotifStore()

    const mycards = computed(() => {
      return costanti.MAINCARDS.filter((rec: any) => rec.table)
    })
    const optionsMainCards = ref({})

    const listgroupsfiltered = ref(<IMyGroup[]>[])
    const listcircuitsfiltered = ref(<IMyCircuit[]>[])


    function profile() {
      return userStore.my.profile
    }

    function myusername() {
      return userStore.my.username
    }

    async function loadProfile() {
      console.log('loadProfile...', username.value)
      try {
        // Carica il profilo di quest'utente
        if (username.value) {
          await userStore.loadUserProfile({ username: username.value, idnotif: idnotif.value }).then((ris) => {
            // console.log('loadUserProfile = ', ris)
            myuser.value = ris
            if (myuser.value) {
              filtroutente.value = [{ userId: myuser.value._id }]
              notifStore.setAsRead(idnotif.value)

              try {
                listgroupsfiltered.value = globalStore.mygroups.filter((grp: IMyGroup) => myuser.value!.profile.mygroups.findIndex((rec: IMyGroup) => rec.groupname === grp.groupname) >= 0)
              } catch (e) {
                listgroupsfiltered.value = []
              }

              try {
                listcircuitsfiltered.value = myuser.value.profile.mycircuits
              } catch (e) {
                listcircuitsfiltered.value = []
              }

            }
          })

        }
        caricato.value = true

        optionsMainCards.value = tools.getoptionsMainCards(true)
      } catch (e) {
        console.error('ERR loadProfile', e)
      }
    }

    watch(() => username.value, (to: any, from: any) => {
      loadProfile()
    })

    watch(() => actualcard.value, (to: any, from: any) => {
      loadProfile()
    })

    function mounted() {
      loadProfile()
    }

    function getImgUser() {
      if (myuser.value)
        return userStore.getImgByProfile(myuser.value)
      else
        return ''
    }

    function checkifShow(col: string) {
      //++Todo: checkifShow Permessi !
      return true
    }

    function getLinkUserTelegram() {

      if (myuser.value) {
        if (!!myuser.value.profile.username_telegram) {
          return 'https://t.me/' + myuser.value.profile.username_telegram
        }
      } else {
        return ''
      }
    }

    function getLinkWebSite() {
      if (myuser.value) {
        let site = myuser.value.profile.website!
        if (site) {
          if (!site.startsWith('http')) {
            site = 'https://' + site
          }
        }
        return site
      } else {
        return ''
      }
    }

    function isMyRecord(username: string) {
      return username === userStore.my.username
    }

    function getlinkpage() {
      if (myuser.value)
        return self.location.host + '/attivita/' + myuser.value.username
      else
        return ''
    }

    function filtrofavorite(table: string) {
      const tab = tools.getNumTabByTable(table)
      if (myuser.value && myuser.value.profile.favorite) {
        let arrfav = myuser.value.profile.favorite.filter((rec: IBookmark) => rec.tab === tab)
        if (arrfav)
          return myuser.value.profile.favorite ? [{ _id: { $in: arrfav.map((rec: any) => rec.id) } }] : []
      }
      return []
    }
    function filtrobookmark(table: string) {
      const tab = tools.getNumTabByTable(table)
      if (myuser.value && myuser.value.profile.bookmark) {
        let arrfav = myuser.value.profile.bookmark.filter((rec: IBookmark) => rec.tab === tab)
        if (arrfav)
          return myuser.value.profile.bookmark ? [{ _id: { $in: arrfav.map((rec: any) => rec.id) } }] : []
      }
      return []
    }

    onMounted(mounted)

    return {
      username,
      profile,
      tools,
      costanti,
      myuser,
      shared_consts,
      getImgUser,
      checkifShow,
      getLinkUserTelegram,
      getLinkWebSite,
      filtroutente,
      filtrofavorite,
      filtrobookmark,
      showPic,
      myusername,
      userStore,
      t,
      static_data,
      animation,
      isMyRecord,
      getRefLink,
      fieldsTable,
      mycards,
      actualcard,
      caricato,
      listgroupsfiltered,
      idnotif,
      site,
      listcircuitsfiltered,
      optionsMainCards,
      getlinkpage,
      mytab,
    }
  }
})
