import { PageLinkModel } from "@insite/client-framework/Services/ContentService";
import { BaseAddressModel, PaymentMethodDto } from "@insite/client-framework/Types/ApiModels";
import { PaymentDetailsCartState } from "@insite/content-library/Hooks/Payments/usePaymentDetailsCartState";
import { UsePaymentDetailsFormState } from "@insite/content-library/Hooks/Payments/usePaymentDetailsFormState";
import { UsePaymentDetailsFormValidation } from "@insite/content-library/Hooks/Payments/usePaymentDetailsFormValidation";
import { History } from "@insite/mobius/utilities/HistoryContext";

export interface UsePaymentDetailsFormHandlers {
    handlePaymentMethodChange: (event: React.ChangeEvent<HTMLSelectElement>) => void;
    handlePONumberChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
    handleVatNumberChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
    handleSaveCardChange: (value: boolean) => void;
    handleCardHolderNameChange: (value: string) => void;
    handleCardNumberChange: (value: string) => void;
    handleCardTypeChange: (value: string) => void;
    handleExpirationMonthChange: (value: number) => void;
    handleExpirationYearChange: (value: number) => void;
    handleSecurityCodeChange: (value: string) => void;
    handleUseBillingAddressChange: (_: React.SyntheticEvent<Element, Event>, value: boolean) => void;
    handleAddressChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
    handleCountryChange: (event: React.ChangeEvent<HTMLSelectElement>, country: BaseAddressModel["country"]) => void;
    handleStateChange: (event: React.ChangeEvent<HTMLSelectElement>, state: BaseAddressModel["state"]) => void;
    handleCityChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
    handlePostalCodeChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
    handleEditCardClick: () => void;
}
export interface UsePaymentDetailsFormHandlersParameter {
    paymentDetailsFormState: UsePaymentDetailsFormState;
    paymentDetailsCartState: PaymentDetailsCartState;
    paymentDetailsFormValidation: UsePaymentDetailsFormValidation;
    setPaymentMethodCallback: (
        event: React.ChangeEvent<HTMLSelectElement>,
        paymentMethods: PaymentMethodDto[] | null | undefined,
    ) => void;
    savedPaymentsPageLink?: Readonly<PageLinkModel>;
    history: History;
}
function usePaymentDetailsFormHandlers(
    usePaymentDetailsFormHandlersParameter: UsePaymentDetailsFormHandlersParameter,
): UsePaymentDetailsFormHandlers {
    const {
        paymentDetailsFormState,
        paymentDetailsCartState,
        paymentDetailsFormValidation,
        history,
        savedPaymentsPageLink,
        setPaymentMethodCallback,
    } = usePaymentDetailsFormHandlersParameter;
    const handlePaymentMethodChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
        if (event.currentTarget.value !== "PayPal") {
            paymentDetailsFormState.setIsPayPal(false);
        }
        paymentDetailsFormState.setPaymentMethod(event.currentTarget.value);
        paymentDetailsFormValidation.validatePaymentMethod(event.currentTarget.value);

        setPaymentMethodCallback(event, paymentDetailsCartState.paymentMethods);
    };
    const handlePONumberChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        paymentDetailsFormState.setPONumber(event.currentTarget.value);
        paymentDetailsFormValidation.validatePONumber(event.currentTarget.value);
    };
    const handleVatNumberChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        paymentDetailsFormState.setVatNumber(event.currentTarget.value);
    };
    const handleSaveCardChange = (value: boolean) => paymentDetailsFormState.setSaveCard(value);

    const handleCardHolderNameChange = (value: string) => {
        paymentDetailsFormState.setCardHolderName(value);
    };
    const handleCardNumberChange = (value: string) => {
        paymentDetailsFormState.setCardNumber(value);
    };
    const handleCardTypeChange = (value: string) => {
        paymentDetailsFormState.setCardType(value);
    };
    const handleExpirationMonthChange = (value: number) => {
        const month = Number(value);
        if (Number.isNaN(month)) {
            return;
        }
        paymentDetailsFormState.setExpirationMonth(month);
    };
    const handleExpirationYearChange = (value: number) => {
        const year = Number(value);
        if (Number.isNaN(year)) {
            return;
        }
        paymentDetailsFormState.setExpirationYear(year);
    };
    const handleSecurityCodeChange = (value: string) => {
        paymentDetailsFormState.setSecurityCode(value);
    };

    const handleUseBillingAddressChange = (_: React.SyntheticEvent<Element, Event>, value: boolean) =>
        paymentDetailsFormState.setUseBillingAddress(value);
    const handleAddressChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        paymentDetailsFormState.setAddress1(event.currentTarget.value);
        paymentDetailsFormValidation.validateAddress1(event.currentTarget.value);
    };
    const handleCountryChange = (event: React.ChangeEvent<HTMLSelectElement>, country: BaseAddressModel["country"]) => {
        paymentDetailsFormState.setCountryId(country?.id || "");
        paymentDetailsFormValidation.validateCountry(country?.id || "");
        paymentDetailsFormState.setStateId("");
    };
    const handleStateChange = (event: React.ChangeEvent<HTMLSelectElement>, state: BaseAddressModel["state"]) => {
        paymentDetailsFormState.setStateId(state?.id || "");
        paymentDetailsFormValidation.validateState(state?.id || "");
    };
    const handleCityChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        paymentDetailsFormState.setCity(event.currentTarget.value);
        paymentDetailsFormValidation.validateCity(event.currentTarget.value);
    };
    const handlePostalCodeChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        paymentDetailsFormState.setPostalCode(event.currentTarget.value);
        paymentDetailsFormValidation.validatePostalCode(event.currentTarget.value);
    };

    const handleEditCardClick = () => {
        if (!savedPaymentsPageLink) {
            return;
        }

        history.push(savedPaymentsPageLink.url);
    };
    return {
        handlePaymentMethodChange,
        handlePONumberChange,
        handleVatNumberChange,
        handleSaveCardChange,
        handleCardHolderNameChange,
        handleCardNumberChange,
        handleCardTypeChange,
        handleExpirationMonthChange,
        handleExpirationYearChange,
        handleSecurityCodeChange,
        handleUseBillingAddressChange,
        handleAddressChange,
        handleCountryChange,
        handleStateChange,
        handleCityChange,
        handlePostalCodeChange,
        handleEditCardClick,
    };
}

export default usePaymentDetailsFormHandlers;
