import { IAction } from '@rh-support/types';
// Constants
export enum AttachmentReducerConstants {
    caseFileSelected = 'caseFileSelected',
    caseLocalFileEdited = 'caseLocalFileEdited',
    caseFileDeleted = 'caseFileDeleted',
    resetAttachment = 'resetAttachment'
}

// Interfaces
export interface IAbleToUpload {
    ableToUpload: boolean; ableToUploadMessage: string;
}

export interface IUploadMetadata {
    inProgress: boolean;
    completed: boolean;
    uploadError: boolean;
    errorMessage: string;
    verifying: boolean;
    aborted: boolean;
    onAbort: (abortedFileIndex: number) => void;
    percentUploaded: number;

}
export interface IFileLocal {
    name: string;
    size: number;
    lastModified: number;
    type: string;
    description: string;
    uploadProgress: IUploadMetadata;
    uploadability: IAbleToUpload;
}

export interface ICaseFiles {
    selectedLocalFiles: IFileLocal[];
    selectedOriginalFiles: File[];
}

export interface IAttachemntState {
    caseFiles: ICaseFiles;
}

export const defaultUploadMetadata: IUploadMetadata = {
    inProgress: false,
    verifying: false,
    completed: false,
    uploadError: false,
    errorMessage: '',
    percentUploaded: 0,
    onAbort: undefined,
    aborted: false,
};
export const initialAttachmentState: IAttachemntState = {
    caseFiles: {
        selectedLocalFiles: [],
        selectedOriginalFiles: []
    }
};
export interface IAttachmentPayloadType extends IAttachemntState {
    editedLocalFile: IFileLocal;
    fileIndexToEdit: number;
    fileIndexToRemove: number;
}
type IActionType = IAction<AttachmentReducerConstants, IAttachmentPayloadType>;
export type AttachmentReducerDispatchType = (value: IActionType) => void;

export const attachmentReducer = (pState: IAttachemntState, action: IActionType): IAttachemntState => {
    switch (action.type) {
        case AttachmentReducerConstants.caseFileSelected: {
            const newCaseFiles: ICaseFiles = action.payload.caseFiles;
            const newState = { ...pState };
            const caseFiles = { ...newState.caseFiles };
            newState.caseFiles = caseFiles;
            newState.caseFiles.selectedLocalFiles.push(...newCaseFiles.selectedLocalFiles);
            newState.caseFiles.selectedOriginalFiles.push(...newCaseFiles.selectedOriginalFiles);
            return newState;
        }
        case AttachmentReducerConstants.caseLocalFileEdited: {
            const editedLocalFile: IFileLocal = action.payload.editedLocalFile;
            const indexToEdit: number = action.payload.fileIndexToEdit;
            const newState = { ...pState };
            const newCaseFiles = { ...newState.caseFiles };
            newState.caseFiles = newCaseFiles;
            newState.caseFiles.selectedLocalFiles[indexToEdit] = editedLocalFile;
            return newState;
        }
        case AttachmentReducerConstants.caseFileDeleted: {
            const fileIndexToRemove: number = action.payload.fileIndexToRemove;
            const newState = { ...pState };
            const newCaseFiles = { ...newState.caseFiles };
            newState.caseFiles = newCaseFiles;
            newState.caseFiles.selectedLocalFiles.splice(fileIndexToRemove, 1);
            newState.caseFiles.selectedOriginalFiles.splice(fileIndexToRemove, 1);
            return newState;
        }
        case AttachmentReducerConstants.resetAttachment: {
            const newState = { ...pState };
            newState.caseFiles = {
                selectedLocalFiles: [],
                selectedOriginalFiles: []
            };
            return newState;
        }
        default: {
            return pState;
        }
    }
};

// Actions
export const onFilesSelect = (dispatch: AttachmentReducerDispatchType, caseFiles: ICaseFiles) => {
    dispatch({ type: AttachmentReducerConstants.caseFileSelected, payload: { caseFiles } });
};
export const onFileEdit = (dispatch: AttachmentReducerDispatchType, editedLocalFile: IFileLocal, fileIndexToEdit: number) => {
    dispatch({ type: AttachmentReducerConstants.caseLocalFileEdited, payload: { editedLocalFile, fileIndexToEdit } });
};
export const onFileRemove = (dispatch: AttachmentReducerDispatchType, fileIndexToRemove: number) => {
    dispatch({ type: AttachmentReducerConstants.caseFileDeleted, payload: { fileIndexToRemove } });
};
export const resetAttachment = (dispatch: AttachmentReducerDispatchType) => {
    dispatch({ type: AttachmentReducerConstants.resetAttachment });
};
