import type { PropType } from 'vue';
import { computed, defineComponent, onMounted, ref, watch } from 'vue'
import { useUserStore } from '@store/UserStore'
import { useCircuitStore } from '@store/CircuitStore'

import type { ICircuit, IMyCircuit, IMyGroup } 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 { CSaldo } from '@src/components/CSaldo'
import { CTitleBanner } from '@src/components/CTitleBanner'
import { toolsext } from '@store/Modules/toolsext'
import { useGlobalStore } from '@store/globalStore'

export default defineComponent({
  name: 'CMyCircuit',
  emits: ['setCmd'],
  components: { CUserNonVerif, CSaldo, CTitleBanner },
  props: {
    mycircuit: {
      type: Object as PropType<ICircuit | null>,
      required: false,
      default: null,
    },
    circuitname: {
      type: String,
      required: false,
      default: null,
    },
    prop_groupnameSel: {
      type: Object as PropType<IMyGroup | null>,
      required: false,
      default: null,
    },
    visu: {
      type: Number,
      required: true,
    },
    noaut: {
      type: Boolean,
      required: false,
      default: false,
    },
    username: {
      type: String,
      required: false,
      default: '',
    }
  },

  setup(props, { emit }) {

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

    const globalStore = useGlobalStore()

    const circuit = ref(<IMyCircuit | ICircuit | null>null)
    const account = computed(() => circuit.value ? userStore.getAccountByCircuitId(circuit.value._id) : null)

    const qtarem = computed(() => account.value ? circuitStore.getRemainingCoinsToSend(account.value) : 0)
    const saldo_pend = computed(() => account.value ? account.value.saldo_pend : 0)
    const saldo = computed(() => account.value ? account.value.saldo : 0)

    const table = ref(shared_consts.TABLES_CIRCUITS)

    const showingtooltip = ref(false)
    const showrules = ref(false)
    const requestToEnterCircuit = ref(false)
    const groupnameSel = ref(<IMyGroup | null>null)

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

    function getNameCircuit() {
      if (circuit.value) {
        if (tools.existProp(circuit.value, 'name')) {
          return tools.getProp(circuit.value, 'name')
        } else if (tools.existProp(circuit.value, 'circuitname')) {
          return tools.getProp(circuit.value, 'circuitname')
        }
      }

      return ''
    }

    function mounted() {
      groupnameSel.value = props.prop_groupnameSel
      if (!props.mycircuit) {
        if (props.circuitname) {
          circuit.value = null
        }
      } else {
        if (props.mycircuit) {
          circuit.value = props.mycircuit
        }
      }
      if (circuit.value) {
        const rectofind = circuitStore.listcircuits.find((circ: ICircuit) => circ.name === getNameCircuit())
        if (rectofind) {
          // console.log('rectofind', rectofind)
          circuit.value = rectofind
        }
      }
    }

    function getImgCircuit(circuit: ICircuit) {
      return userStore.getImgByCircuit(circuit)
    }

    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
    }

    function getRegulation(reg: string) {
      const strreg = reg + ''
      if (!reg) {
        let name = getNameCircuit()
        const mystringa = t('circuit.regolamento', { nomecircuito: name })
        return mystringa
      } else {
        return reg
      }

    }

    onMounted(mounted)

    return {
      circuit,
      getRegulation,
      costanti,
      getImgCircuit,
      naviga,
      setCmd,
      shared_consts,
      userStore,
      tools,
      table,
      myusername,
      circuitStore,
      t,
      account,
      qtarem,
      saldo,
      saldo_pend,
      globalStore,
      showingtooltip,
      showrules,
      requestToEnterCircuit,
      groupnameSel,
    }
  },
})
