import useAppSelector from "@insite/client-framework/Common/Hooks/useAppSelector";
import { Cart } from "@insite/client-framework/Services/CartService";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import { getSettingsCollection } from "@insite/client-framework/Store/Context/ContextSelectors";
import { getCurrentCartState } from "@insite/client-framework/Store/Data/Carts/CartsSelector";
import { getCurrentCountries } from "@insite/client-framework/Store/Data/Countries/CountriesSelectors";
import {
    CountryModel,
    PaymentMethodDto,
    PaymentOptionsDto,
    WebsiteSettingsModel,
} from "@insite/client-framework/Types/ApiModels";

export interface PaymentDetailsCartState {
    cart?: Readonly<Cart>;
    useTokenExGateway: boolean;
    useECheckTokenExGateway: boolean;
    usePaymetricGateway: boolean;
    useSquareGateway: boolean;
    useSpreedlyDropIn: boolean;
    useAdyenDropIn: boolean;
    usePaystandDropIn: boolean;
    countries?: CountryModel[];
    showPayPal: boolean;
    paymentOptions?: PaymentOptionsDto | null;
    paymentMethods?: PaymentMethodDto[] | null;
    paymentMethodDto?: PaymentMethodDto;
    isCreditCard: boolean;
    isECheck: boolean;
    isPaymentProfile: boolean;
    isPaymentProfileExpired: boolean;
    selectedCountry?: CountryModel;
    websiteSettings: WebsiteSettingsModel;
    enableGooglePay: boolean;
}

function usePaymentDetailsCartState(
    cartState: ReturnType<typeof getCurrentCartState>,
    paymentMethod: string,
    countryId: string,
): PaymentDetailsCartState {
    const { value: cart } = cartState;

    const settingsCollection = useAppSelector((state: ApplicationState) => {
        return getSettingsCollection(state);
    });
    const { websiteSettings, cartSettings } = settingsCollection;
    const {
        useTokenExGateway,
        useECheckTokenExGateway,
        usePaymetricGateway,
        useSquareGateway,
        useAdyenDropIn,
        usePaystandDropIn,
        useSpreedlyDropIn,
    } = websiteSettings;

    const countries = useAppSelector(getCurrentCountries);

    const { showPayPal, enableGooglePay } = cartSettings;
    const paymentOptions = cart ? cart.paymentOptions : undefined;
    const paymentMethods = paymentOptions ? paymentOptions.paymentMethods : undefined;

    const paymentMethodDto = paymentMethods?.find(method => method.name === paymentMethod);
    const isCreditCard = paymentMethodDto?.isCreditCard === true;
    const isECheck = paymentMethodDto?.isECheck === true;
    const isPaymentProfile = paymentMethodDto?.isPaymentProfile === true;
    const isPaymentProfileExpired = paymentMethodDto?.isPaymentProfileExpired === true;
    const selectedCountry = countries?.find(country => country.id === countryId);

    return {
        cart,
        useTokenExGateway,
        useECheckTokenExGateway,
        usePaymetricGateway,
        useSquareGateway,
        useSpreedlyDropIn,
        useAdyenDropIn,
        usePaystandDropIn,
        countries,
        showPayPal,
        paymentOptions,
        paymentMethods,
        paymentMethodDto,
        isCreditCard,
        isECheck,
        isPaymentProfile,
        isPaymentProfileExpired,
        selectedCountry,
        websiteSettings,
        enableGooglePay,
    };
}

export default usePaymentDetailsCartState;
