import { createTypedReducerWithImmer } from "@insite/client-framework/Common/CreateTypedReducer";
import { ProcessFileResult } from "@insite/client-framework/Store/Components/VmiLocationsImportModal/Handlers/ProcessFile";
import { BatchCreateVmiLocationsResult } from "@insite/client-framework/Store/Components/VmiLocationsImportModal/Handlers/ValidateVmiLocations";
import VmiLocationsImportModalState from "@insite/client-framework/Store/Components/VmiLocationsImportModal/VmiLocationsImportModalState";
import { Draft } from "immer";

const initialState: VmiLocationsImportModalState = {
    parsedItems: [],
    importedItems: [],
    errorEntries: [],
    isBadFile: false,
    isUploading: false,
    vmiLocationsProcessed: false,
    allowCancel: false,
    uploadCancelled: false,
    uploadLimitExceeded: false,
    errorsModalIsOpen: false,
    modalIsOpen: false,
    fullFileError: false,
    continueUpload: false,
};

const reducer = {
    "Components/VmiLocationsImportModal/BeginValidateVmiLocations": (draft: Draft<VmiLocationsImportModalState>) => {
        draft.vmiLocationsProcessed = false;
    },
    "Components/VmiLocationsImportModal/CompleteValidateVmiLocations": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { result: BatchCreateVmiLocationsResult },
    ) => {
        draft.importedItems = action.result.importedItems;
        draft.errorEntries = action.result.errorEntries;
        draft.vmiLocationsProcessed = true;
    },
    "Components/VmiLocationsImportModal/CleanupUploadData": (draft: Draft<VmiLocationsImportModalState>) => {
        draft.importedItems = [];
        draft.errorEntries = [];
        draft.continueUpload = false;
        draft.vmiLocationsProcessed = false;
    },
    "Components/VmiLocationsImportModal/CompleteProcessFile": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { result: ProcessFileResult },
    ) => {
        draft.parsedItems = action.result.parsedItems;
        draft.uploadLimitExceeded = action.result.uploadLimitExceeded;
        draft.isBadFile = action.result.isBadFile;
        draft.isUploading = action.result.isUploading;
        draft.allowCancel = action.result.allowCancel;
    },
    "Components/VmiLocationsImportModal/SetAllowCancel": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { parameter: { allowCancel: boolean } },
    ) => {
        draft.allowCancel = action.parameter.allowCancel;
    },
    "Components/VmiLocationsImportModal/SetContinueUpload": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { parameter: { continueUpload: boolean } },
    ) => {
        draft.continueUpload = action.parameter.continueUpload;
    },
    "Components/VmiLocationsImportModal/SetErrorsModalIsOpen": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { parameter: { errorsModalIsOpen: boolean } },
    ) => {
        draft.errorsModalIsOpen = action.parameter.errorsModalIsOpen;
    },
    "Components/VmiLocationsImportModal/SetIsBadFile": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { parameter: { isBadFile: boolean } },
    ) => {
        draft.isBadFile = action.parameter.isBadFile;
    },
    "Components/VmiLocationsImportModal/SetIsUploading": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { parameter: { isUploading: boolean } },
    ) => {
        draft.isUploading = action.parameter.isUploading;
    },
    "Components/VmiLocationsImportModal/SetUploadCancelled": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { parameter: { uploadCancelled: boolean } },
    ) => {
        draft.uploadCancelled = action.parameter.uploadCancelled;
    },
    "Components/VmiLocationsImportModal/SetUploadLimitExceeded": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { parameter: { uploadLimitExceeded: boolean } },
    ) => {
        draft.uploadLimitExceeded = action.parameter.uploadLimitExceeded;
    },
    "Components/VmiLocationsImportModal/SetModalIsOpen": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { isOpen: boolean },
    ) => {
        draft.modalIsOpen = action.isOpen;
    },
    "Components/VmiLocationsImportModal/SetFullFileError": (
        draft: Draft<VmiLocationsImportModalState>,
        action: { fullFileError: boolean },
    ) => {
        draft.fullFileError = action.fullFileError;
    },
};

export default createTypedReducerWithImmer(initialState, reducer);
