import * as _ from 'lodash'
import { $roleUtils } from '~/services/rights/roleUtils'
import {AbilitiesType, accessState, baseAccessState, baseOrganizationState, Historical, OrignalAccessState} from '~/types/interfaces'
import { GENDER } from '~/types/enums'
import { MyProfile } from '~/models/Access/MyProfile'
import { repositoryHelper } from '~/services/store/repository'

export const state = () => ({
  bearer: null,
  id: null,
  switchId: null,
  name: null,
  givenName: null,
  gender: null,
  avatarId: null,
  activityYear: null,
  historical: [],
  roles: [],
  abilities: [],
  isAdminAccess: false,
  isSuperAdminAccess: false,
  isAdmin: false,
  isAdministratifManager: false,
  isPedagogicManager: false,
  isFinancialManager: false,
  isCaMember: false,
  isStudent: false,
  isTeacher: false,
  isMember: false,
  isOther: false,
  isGuardian: false,
  isPayor: false,
  hasLateralMenu: false,
  hasConfigurationMenu: false,
  hasAccessesMenu: false,
  hasFamilyMenu: false,
  multiAccesses: [],
  familyAccesses: [],
  originalAccess: null
})

export const mutations = {
  setBearer (state: accessState, bearer: string) {
    state.bearer = bearer
  },
  setId (state: accessState, id: number) {
    state.id = id
  },
  setSwitchId (state: accessState, switchId: number) {
    state.switchId = switchId
  },
  setName (state: accessState, name: string) {
    state.name = name
  },
  setGivenName (state: accessState, givenName: string) {
    state.givenName = givenName
  },
  setGender (state: accessState, gender: GENDER) {
    state.gender = gender
  },
  setAvatarId (state: accessState, avatarId: number) {
    state.avatarId = avatarId
  },
  setActivityYear (state: accessState, activityYear: number) {
    state.activityYear = activityYear
  },
  setHistorical (state: accessState, historical: Historical) {
    state.historical = historical
  },
  setRoles (state: accessState, roles: Array<string>) {
    state.roles = roles
  },
  setAbilities (state: accessState, abilities: Array<AbilitiesType>) {
    state.abilities = abilities
  },
  setIsAdminAccess (state: accessState, isAdminAccess: boolean) {
    state.isAdminAccess = isAdminAccess
  },
  setIsSuperAdminAccess (state: accessState, isSuperAdminAccess: boolean) {
    state.isSuperAdminAccess = isSuperAdminAccess
  },
  setIsAdmin (state: accessState, isRole: boolean) {
    state.isAdmin = isRole
  },
  setIsAdministratifManager (state: accessState, isRole: boolean) {
    state.isAdministratifManager = isRole
  },
  setIsPedagogicManager (state:accessState, isRole:boolean) {
    state.isPedagogicManager = isRole
  },
  setIsFinancialManager (state: accessState, isRole: boolean) {
    state.isFinancialManager = isRole
  },
  setIsCaMember (state: accessState, isRole: boolean) {
    state.isCaMember = isRole
  },
  setIsStudent (state: accessState, isRole: boolean) {
    state.isStudent = isRole
  },
  setIsTeacher (state: accessState, isRole: boolean) {
    state.isTeacher = isRole
  },
  setIsMember (state: accessState, isRole: boolean) {
    state.isMember = isRole
  },
  setIsOther (state: accessState, isRole: boolean) {
    state.isOther = isRole
  },
  setIsGuardian(state:accessState, isGuardian:boolean){
    state.isGuardian = isGuardian
  },
  setIsPayor(state:accessState, isPayor:boolean){
    state.isPayor = isPayor
  },
  setHasLateralMenu(state:accessState, hasLateralMenu:boolean){
    state.hasLateralMenu = hasLateralMenu
  },
  setHasConfigurationMenu (state: accessState, hasConfigurationMenu: boolean) {
    state.hasConfigurationMenu = hasConfigurationMenu
  },
  setHasAccessesMenu (state: accessState, hasAccessesMenu: boolean) {
    state.hasAccessesMenu = hasAccessesMenu
  },
  setHasFamilyMenu (state: accessState, hasFamilyMenu: boolean) {
    state.hasFamilyMenu = hasFamilyMenu
  },
  addMultiAccess (state: accessState, access: baseOrganizationState) {
    state.multiAccesses.push(access)
  },
  addFamilyAccess (state: accessState, access: baseAccessState) {
    state.familyAccesses.push(access)
  },
  setOriginalAccess (state: accessState, access: OrignalAccessState) {
    state.originalAccess = access
  }
}

