import React, { useState } from 'react';
import { Button, FullscreenModal } from '@theorchard/suite-components';
import { ABACUS_ACTION_STATUSES } from '@theorchard/accounting-apps-shared';

import { AbacusStatementPeriodAdjustmentFileErrorType } from 'src/apollo/definitions/globalTypes';
import { AdjustmentsContext } from 'src/contexts/adjustments-context';
import AdjustmentsUploadContentErrorScreen from './adjustments-upload-content-error-screen';
import AdjustmentsUploadFailedScreen from './adjustments-upload-failed-screen';
import AdjustmentsUploadFormatErrorScreen from './adjustments-upload-format-error-screen';
import AdjustmentsUploadSuccessScreen from './adjustments-upload-success-screen';
import { AdjustmentsUploadRowCountErrorScreen } from './adjustments-upload-row-count-error-screen';

import type { AdjustmentFile } from 'src/types/adjustment-file';
import AdjustmentsUploadFileScreenWithAbacusFileUpload from 'src/components/adjustments/adjustments-import-modal/using-abacus-file-upload/adjustments-upload-file-screen-with-abacus-file-upload';
import AdjustmentsUploadLoadingScreenWithAbacusFileUpload from 'src/components/adjustments/adjustments-import-modal/using-abacus-file-upload/adjustments-upload-loading-screen-with-abacus-file-upload';
import './styles.scss';

export interface AdjustmentsImportModalPropTypes {
    isModalOpen: boolean;
    setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
}

export const AdjustmentsImportModal: React.FC<
    AdjustmentsImportModalPropTypes
> = ({ isModalOpen, setIsModalOpen }) => {
    const [modalState, setModalState] = useState<string>(
        ABACUS_ACTION_STATUSES.INIT
    );

    const [adjustmentFile, setAdjustmentFile] = useState<
        AdjustmentFile | undefined
    >(undefined);

    const [statementPeriodId, setStatementPeriodId] = useState<string>('');

    const [
        statementPeriodAdjustmentFileId,
        setStatementPeriodAdjustmentFileId,
    ] = useState<string>('');

    const resetToInitialState = () => {
        setAdjustmentFile(undefined);
        setModalState(ABACUS_ACTION_STATUSES.INIT);
    };

    const handleModalClose = () => {
        resetToInitialState();
        setIsModalOpen(false);
    };

    const adjustmentsContext = {
        adjustmentFile,
        handleModalClose,
        resetToInitialState,
        setAdjustmentFile,
        setModalState,
        setStatementPeriodAdjustmentFileId,
        setStatementPeriodId,
        statementPeriodAdjustmentFileId,
        statementPeriodId,
    };

    const renderModalState = () => {
        if (modalState === ABACUS_ACTION_STATUSES.INIT) {
            return <AdjustmentsUploadFileScreenWithAbacusFileUpload />;
        }

        if (modalState === ABACUS_ACTION_STATUSES.ERROR) {
            switch (adjustmentFile?.errorType) {
                case AbacusStatementPeriodAdjustmentFileErrorType.CONTENT_ERROR:
                    return <AdjustmentsUploadContentErrorScreen />;
                case AbacusStatementPeriodAdjustmentFileErrorType.FORMAT_ERROR:
                    return <AdjustmentsUploadFormatErrorScreen />;
                case AbacusStatementPeriodAdjustmentFileErrorType.ROW_COUNT_ERROR:
                    return <AdjustmentsUploadRowCountErrorScreen />;
                default:
                    return <AdjustmentsUploadFailedScreen />;
            }
        }

        if (modalState === ABACUS_ACTION_STATUSES.RUNNING) {
            return <AdjustmentsUploadLoadingScreenWithAbacusFileUpload />;
        }

        if (modalState === ABACUS_ACTION_STATUSES.COMPLETE && adjustmentFile) {
            return <AdjustmentsUploadSuccessScreen />;
        }
    };

    return (
        <FullscreenModal
            className="AdjustmentsImportModal"
            isOpen={isModalOpen}
            layout="fluid"
            onRequestClose={handleModalClose}
        >
            <FullscreenModal.Header title="Add Adjustments File">
                <Button
                    onClick={handleModalClose}
                    testId="headerButton"
                    variant="tertiary"
                >
                    Cancel
                </Button>
            </FullscreenModal.Header>
            <AdjustmentsContext.Provider value={adjustmentsContext}>
                {renderModalState()}
            </AdjustmentsContext.Provider>
        </FullscreenModal>
    );
};

export default AdjustmentsImportModal;
