import React from 'react';
import { isNumber } from 'lodash-es';
import {
    AbacusContractLifecycleSchedulePeriodType,
    AbacusContractLifecycleScheduleRenewalType,
} from 'src/apollo/definitions/globalTypes';
import {
    CONTRACT_RENEWAL_TYPES_SELECT,
    MAX_CONTRACT_LIFECYCLE_DETAIL_INTERVAL,
} from 'src/apollo/type-constants/contract';
import { RenewPeriodically } from 'src/components/contract-lifecycle-shared/renew-periodically';
import { RenewalTypeSelect } from 'src/components/contract-lifecycle-shared/renewal-type-select';
import { TerminationNotice } from 'src/components/contract-lifecycle-shared/termination-notice';
import type {
    ContractLifecycle,
    ContractLifecycleSchedule,
} from 'src/types/abacus-contract-lifecycle';

export interface SubsequentPeriodFormFieldsPropTypes {
    currentSchedule: ContractLifecycleSchedule;
    contractLifecycleSchedules: ContractLifecycleSchedule[];
    defaultFormValues: ContractLifecycleSchedule;
    errors: Partial<ContractLifecycle> & Partial<ContractLifecycleSchedule>;
    isDisabled?: boolean;
    isEditForm?: boolean;
    setContractLifecycleSchedules: (
        params: ContractLifecycleSchedule[]
    ) => void;
    setErrors: React.Dispatch<
        React.SetStateAction<
            Partial<ContractLifecycle> & Partial<ContractLifecycleSchedule>
        >
    >;
}

export const SubsequentPeriodFormFields: React.FC<
    SubsequentPeriodFormFieldsPropTypes
> = ({
    currentSchedule,
    contractLifecycleSchedules,
    defaultFormValues,
    errors,
    isDisabled = false,
    isEditForm = false,
    setContractLifecycleSchedules,
    setErrors,
}) => {
    const handleFormChange = (
        formField: string,
        inputValue: number | string | null | undefined
    ) => {
        setErrors({});

        if (
            [
                'renewalOffsetDetailInterval',
                'terminationNoticeDetailInterval',
            ].includes(formField)
        ) {
            if (
                isNumber(inputValue) &&
                inputValue > MAX_CONTRACT_LIFECYCLE_DETAIL_INTERVAL
            )
                return;
        }

        let updatedSchedule = { ...currentSchedule, [formField]: inputValue };
        if (formField === 'renewalType')
            updatedSchedule = {
                ...currentSchedule,
                ...defaultFormValues,
                terminationNoticeDetailInterval: !inputValue
                    ? null
                    : currentSchedule.terminationNoticeDetailInterval,
                terminationNoticeDetailType: !inputValue
                    ? AbacusContractLifecycleSchedulePeriodType.DAY
                    : currentSchedule.terminationNoticeDetailType,
                [formField]: !inputValue ? null : `${inputValue}`,
            };

        setContractLifecycleSchedules([
            contractLifecycleSchedules[0],
            updatedSchedule,
        ]);
    };

    const renewalTypeOptions = () => {
        const renewalTypeOptionsCopy = [...CONTRACT_RENEWAL_TYPES_SELECT];
        renewalTypeOptionsCopy.splice(1, 1);
        return renewalTypeOptionsCopy;
    };

    const renderRenewalTypeFormFields = () => {
        switch (currentSchedule.renewalType) {
            case AbacusContractLifecycleScheduleRenewalType.CONTINUOUSLY_ACTIVE:
                return (
                    <TerminationNotice
                        contractLifecycleSchedule={currentSchedule}
                        errors={errors}
                        handleFormChange={handleFormChange}
                        isDisabled={isDisabled}
                        isEditForm={isEditForm}
                    />
                );
            case AbacusContractLifecycleScheduleRenewalType.RENEW_PERIODICALLY:
                return (
                    <>
                        <RenewPeriodically
                            contractLifecycleSchedule={currentSchedule}
                            errors={errors}
                            handleFormChange={handleFormChange}
                            isDisabled={isDisabled}
                            isEditForm={isEditForm}
                        />
                        <TerminationNotice
                            contractLifecycleSchedule={currentSchedule}
                            errors={errors}
                            handleFormChange={handleFormChange}
                            isDisabled={isDisabled}
                            isEditForm={isEditForm}
                        />
                    </>
                );
            default:
                return null;
        }
    };

    return (
        <>
            <RenewalTypeSelect
                contractLifecycleSchedule={currentSchedule}
                errors={errors}
                handleFormChange={handleFormChange}
                isDisabled={isDisabled}
                isEditForm={isEditForm}
                renewalTypeOptions={renewalTypeOptions()}
            />
            {renderRenewalTypeFormFields()}
        </>
    );
};
