/* eslint-disable no-param-reassign */
import {
  createSlice,
} from '@reduxjs/toolkit';

export const initialState = {
  isFetchingALPR: false,
  isFetchingData: false,
  lastFetchedDataPlate: '540BLG',
  results: {
    plate: '540BLG',
    name: 'ALFA ROMEO 159 SPORTWAGON',
    vin: 'ZAR93900007055169',
    'first registration': '07.09.2006',
    category: 'passenger car',
    body: 'universal',
    'body colour': 'grey',
    engine: '2387 cm',
    'engine power': '147 kW',
    fuel: 'Diesel',
    transmission: 'Manual',
    drivetrain: 'front wheel drive',
    'registration certificate': 'EL573054',
    score: 1.0,
  },
};

const recognitionSlice = createSlice({
  name: 'alpr',
  initialState,
  reducers: {
    alprFetchStart(state) {
      state.isFetchingALPR = true;
    },
    alprFetchSuccess(state, {
      payload,
    }) {
      state.isFetchingALPR = false;
      state.results = payload.results;
    },
    resetResults(state) {
      state.results = [];
      state.lastFetchedDataPlate = null;
    },
    dataFetchStart(state) {
      state.isFetchingData = true;
    },
    dataFetchSuccess(state, {
      payload,
    }) {
      state.isFetchingData = false;
      state.results = { ...state.results, ...payload };
      state.lastFetchedDataPlate = payload ? payload.plate : null;
    },
  },
});

export const {
  alprFetchStart,
  alprFetchSuccess,
  resetResults,
  dataFetchStart,
  dataFetchSuccess,
} = recognitionSlice.actions;

export default recognitionSlice.reducer;
