import React from 'react';
import { Row, Col } from '@theorchard/suite-components';
import { ContractMechanicalDeductionAdminFee } from 'src/components/contract-mechanical-deduction-shared/contract-mechanical-deduction-admin-fee';
import { ContractMechanicalDeductionAdminTypes } from 'src/components/contract-mechanical-deduction-shared/contract-mechanical-deduction-admin-types';
import { ContractMechanicalDeductionProductTypes } from 'src/components/contract-mechanical-deduction-shared/contract-mechanical-deduction-product-types';
import { CONTRACT_MECH_DEDUCTION_ADMIN_FEE } from 'src/constants';
import type { AbacusContractMechDeductionMechanicalType } from 'src/apollo/definitions/globalTypes';
import type { ContractMechanicalDeduction } from 'src/types/abacus-contract-mechanical-deduction';

export interface ContractMechanicalDeductionFormFieldsPropTypes {
    /* eslint-disable no-unused-vars */
    contractMechanicalDeduction: ContractMechanicalDeduction;
    isEditForm?: boolean;
    setContractMechanicalDeduction: (
        params: ContractMechanicalDeduction
    ) => void;
    errors: Partial<ContractMechanicalDeduction>;
    setErrors: (params: Partial<ContractMechanicalDeduction>) => void;
    setIsServerError: (params: boolean) => void;
    /* eslint-enable no-unused-vars */
}

export const ContractMechanicalDeductionFormFields: React.FC<
    ContractMechanicalDeductionFormFieldsPropTypes
> = ({
    contractMechanicalDeduction,
    isEditForm = false,
    setContractMechanicalDeduction,
    errors,
    setErrors,
    setIsServerError,
}) => {
    const handleFormChange = (
        fieldName: string,
        inputValue: string | null | undefined
    ) => {
        setIsServerError(false);
        setErrors({});
        if (
            fieldName === 'adminFee' &&
            inputValue &&
            !CONTRACT_MECH_DEDUCTION_ADMIN_FEE.test(inputValue)
        )
            return;

        if (fieldName === 'mechanicalType') {
            let mechanicalTypes: AbacusContractMechDeductionMechanicalType[] =
                contractMechanicalDeduction?.mechanicalType;

            if (
                !mechanicalTypes.includes(
                    inputValue as AbacusContractMechDeductionMechanicalType
                )
            )
                mechanicalTypes.push(
                    inputValue as AbacusContractMechDeductionMechanicalType
                );
            else
                mechanicalTypes = mechanicalTypes.filter(
                    (
                        mechanicalType: AbacusContractMechDeductionMechanicalType
                    ) => mechanicalType !== inputValue
                );

            setContractMechanicalDeduction({
                ...contractMechanicalDeduction,
                [fieldName]: mechanicalTypes,
            });

            return;
        }

        setContractMechanicalDeduction({
            ...contractMechanicalDeduction,
            [fieldName]: inputValue,
        });
    };

    const handleAdminFeeChange = (isAdminFeeSelected: boolean) => {
        setIsServerError(false);

        setContractMechanicalDeduction({
            ...contractMechanicalDeduction,
            isAdminFeeSelected,
        });

        if (!isAdminFeeSelected)
            setContractMechanicalDeduction({
                ...contractMechanicalDeduction,
                isAdminFeeSelected,
                adminFee: null,
            });
    };

    return (
        <>
            <Row>
                <Col>
                    <ContractMechanicalDeductionProductTypes
                        handleFormChange={handleFormChange}
                        isEditForm={isEditForm}
                        productTypes={
                            contractMechanicalDeduction?.mechanicalType
                        }
                        territory={
                            contractMechanicalDeduction?.territory || null
                        }
                    />
                </Col>
            </Row>
            <Row>
                <Col>
                    <ContractMechanicalDeductionAdminTypes
                        adminType={contractMechanicalDeduction?.adminType}
                        handleFormChange={handleFormChange}
                    />
                </Col>
            </Row>
            <Row>
                <Col>
                    <ContractMechanicalDeductionAdminFee
                        contractMechanicalDeduction={
                            contractMechanicalDeduction
                        }
                        errors={errors}
                        handleFormChange={handleFormChange}
                        onAdminFeeSelect={(isSelected: boolean) =>
                            handleAdminFeeChange(isSelected)
                        }
                    />
                </Col>
            </Row>
        </>
    );
};
