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

export default defineComponent({
  name: 'CMyUser',
  components: { CSendCoins, CSaldo, CUserInfoAccount },
  emits: ['setCmd', 'showInnerDialog'],
  props: {
    mycontact: {
      type: Object as PropType<IUserFields | null>,
      required: false,
      default: null,
    },
    actionType: {
      type: Number,
      required: false,
      default: costanti.ACTIONTYPE.NONE,
    },
    myusername: {
      type: String,
      required: false,
      default: null,
    },
    visu: {
      type: Number,
      required: true,
    },
    groupname: {
      type: String,
      required: false,
      default: '',
    },
    circuitname: {
      type: String,
      required: false,
      default: '',
    },
    name: {
      type: String,
      required: false,
      default: '',
    },
    labelextra: {
      type: String,
      required: false,
      default: '',
    },
    labelFooter: {
      type: String,
      required: false,
      default: '',
    },
    notsetcmd: {
      type: Boolean,
      required: false,
      default: false,
    },
    hideellipses: {
      type: Boolean,
      required: false,
      default: false,
    }
  },

  setup(props, { emit }) {

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

    const showsendCoinTo = ref(false)

    const showAccountInfo = ref(false)

    const username = ref('')

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

    watch(() => props.mycontact!.username, (newval, oldval) => {
      console.log('watch: mycontact', props.mycontact)
      mounted()
    })

    function mounted() {
      if (props.mycontact) {
        contact.value = props.mycontact
        username.value = props.mycontact.username
      }

      circuit.value = circuitStore.getCircuitByName(props.circuitname)
    }

    function getImgUser(profile: IUserFields) {
      return userStore.getImgByProfile(profile)
    }

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

    function setCmd($q: any, cmd: number, myusername: string, value: any, dest: string) {
      if (props.notsetcmd) {
        tools.setCmd($q, cmd, myusername, value, dest)
      } else {
        emit('setCmd', $q, cmd, myusername, value, dest)
      }
    }

    function showsendCoin(value: boolean) {
      showsendCoinTo.value = value
      emit('showInnerDialog', showsendCoinTo.value)
    }

    function clickToUser(username: string) {
      if (props.actionType === costanti.ACTIONTYPE.SEND_RIS)
        naviga(`/my/` + username + '?sr=0')
      else if (props.actionType === costanti.ACTIONTYPE.LINK_REG)
        naviga(`/registrati/` + username)
      else
        naviga(`/my/` + username)
    }

    onMounted(mounted)

    return {
      contact,
      costanti,
      getImgUser,
      naviga,
      setCmd,
      shared_consts,
      userStore,
      tools,
      circuit,
      showAccountInfo,
      showsendCoinTo,
      showsendCoin,
      clickToUser,
    }
  },
})
