import {
    createInitialDesignDataFromDesignAreas,
    recalculateDesignAreas,
    recalculateSingleDesignArea,
} from '../Transformers';
import {
    designProductionMethodMap,
    expectedPosition,
    expectedBaseShape,
    recalculateMockState,
    visualMode,
} from '../__mocks__/recalculateAreaData';
import { setConf } from '../../../configuration';

const mockDesignAreas = [
    {
        identifier: 'front',
        sequenceNumber: '10',
        title: 'Vorne',
        baseShape: {},
        position: {},
        designProductionMethods: [
            {
                identifier: 'print',
                title: 'Druck',
                height: 15,
                width: 10,
                options: { maxColorAmount: 5 },
                colorPalettes: [
                    {
                        identifier: 'default',
                        title: 'Standardfarben',
                        colors: [],
                    },
                ],
                customData: 'null',
            },
            {
                identifier: 'digital_print',
                title: 'Digitaldruck',
                height: 15,
                width: 10,
                options: { maxColorAmount: 8 },
                isDefault: true,
                colorPalettes: [
                    {
                        identifier: 'default',
                        title: 'Standardfarben',
                        colors: [],
                    },
                ],
                customData: 'null',
            },
        ],
        customData: {},
    },
    {
        identifier: 'sleeve',
        sequenceNumber: '20',
        title: '\u00c4rmel',
        baseShape: {},
        position: {},
        designProductionMethods: [
            {
                identifier: 'print',
                title: 'Druck',
                height: 20,
                width: 25,
                options: { minFontSize: 5 },
                colorPalettes: [
                    {
                        identifier: 'default',
                        title: 'Standardfarben',
                        colors: [],
                    },
                ],
                customData: 'null',
            },
            {
                identifier: 'digital_print',
                title: 'Digitaldruck',
                height: 15,
                width: 10,
                options: { maxColorAmount: 8 },
                colorPalettes: [
                    {
                        identifier: 'default',
                        title: 'Standardfarben',
                        colors: [],
                    },
                ],
                customData: 'null',
            },
        ],
        customData: {},
    },
];

describe('Reducers/DesignArea/Transformers', () => {
    test('recalculateDesignAreas calculates the correct size', () => {
        const recalculatedArea = recalculateDesignAreas(
            recalculateMockState.designArea,
            designProductionMethodMap,
            visualMode
        );
        expect(recalculatedArea.filteredDesignAreas[0].position).toEqual(expectedPosition);
        expect(recalculatedArea.filteredDesignAreas[0].baseShape).toEqual(expectedBaseShape);
    });

    test('recalculateSingleDesignArea adjusts the design area size to the selected printing method size', () => {
        const state = {
            configurator: {
                configuration: {
                    designdata: {
                        apron: {
                            canvasData: { objects: [] },
                            colorAmount: 3,
                            designProductionMethodIdentifier: '042-EMB001-180cm',
                        },
                    },
                    item: {
                        settings: {
                            visualizationDesigner: '2d',
                        },
                    },
                },
            },
            configurationMode: {
                selectedMode: 'designer',
            },
        };
        const designArea = {
            height: 300,
            width: 200,
            baseShape: { type: 'Plane', x: 200, y: 150, width: 200, height: 300 },
            position: { x: 150, y: 200, width: 200, height: 300 },
            designProductionMethods: [
                { identifier: '042-EMB001-180cm', title: 'Bestickung', mask: null, height: 270, width: 180 },
            ],
        };
        const expectedResult = {
            height: 300,
            width: 200,
            baseShape: { type: 'Plane', x: 200, y: 150, width: 180, height: 270 },
            position: { x: 135, y: 180, width: 180, height: 270 },
            designProductionMethods: [
                { identifier: '042-EMB001-180cm', title: 'Bestickung', mask: null, height: 270, width: 180 },
            ],
        };
        expect(recalculateSingleDesignArea(state, designArea)).toEqual(expectedResult);
    });

    test('createInitialDesignDataFromDesignAreas returns the correct design data for the given input', () => {
        const initialDesignData = createInitialDesignDataFromDesignAreas(mockDesignAreas);
        expect(initialDesignData).toEqual({
            front: {
                canvasData: null, // should be null initially
                designProductionMethodIdentifier: 'digital_print', // should be the id. of the default design production method
                colorAmount: 1, // if there is a maxColorAmount set the colorAmount to the minimum (1)
            },
            sleeve: {
                canvasData: null,
                designProductionMethodIdentifier: 'print', // should be the id. of the first design production method
                // if there is no maxColorAmount do not set the colorAmount
            },
        });
    });

    test('createInitialDesignDataFromDesignAreas select the default design production method if other provided, but the canvasData is empty', () => {
        const designData = {
            front: {
                designProductionMethodIdentifier: 'print',
                canvasData: null,
            },
        };
        const initialDesignData = createInitialDesignDataFromDesignAreas(mockDesignAreas, designData);
        expect(initialDesignData).toEqual({
            front: {
                canvasData: null,
                designProductionMethodIdentifier: 'digital_print', // should be the id. of the default design production method
                colorAmount: 1, // if there is a maxColorAmount set the colorAmount to the minimum (1)
            },
            sleeve: {
                canvasData: null,
                designProductionMethodIdentifier: 'print', // should be the id. of the first design production method
                // if there is no maxColorAmount do not set the colorAmount
            },
        });
    });

    test('createInitialDesignDataFromDesignAreas keeps the selected design production method if the designData is not empty', () => {
        const designData = {
            front: {
                designProductionMethodIdentifier: 'print',
                canvasData: { objects: [{ type: 'Image' }] },
            },
        };
        const initialDesignData = createInitialDesignDataFromDesignAreas(mockDesignAreas, designData);
        expect(initialDesignData).toEqual({
            front: {
                canvasData: { objects: [{ type: 'Image' }] },
                designProductionMethodIdentifier: 'print', // should be the id. of the selected design production method
                colorAmount: 1, // if there is a maxColorAmount set the colorAmount to the minimum (1)
            },
            sleeve: {
                canvasData: null,
                designProductionMethodIdentifier: 'print', // should be the id. of the first design production method
                // if there is no maxColorAmount do not set the colorAmount
            },
        });
    });

    test('createInitialDesignDataFromDesignAreas keeps the colorAmount method if the designData is not empty', () => {
        const designData = {
            front: {
                colorAmount: 6,
            },
        };

        const initialDesignData = createInitialDesignDataFromDesignAreas(mockDesignAreas, designData);
        expect(initialDesignData).toEqual({
            front: {
                canvasData: null,
                designProductionMethodIdentifier: 'digital_print',
                colorAmount: 6,
            },
            sleeve: {
                canvasData: null,
                designProductionMethodIdentifier: 'print',
            },
        });
    });

    test('createInitialDesignDataFromDesignAreas returns the correct design data for the given input and preselection', () => {
        const mockPreSelection = {
            designAreas: [{ identifier: 'front', colorAmount: 2, productionMethod: 'print' }],
        };

        setConf('designerPreSelection', mockPreSelection);

        const initialDesignData = createInitialDesignDataFromDesignAreas(mockDesignAreas);
        expect(initialDesignData).toEqual({
            front: {
                canvasData: null,
                designProductionMethodIdentifier: 'print',
                colorAmount: 2,
            },
            sleeve: {
                canvasData: null,
                designProductionMethodIdentifier: 'print',
            },
        });
    });
});
