import { defineStore } from 'pinia'

import {
  IAccount,
  ICircuit, ICircuitState, IGlobalState,
} from '@src/model'
import { tools } from '@store/Modules/tools'
import translate from '@src/globalroutines/util'

import * as Types from '@src/store/Api/ApiTypes'
import { useGlobalStore } from '@store/globalStore'
import { serv_constants } from '@store/Modules/serv_constants'
import { Api } from '@api'
import { toolsext } from '@store/Modules/toolsext'
import { static_data } from '@src/db/static_data'
import { useUserStore } from '@store/UserStore'


import { shared_consts } from '@/common/shared_vuejs'
import { costanti } from '@costanti'

import globalroutines from '../globalroutines/index'

export const useCircuitStore = defineStore('CircuitStore', {
  state: (): ICircuitState => ({
    listcircuits: []
  }),

  getters: {},

  actions: {
    getRemainingCoinsToSend(account: IAccount) {
      if (account)
        return tools.roundDec2(account.saldo + account.fidoConcesso)
      else
        return 0
    },
    getMaxCoinsToSend(account: IAccount) {
      if (account)
        return tools.roundDec2(account.qta_maxConcessa - account.saldo)
      else
        return 0
    },
    getSaldoByCircuitId(circuitId: string): number {
      const userStore = useUserStore()
      const account = userStore.my.profile.useraccounts.find((rec: IAccount) => rec.circuitId === circuitId)
      if (account)
        return account.saldo
      else
        return 0
    },

    getCircuitClass(circuit: ICircuit) {
      if (circuit.status === shared_consts.CIRCUIT_STATUS.FASE3_MONETA_ABILITATA)
        return 'circuito_abilitato'
      else if (circuit.status === shared_consts.CIRCUIT_STATUS.FASE2_ORGANIZZAZIONE)
        return 'circuito_fase_2'
      else if (circuit.status === shared_consts.CIRCUIT_STATUS.FASE1_CREAZIONE_GRUPPO)
        return 'circuito_in_creazione'
    },

    getColorCircuitClass(circuit: ICircuit) {
      if (circuit.status === shared_consts.CIRCUIT_STATUS.FASE3_MONETA_ABILITATA)
        return 'green'
      else if (circuit.status === shared_consts.CIRCUIT_STATUS.FASE2_ORGANIZZAZIONE)
        return 'orange'
      else if (circuit.status === shared_consts.CIRCUIT_STATUS.FASE1_CREAZIONE_GRUPPO)
        return 'red'
    },

    async loadCircuits() {
      return Api.SendReq('/users/circuits', 'POST', null)
        .then((res) => {
          return res.data
        }).catch((error) => {
          return {}
        })

    },

  },
})
