import {computed, ComputedRef} from '@nuxtjs/composition-api'
import { Item, Model } from '@vuex-orm/core'
import { repositoryHelper } from '~/services/store/repository'
import { QUERY_TYPE } from '~/types/enums'
import {AccessStore, Historical} from '~/types/interfaces'
import { MyProfile } from '~/models/Access/MyProfile'
import { $accessProfile } from '@/services/profile/accessProfile'
import DataPersister from "~/services/data/dataPersister";

/**
 * Composable function qui permet de gérer les opérations sur le profil connecté
 * @param $dataPersister
 * @param store
 */
export function useMyProfile($dataPersister: DataPersister, store: AccessStore){
  $accessProfile.setStore(store)
  const currentAccessId = $accessProfile.getCurrentAccessId()
  const myProfile = getMyProfileInstance(currentAccessId) as MyProfile
  const activityYear:ComputedRef<number> = computed(() => myProfile.activityYear)
  const historical:ComputedRef<Historical> = computed(() => myProfile.historical)

  /**
   * Effectue la mise à jour (coté API) de MyProfile
   */
  async function updateMyProfile (): Promise<any> {
    await $dataPersister.invoke({
      type: QUERY_TYPE.MODEL,
      model: MyProfile,
      id: myProfile.id
    })
  }

  /**
   * Mets à jour l'activity de my profile
   * @param activityYear
   */
  function setActivityYear (activityYear:number) {
    if (activityYear <= 0) { throw new Error('year must be positive') }
    repositoryHelper.updateStoreFromField(MyProfile, myProfile, activityYear, 'activityYear')
  }

  /**
   * Mets à jour l'historical de my profile
   * @param historicalChoices
   */
  function setHistorical (historicalChoices:Array<string>) {
    repositoryHelper.updateStoreFromField(MyProfile, myProfile, getHistoricalEntry(historicalChoices), 'historical')
  }


  /**
   * Mets à jour l'historical de my profile
   * @param dates
   */
  function setHistoricalRange (dates:Array<string>) {
    repositoryHelper.updateStoreFromField(MyProfile, myProfile, getHistoricalRangeEntry(dates), 'historical')
  }

  return {
    currentAccessId,
    activityYear,
    historical,
    updateMyProfile,
    setActivityYear,
    setHistorical,
    setHistoricalRange
  }
}

/**
 * récupère l'instance MyProfile
 * @param myProfileId
 */
function getMyProfileInstance (myProfileId:any): Item<Model> {
  return repositoryHelper.findItemFromModel(MyProfile, parseInt(myProfileId))
}

/**
 * Transform les choix de l'historique en objet JSON reconnaissable coté API
 * @param historicalChoices
 */
function getHistoricalEntry (historicalChoices:Array<string>) {
  const historicalDefault:any = { past: false, future: false, present: false }
  for (const historicalChoice of historicalChoices) {
    historicalDefault[historicalChoice] = true
  }
  return historicalDefault
}

/**
 * Transforme le choix des période en Objet JSON reconnaissable coté API
 * @param dates
 */
function getHistoricalRangeEntry (dates:Array<string>) {
  return { past: false, future: false, present: false, dateStart: dates[0], dateEnd: dates[1] }
}

/**
 * Const servant à assurer les tests des fonctions non exportées
 */
export const _exportedForTesting = {
  getMyProfileInstance,
  getHistoricalEntry,
  getHistoricalRangeEntry
}
