import React from 'react';
import PropTypes from 'prop-types';
import { NAMESPACES } from '../../constants/namespaces';
import { TOGGLE_CHECK, DROPDOWN, COLORPICKER, RADIO,
    SLIDER, INPUT, TEXTAREA, PRODUCT_GROUPS_LIST, GROUP_HEADER } from '../../constants/schemaComponentTypes';
import { LEFT, RIGHT, CENTER, SPACE_BETWEEN, SPACE_EVENLY } from '../../constants/alignment';
import { PRIMARY, SECONDARY, LINK } from '../../constants/components/schemas/button-types';
import { SMALL, MEDIUM, LARGE } from '../../constants/sizes';
import { CARD_TILE, GRAPHIC_TILE, HORIZONTAL_TILE, CARD, GRAPHIC, HORIZONTAL } from '../../constants/components/tile';
import { createNamespace } from '../../tools/namingTools';
import FlexContainer from '../../atoms/flex-container/FlexContainer';
import CardStyle from '../../atoms/product-groups-tiles/card-style/CardStyle';
import GraphicStyle from '../../atoms/product-groups-tiles/graphic-style/GraphicStyle';
import HorizontalStyle from '../../atoms/product-groups-tiles/horizontal-style/HorizontalStyle';
import withListener, { parseSettings } from '../withListener';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/ProductGroups.scss';

const n = createNamespace('ProductGroups');

export const PRODUCT_GROUP_TILE_COMPONENTS = {
    [CARD]: CardStyle,
    [GRAPHIC]: GraphicStyle,
    [HORIZONTAL]: HorizontalStyle
};

const ProductGroups = ({ data, settings }) => {
    const {
        style,
        rowGap,
        columnGap,
        contentAlignment,
        headerTitle,
        headerDescription,
        headerTextAlignment,
        displayHeaderTitle,
        displayHeaderDescription,
        containerBackgroundColor,
        productGroupsList,
        ...otherProps
    } = settings;

    const displayHeaderWrapper = displayHeaderTitle || displayHeaderDescription;

    const parsedProductGroupList = productGroupsList?.data?.map(item => parseSettings(item));

    const productGroupListWithKeys = parsedProductGroupList?.map(item => ({
        ...item,
        link: item.link ? item.link.replace(/\/[a-z]{2}-[A-Z]{2}\/listing\?/, '/listing?') : '',
        i18n: data?.i18n,
        ...otherProps
    }));

    const wrapperVariables = {
        'container-background-color': containerBackgroundColor
    };

    const headerWrapperVariables = {
        'header-text-alignment': headerTextAlignment
    };

    const component = PRODUCT_GROUP_TILE_COMPONENTS[style];

    return (
        <div {...n('wrapper').withVariables(wrapperVariables).withE2EId().props}>
            { displayHeaderWrapper
                && (
                    <div {...n('headerWrapper').withVariables(headerWrapperVariables).props}>
                        { displayHeaderTitle && <h2 {...n('headerTitle').withE2EId().props}>{headerTitle}</h2>}
                        { displayHeaderDescription && <div {...n('headerDescription').withE2EId().props}>{headerDescription}</div>}
                    </div>
                )}
            <FlexContainer
                component={component}
                flexContainerSettings={{
                    rowGap,
                    columnGap,
                    displayBorder: settings.displayBorder,
                    contentAlignment
                }}
                itemList={productGroupListWithKeys}
            />
        </div>
    );
};

const STYLE_OPTIONS = [CARD_TILE, GRAPHIC_TILE, HORIZONTAL_TILE];
const SIZES_OPTIONS = [SMALL, MEDIUM, LARGE];
const ALIGN_OPTIONS = [LEFT, CENTER, RIGHT];
const FLEX_CONTAINER_ALIGN_OPTIONS = [LEFT, CENTER, RIGHT, SPACE_BETWEEN, SPACE_EVENLY];
const BUTTON_TYPES = [PRIMARY, SECONDARY, LINK];

