import {
  ActionContext,
  ActionTree,
  GetterTree,
  Module,
  MutationTree,
} from 'vuex';

import { menuCounters } from '@/recruits/api/ervu-person-registry';

const POLLING_INTERVAL_MILISECONDS = 15000;
let pollingIntervalId: any = null;
export interface State {
  batchSigning: number;
  initialMilRegOver18: number;
  militaryRegistration: number;
  personalFile: number;
  removal: number;
  conversionLog: number;
  conversionError: number;
  conversionManual: number;
  conscription: number;
  list: number;
  recruitsForLists: number;
  applications: number;
  activeIncidents: number;
  archiveIncidents: number;
}

export const state: State = {
  batchSigning: 0,
  initialMilRegOver18: 0,
  militaryRegistration: 0,
  personalFile: 0,
  removal: 0,
  conversionLog: 0,
  conversionError: 0,
  conversionManual: 0,
  conscription: 0,
  list: 0,
  recruitsForLists: 0,
  applications: 0,
  activeIncidents: 0,
  archiveIncidents: 0,
};

export const getters: GetterTree<State, any> = {
  batchSigning(state: State): number {
    return state.batchSigning;
  },
  initialMilRegOver18(state: State): number {
    return state.initialMilRegOver18;
  },
  militaryRegistration(state: State): number {
    return state.militaryRegistration;
  },
  personalFile(state: State): number {
    return state.personalFile;
  },
  removal(state: State): number {
    return state.removal;
  },
  conversionLog(state: State): number {
    return state.conversionLog;
  },
  conversionError(state: State): number {
    return state.conversionError;
  },
  conversionManual(state: State): number {
    return state.conversionManual;
  },
  conscription(state: State): number {
    return state.conscription;
  },
  list(state: State): number {
    return state.list;
  },
  recruitsForLists(state: State): number {
    return state.recruitsForLists;
  },
  applicationsCounter(state: State): number {
    return state.applications;
  },
  activeIncidentsCounter(state: State): number {
    return state.activeIncidents;
  },
  archiveIncidentsCounter(state: State): number {
    return state.archiveIncidents;
  },
};

export const mutations: MutationTree<State> = {
  setBatchSigning(state: State, value: number) {
    state.batchSigning = value;
  },
  setInitialMilRegOver18(state: State, value: number) {
    state.initialMilRegOver18 = value;
  },
  setMilitaryRegistration(state: State, value: number) {
    state.militaryRegistration = value;
  },
  setPersonalFile(state: State, value: number) {
    state.personalFile = value;
  },
  setRemoval(state: State, value: number) {
    state.removal = value;
  },
  setConversionLog(state: State, value: number) {
    state.conversionLog = value;
  },
  setConversionError(state: State, value: number) {
    state.conversionError = value;
  },
  setConversionManual(state: State, value: number) {
    state.conversionManual = value;
  },
  setConscription(state: State, value: number) {
    state.conscription = value;
  },
  setList(state: State, value: number) {
    state.list = value;
  },
  setRecruitsForLists(state: State, value: number) {
    state.recruitsForLists = value;
  },
  setApplications(state: State, value: number) {
    state.applications = value;
  },
  setActiveIncidents(state: State, value: number) {
    state.activeIncidents = value;
  },
  setArchiveIncidents(state: State, value: number) {
    state.archiveIncidents = value;
  },
};

export const actions: ActionTree<State, State> = {
  startPolling({ commit, dispatch }: ActionContext<State, State>) {
    if (pollingIntervalId === null) {
      const callback = async () => {
        try {
          const result = await menuCounters.numbers();
          commit('setBatchSigning', result.batchSigning);
          commit('setInitialMilRegOver18', result.initialMilRegOver18);
          commit('setMilitaryRegistration', result.militaryRegistration);
          commit('setPersonalFile', result.personalFile);
          commit('setRemoval', result.removal);
          commit('setConversionLog', result.conversionLog);
          commit('setConversionError', result.conversionError);
          commit('setConversionManual', result.conversionManual);
          commit('setConscription', result.conscription);
          commit('setList', result.list);
          commit('setRecruitsForLists', result.recruitsForLists);
          commit('setApplications', result.applications);
          commit('setActiveIncidents', result.activeIncidents);
          commit('setArchiveIncidents', result.archiveIncidents);
        } catch (error) {
          console.error('Ошибка при запросе счётчиков меню', error);
        }
      }
      callback();
      pollingIntervalId = setInterval(callback, POLLING_INTERVAL_MILISECONDS);
    }
  },
  stopPolling({ commit }: ActionContext<State, State>) {
    clearInterval(pollingIntervalId);
    pollingIntervalId = null;
    commit('setBatchSigning', 0);
    commit('setInitialMilRegOver18', 0);
    commit('setMilitaryRegistration', 0);
    commit('setPersonalFile', 0);
    commit('setRemoval', 0);
    commit('setConversionLog', 0);
    commit('setConversionError', 0);
    commit('setConversionManual', 0);
    commit('setConscription', 0)
    commit('setList', 0)
    commit('setRecruitsForLists', 0)

  },
};