export const actions = {
  setProfile (context: any, profile: any) {
    const roles_to_array: Array<string> = Object.values(profile.roles)

    context.commit('setName', profile.name)
    context.commit('setGivenName', profile.givenName)
    context.commit('setGender', profile.gender)
    context.commit('setAvatarId', profile.avatarId)
    context.commit('setActivityYear', profile.activityYear)
    context.commit('setHistorical', profile.historical)
    context.commit('setIsAdminAccess', profile.isAdminAccess)
    context.commit('setIsAdmin', $roleUtils.isA('ADMIN', roles_to_array))
    context.commit('setIsAdministratifManager', $roleUtils.isA('ADMINISTRATIF_MANAGER', roles_to_array))
    context.commit('setIsPedagogicManager', $roleUtils.isA('PEDAGOGICS_MANAGER', roles_to_array))
    context.commit('setIsFinancialManager', $roleUtils.isA('FINANCIAL_MANAGER', roles_to_array))
    context.commit('setIsCaMember', $roleUtils.isA('CA', roles_to_array))
    context.commit('setIsStudent', $roleUtils.isA('STUDENT', roles_to_array))
    context.commit('setIsTeacher', $roleUtils.isA('TEACHER', roles_to_array))
    context.commit('setIsMember', $roleUtils.isA('MEMBER', roles_to_array))
    context.commit('setIsOther', $roleUtils.isA('OTHER', roles_to_array))
    context.commit('setIsGuardian', profile.isGuardian)
    context.commit('setIsPayor', profile.isPayor)
    context.commit('setRoles', $roleUtils.filterFunctionRoles(roles_to_array))
    context.commit('setIsAdminAccess', profile.isAdminAccess)

    // Time to set Multi Accesses
    context.dispatch('setMultiAccesses', profile.multiAccesses)

    // Time to set Family Accesses
    context.dispatch('setFamilyAccesses', profile.familyAccesses)

    // Time to add the original Access (switch User case)
    context.dispatch('setOriginalAccess', profile.originalAccess)

    // Time to set Oganization Profile
    context.dispatch('profile/organization/setProfile', profile.organization, { root: true })

    context.dispatch('createNewMyProfileVUexOrmInstance', profile)
  },
  setMultiAccesses (context: any, organizations: any) {
    _.each(organizations, (organization:baseOrganizationState) => {
      const o: baseOrganizationState = {
        id: organization.id,
        name: organization.name
      }
      context.commit('addMultiAccess', o)
    })
  },
  setFamilyAccesses (context: any, accesses: any) {
    _.each(accesses, (access: baseAccessState) => {
      const a:baseAccessState = {
        id: access.id,
        name: access.name,
        givenName: access.givenName,
        gender: access.gender,
        avatarId: access.avatarId
      }
      context.commit('addFamilyAccess', a)
    })
  },
  setOriginalAccess (context: any, access: any) {
    if (access) {
      const organization: baseOrganizationState = {
        id: access.organization.id,
        name: access.organization.name
      }

      const originalAccess:OrignalAccessState = {
        id: access.id,
        name: access.name,
        givenName: access.givenName,
        gender: access.gender,
        isSuperAdminAccess: access.isSuperAdminAccess,
        avatarId: access.avatarId,
        organization: organization
      }
      context.commit('setOriginalAccess', originalAccess)
    }
  },
  /**
   * Créer une nouvelle instance MyProfile dans Vuex ORM
   * @param _context
   * @param access
   */
  createNewMyProfileVUexOrmInstance (_context: any, access: any): void {
    repositoryHelper.persist(MyProfile, access)
  }
}