const productGroupsForm = {
    productGroupsTabTitle: {
        title: 'uieditor.sfbComponent.productGroups.tabTitle.title',
        defaultValue: 'uieditor.sfbComponent.productGroups.title',
        type: INPUT,
        isLocalized: true
    },
    productGroupsList: {
        title: 'uieditor.sfbComponent.productGroups.title',
        type: PRODUCT_GROUPS_LIST
    },
    style: {
        title: 'uieditor.sfbComponent.productGroups.style.title',
        type: RADIO,
        defaultValue: STYLE_OPTIONS[0].value,
        options: STYLE_OPTIONS
    },
    size: {
        title: 'uieditor.sfbComponent.productGroups.size.title',
        type: RADIO,
        defaultValue: SIZES_OPTIONS[1].value,
        options: SIZES_OPTIONS
    },
    rowGap: {
        title: 'uieditor.sfbComponent.productGroups.rowSpacing.title',
        type: SLIDER,
        defaultValue: 28,
        options: {
            range: true,
            min: 0,
            max: 80,
            step: 1
        }
    },
    columnGap: {
        title: 'uieditor.sfbComponent.productGroups.columnSpacing.title',
        type: SLIDER,
        defaultValue: 28,
        options: {
            range: true,
            min: 0,
            max: 80,
            step: 1
        }
    },
    contentAlignment: {
        title: 'uieditor.sfbComponent.productGroups.contentAlignment.title',
        type: DROPDOWN,
        defaultValue: FLEX_CONTAINER_ALIGN_OPTIONS[1].value,
        options: FLEX_CONTAINER_ALIGN_OPTIONS
    },
    displaySettings: {
        type: GROUP_HEADER,
        title: 'uieditor.sfbComponent.productGroups.displaySettings.title',
        symbol: 'style'
    },
    displayHeaderTitle: {
        label: 'uieditor.sfbComponent.productGroups.displayHeaderTitle.label',
        defaultValue: true,
        type: TOGGLE_CHECK,
        marginSize: 10
    },
    displayHeaderDescription: {
        label: 'uieditor.sfbComponent.productGroups.displayHeaderDescription.label',
        defaultValue: true,
        type: TOGGLE_CHECK,
        marginSize: 10
    },
    displayUpperTitle: {
        label: 'uieditor.sfbComponent.productGroups.displayUpperTitle.label',
        defaultValue: true,
        type: TOGGLE_CHECK,
        marginSize: 10,
        conditionalAttributes: {
            isVisible: {
                when: 'style',
                connector: 'equals',
                value: CARD_TILE.value
            }
        }
    },
    displayTitle: {
        label: 'uieditor.sfbComponent.productGroups.displayTitle.label',
        defaultValue: true,
        type: TOGGLE_CHECK,
        marginSize: 10
    },
    displayDescription: {
        label: 'uieditor.sfbComponent.productGroups.displayDescription.label',
        defaultValue: true,
        type: TOGGLE_CHECK,
        marginSize: 10
    },
    displayButton: {
        label: 'uieditor.sfbComponent.productGroups.displayButton.label',
        defaultValue: true,
        type: TOGGLE_CHECK,
        marginSize: 10
    },
    componentSettings: {
        type: GROUP_HEADER,
        title: 'uieditor.sfbComponent.productGroups.componentSettings.title',
        symbol: 'style'
    },
    headerTitle: {
        title: 'uieditor.sfbComponent.productGroups.headerTitle.title',
        type: INPUT,
        defaultValue: 'uieditor.sfbComponent.productGroups.headerTitle.default',
        isLocalized: true
    },
    headerDescription: {
        title: 'uieditor.sfbComponent.productGroups.headerDescription.title',
        defaultValue: 'uieditor.sfbComponent.productGroups.headerDescription.placeholder',
        type: TEXTAREA,
        isLocalized: true
    },
    headerTextAlignment: {
        title: 'uieditor.sfbComponent.productGroups.headerTextAlignment.title',
        type: DROPDOWN,
        defaultValue: ALIGN_OPTIONS[0].value,
        options: ALIGN_OPTIONS
    },
    containerBackgroundColor: {
        title: 'uieditor.sfbComponent.productGroups.containerBackgroundColor.title',
        type: COLORPICKER,
        defaultValue: '#EAEAEA'
    },
    contentSettings: {
        type: GROUP_HEADER,
        title: 'uieditor.sfbComponent.productGroups.contentSettings.title',
        symbol: 'style'
    },
    upperTitleColor: {
        title: 'uieditor.sfbComponent.productGroups.upperTitleColor.title',
        type: COLORPICKER,
        defaultValue: '#474747'
    },
    titleColor: {
        title: 'uieditor.sfbComponent.productGroups.titleColor.title',
        type: COLORPICKER,
        defaultValue: '#474747'
    },
    descriptionColor: {
        title: 'uieditor.sfbComponent.productGroups.descriptionColor.title',
        type: COLORPICKER,
        defaultValue: '#474747'
    },
    imageBackgroundColor: {
        title: 'uieditor.sfbComponent.productGroups.imageBackgroundColor.title',
        type: COLORPICKER,
        defaultValue: '#D8D8D8'
    },
    cardBackgroundColor: {
        title: 'uieditor.sfbComponent.productGroups.cardBackgroundColor.title',
        type: COLORPICKER,
        defaultValue: '#ffffff',
        conditionalAttributes: {
            isVisible: {
                when: 'style',
                connector: 'notEquals',
                value: GRAPHIC_TILE.value
            }
        }
    },
    cardBorderColor: {
        title: 'uieditor.sfbComponent.productGroups.cardBorderColor.title',
        type: COLORPICKER,
        defaultValue: '#ffffff'
    },
    shadowColor: {
        title: 'uieditor.sfbComponent.productGroups.shadowColor.title',
        type: COLORPICKER,
        defaultValue: 'rgba(0,1,0,0.2)'
    },
    upperTitleAlignment: {
        title: 'uieditor.sfbComponent.productGroups.upperTitleAlignment.label',
        type: DROPDOWN,
        defaultValue: ALIGN_OPTIONS[0].value,
        options: ALIGN_OPTIONS
    },
    textAlignment: {
        title: 'uieditor.sfbComponent.productGroups.textAlignment.label',
        type: DROPDOWN,
        defaultValue: ALIGN_OPTIONS[1].value,
        options: ALIGN_OPTIONS
    },
    buttonAlignment: {
        title: 'uieditor.sfbComponent.productGroups.buttonAlignment.label',
        type: DROPDOWN,
        defaultValue: ALIGN_OPTIONS[1].value,
        options: ALIGN_OPTIONS
    },
    buttonType: {
        title: 'uieditor.sfbComponent.productGroups.buttonType.title',
        type: RADIO,
        defaultValue: BUTTON_TYPES[0].value,
        options: BUTTON_TYPES
    },
    displayBorder: {
        label: 'uieditor.sfbComponent.productGroups.displayBorder.label',
        defaultValue: true,
        type: TOGGLE_CHECK,
        marginSize: 10
    },
    onHoverBorderOnly: {
        label: 'uieditor.sfbComponent.productGroups.onHoverBorderOnly.label',
        defaultValue: false,
        type: TOGGLE_CHECK,
        indentSize: 1,
        marginSize: 10,
        conditionalAttributes: {
            isVisible: {
                when: 'displayBorder',
                connector: 'equals',
                value: true
            }
        }
    },
    displayShadow: {
        label: 'uieditor.sfbComponent.productGroups.displayShadow.label',
        defaultValue: true,
        type: TOGGLE_CHECK,
        marginSize: 10
    },
    onHoverShadowOnly: {
        label: 'uieditor.sfbComponent.productGroups.onHoverShadowOnly.label',
        defaultValue: false,
        type: TOGGLE_CHECK,
        indentSize: 1,
        marginSize: 10,
        conditionalAttributes: {
            isVisible: {
                when: 'displayShadow',
                connector: 'equals',
                value: true
            }
        }
    },
    imageCornerRadius: {
        title: 'uieditor.sfbComponent.productGroups.imageCornerRadius.title',
        type: SLIDER,
        defaultValue: 0,
        options: {
            range: true,
            min: 0,
            max: 100,
            step: 1
        }
    },
    cardCornerRadius: {
        title: 'uieditor.sfbComponent.productGroups.cardCornerRadius.title',
        type: SLIDER,
        defaultValue: 0,
        options: {
            range: true,
            min: 0,
            max: 100,
            step: 1
        },
        conditionalAttributes: {
            isVisible: {
                when: 'style',
                connector: 'notEquals',
                value: GRAPHIC_TILE.value
            }
        }
    }
};

