import mergeToNew from "@insite/client-framework/Common/mergeToNew";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import sendVmiBinsPdf from "@insite/client-framework/Store/Pages/VmiLocationDetails/Handlers/SendVmiBinsPdf";
import translate from "@insite/client-framework/Translate";
import { PaginationModel } from "@insite/client-framework/Types/ApiModels";
import ProductListPrintModal, {
    ProductListPrintModalStyles,
    productListPrintModalStyles,
    ProductListPrintParameters,
} from "@insite/content-library/Components/ProductListPrintModal";
import { HasToasterContext, withToaster } from "@insite/mobius/Toast/ToasterContext";
import React from "react";
import { connect, ResolveThunks } from "react-redux";

const mapStateToProps = (state: ApplicationState) => {
    return { currentParameters: state.pages.vmiLocationDetails.getVmiBinsParameter };
};

const mapDispatchToProps = {
    sendVmiBinsPdf,
};

type OwnProps = {
    lineCollection: { pagination: Pick<PaginationModel, "totalItemCount" | "numberOfPages"> | null };
    isOpen: boolean;
    initialPageSize: number;
    handleClose(): void;
    styles?: VmiPrintModalStyles;
    selectedIds?: string[];
};

type Props = OwnProps &
    ResolveThunks<typeof mapDispatchToProps> &
    ReturnType<typeof mapStateToProps> &
    HasToasterContext;

export interface VmiPrintModalStyles extends ProductListPrintModalStyles {}
export const vmiPrintModalStyles: VmiPrintModalStyles = {
    ...productListPrintModalStyles,
};

const VmiPrintModal: React.FC<Props> = ({
    lineCollection,
    isOpen,
    handleClose,
    styles,
    sendVmiBinsPdf,
    currentParameters,
    toaster,
    selectedIds,
}) => {
    const totalItemCount = lineCollection?.pagination?.totalItemCount || 999;
    const mergeStyles = mergeToNew(vmiPrintModalStyles, styles);

    const handlePrintClick = (parameters: ProductListPrintParameters) => {
        const { productsToPrint, layout, barcodeType, barcodeData, productPropertiesToPrint } = parameters;
        const baseParameter = {
            ...currentParameters,
            page: productsToPrint === "all" || productsToPrint === "selected" ? 1 : currentParameters.page,
            pageSize:
                productsToPrint === "all" || productsToPrint === "selected"
                    ? totalItemCount
                    : currentParameters.pageSize ?? currentParameters.defaultPageSize,
        };
        const parameter = {
            layout,
            barcodeFormat: barcodeType,
            barcodeField: barcodeData,
            displayFields: productPropertiesToPrint,
            lineIds: productsToPrint === "selected" ? selectedIds : undefined,
            baseParameter,
            onSuccess: () => {
                toaster.addToast({
                    body: translate(
                        "Your request has been received. Your list will be sent to the email address on your account.",
                    ),
                    messageType: "success",
                    timeoutLength: 6000,
                });
                handleClose();
            },
            onError: () => {
                toaster.addToast({
                    body: translate(
                        "Something went wrong during the request to generate your PDF. Please try again or contact us.",
                    ),
                    messageType: "danger",
                    timeoutLength: 6000,
                });
            },
        };
        sendVmiBinsPdf(parameter);
    };

    const handleCancelClick = () => {
        handleClose();
    };

    return (
        <ProductListPrintModal
            currentParameters={currentParameters}
            lineCollection={lineCollection}
            handleClose={handleClose}
            isOpen={isOpen}
            handleSubmit={handlePrintClick}
            handleCancelClick={handleCancelClick}
            variant="VMI"
            styles={mergeStyles}
            selectedIds={selectedIds}
        />
    );
};

export default connect(mapStateToProps, mapDispatchToProps)(withToaster(VmiPrintModal));
