import { Accordion, ActionIcon, Center } from "@mantine/core";
import { IconBarcode, IconBox, IconPlayerPause, IconPlus, IconTrash } from "@tabler/icons-react";
import ReceiptProducts from "./ReceiptProducts.tsx";
import { ProductSchema, ResidualBoxSchema, ResidualPalletSchema, ResiduesService } from "../../../../../client";
import { modals } from "@mantine/modals";
import { notifications } from "../../../../../shared/lib/notifications.ts";
import { ScanningData } from "../../../hooks/useScanningMode.tsx";

type Props = {
    pallet: ResidualPalletSchema;
    clientId: number;
    fetchPallet: () => void;
    barcodesProductsMap: Map<string, ProductSchema[]>;
    scanningData: ScanningData
    toggleScanning: (boxId?: number, palletId?: number) => void;
}

const AccordionBoxesOnPallet = ({
                                    pallet,
                                    clientId,
                                    fetchPallet,
                                    barcodesProductsMap,
                                    scanningData,
                                    toggleScanning,
                                }: Props) => {
    const {
        isScanning,
        boxId,
    } = scanningData;

    const deleteBox = (boxId: number) => {
        ResiduesService.deleteResidualBox({
            boxId,
        })
            .then(({ ok, message }) => {
                if (!ok) {
                    notifications.error({ message });
                }
                fetchPallet();
            })
            .catch(err => console.log(err));
    };

    const onCreateProductClick = (box: ResidualBoxSchema) => {
        if (!(box && clientId)) return;
        modals.openContextModal({
            modal: "receiptModal",
            title: "Добавление товара",
            withCloseButton: false,
            innerProps: {
                clientId,
                isBox: true,
                object: box,
                fetchObject: fetchPallet,
                barcodesProductsMap,
            },
        });
    };

    const boxActions = (box: ResidualBoxSchema) => {
        return (
            <>
                <ActionIcon
                    variant={"default"}
                    onClick={() => toggleScanning(box.id, pallet?.id)}
                    mr={"sm"}
                    disabled={isScanning && box.id !== boxId}
                >
                    {isScanning && box.id === boxId ? (
                        <IconPlayerPause />
                    ) : (
                        <IconBarcode />
                    )}
                </ActionIcon>
                <ActionIcon
                    variant={"default"}
                    onClick={() => onCreateProductClick(box)}
                    mr={"sm"}
                    disabled={isScanning}
                >
                    <IconPlus />
                </ActionIcon>
                <ActionIcon
                    variant={"default"}
                    onClick={() => deleteBox(box.id)}
                    mr={"sm"}
                    disabled={isScanning}
                >
                    <IconTrash />
                </ActionIcon>
            </>
        );
    };

    if (pallet.boxes.length === 0) return;

    return (
        <Accordion multiple={true} bd={"solid 1px gray"}>
            {pallet.boxes.map(box => (
                <Accordion.Item key={box.id} value={box.id.toString()}>
                    <Center>
                        <Accordion.Control icon={<IconBox />}>
                            Короб {box.id}
                        </Accordion.Control>
                        {boxActions(box)}
                    </Center>
                    <Accordion.Panel>
                        <ReceiptProducts
                            clientId={clientId}
                            object={box}
                            updateObject={fetchPallet}
                            disabled={isScanning}
                        />
                    </Accordion.Panel>
                </Accordion.Item>
            ))}
        </Accordion>
    );
};

export default AccordionBoxesOnPallet;