ProductGroups.schema = () => ({
    name: 'ProductGroups',
    title: 'uieditor.sfbComponent.productGroups.title',
    iconName: 'productGroups',
    componentTitleKey: 'productGroupsTabTitle',
    orderableSection: [
        NAMESPACES.home
    ],
    form: productGroupsForm,
    marketplaceData: {
        i18n: 'i18n.productGroups'
    }
});

ProductGroups.propTypes = {
    settings: PropTypes.shape({
        style: PropTypes.string,
        rowGap: PropTypes.number,
        columnGap: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
        contentAlignment: PropTypes.string,
        headerTextAlignment: PropTypes.string,
        displayHeaderTitle: PropTypes.bool,
        displayHeaderDescription: PropTypes.bool,
        size: PropTypes.string,
        headerTitle: PropTypes.string,
        headerDescription: PropTypes.string,
        imageBackgroundColor: PropTypes.string,
        displayUpperTitle: PropTypes.bool,
        upperTitleAlignment: PropTypes.string,
        buttonAlignment: PropTypes.string,
        textAlignment: PropTypes.string,
        displayTitle: PropTypes.bool,
        displayDescription: PropTypes.bool,
        cardBackgroundColor: PropTypes.string,
        displayButton: PropTypes.bool,
        buttonType: PropTypes.string,
        cardCornerRadius: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
        imageCornerRadius: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
        cardBorderColor: PropTypes.string,
        displayShadow: PropTypes.bool,
        displayBorder: PropTypes.bool,
        onHoverShadowOnly: PropTypes.bool,
        onHoverBorderOnly: PropTypes.bool,
        upperTitleColor: PropTypes.string,
        titleColor: PropTypes.string,
        descriptionColor: PropTypes.string,
        shadowColor: PropTypes.string,
        containerBackgroundColor: PropTypes.string,
        productGroupsList: PropTypes.shape({
            name: PropTypes.string,
            data: PropTypes.array
        })
    }),
    data: PropTypes.object
};

ProductGroups.defaultProps = {
    settings: {
        style: CARD
    },
    data: {}
};

export default withListener(ProductGroups);
