import type { PropType } from 'vue';
import { defineComponent, onMounted, ref, watch } from 'vue'
import { useUserStore } from '@store/UserStore'
import type { IMyGroup, ICircuit } from 'model';
import { IImgGallery, IUserFields, IUserProfile, IFriends, IAccount } from 'model'
import { costanti } from '@costanti'
import { shared_consts } from '@src/common/shared_vuejs'
import { tools } from '@tools'
import { useQuasar } from 'quasar'
import { useI18n } from 'vue-i18n'
import { useRoute, useRouter } from 'vue-router'
import { CUserNonVerif } from '@src/components/CUserNonVerif'
import { toolsext } from '@store/Modules/toolsext'
import { CSaldo } from '@src/components/CSaldo'
import { CInfoAccount } from '@src/components/CInfoAccount'
import { CSendCoins } from '@src/components/CSendCoins'
import { CCurrencyValue } from '@src/components/CCurrencyValue'
import { useCircuitStore } from '@store/CircuitStore'

export default defineComponent({
  name: 'CMyGroup',
  emits: ['setCmd'],
  components: { CInfoAccount, CUserNonVerif, CSaldo, CSendCoins, CCurrencyValue },
  props: {
    mygrp: {
      type: Object as PropType<IMyGroup | null>,
      required: false,
      default: null,
    },
    mygroupname: {
      type: String,
      required: false,
      default: null,
    },
    visu: {
      type: Number,
      required: true,
    },
    circuitname: {
      type: String,
      required: false,
      default: '',
    },
    noaut: {
      type: Boolean,
      required: false,
      default: false,
    },

  },

  setup(props, { emit }) {

    const userStore = useUserStore()
    const $q = useQuasar()
    const { t } = useI18n()
    const $router = useRouter()
    const $route = useRoute()

    const groupname = ref('')
    const circuitStore = useCircuitStore()

    const showsendCoinTo = ref(false)
    const showAccountInfo = ref(false)

    const grp = ref(<IMyGroup | null>null)

    const table = ref(shared_consts.TABLES_MYGROUPS)

    const circuit = ref(<ICircuit | null | undefined>null)

    watch(() => props.mygrp, (newval, oldval) => {
      mounted()
    })

    function mounted() {
      if (!props.mygrp) {
        if (props.mygroupname) {
          groupname.value = props.mygroupname
          //++Todo: carica contact
          grp.value = null
        }
      } else {
        if (props.mygrp) {
          grp.value = props.mygrp
          groupname.value = props.mygrp.groupname
        }
      }
      circuit.value = circuitStore.getCircuitByName(props.circuitname)
    }

    function getImgGroup(group: IMyGroup) {
      return userStore.getImgByGroup(group)
    }

    function naviga(path: string) {
      $router.push(path)
    }

    function setCmd(cmd: number, myusername: string, value: any = '') {
      emit('setCmd', cmd, myusername, value)
    }

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

    onMounted(mounted)

    return {
      grp,
      costanti,
      getImgGroup,
      naviga,
      setCmd,
      shared_consts,
      userStore,
      tools,
      table,
      myusername,
      circuit,
      circuitStore,
      showsendCoinTo,
      showAccountInfo,
      t,
    }
  },
})
