import React from 'react';
import PropTypes from 'prop-types';
import withListener from '../withListener';
import { createNamespace } from '../../tools/namingTools';
import { addonShape } from '../../constants/components/prop-types/addon';
import { editionsPropTypes } from '../../constants/prop-types/application/editions';
import { ProductPlanComponent } from '../product-plan/ProductPlan';
import ProductEditionList from '../../atoms/product-edition/list/ProductEditionList';
import ProductAddonList from '../../atoms/product-addon/list/ProductAddonList';
import { parseToBool } from '../../tools/helpers';
import { TOGGLE } from '../../constants/schemaComponentTypes';

const n = createNamespace('ProductPricing');

const ProductPricing = ({ data, settings }) => {
    const {
        editions = {},
        addonProducts = {},
        locale,
        productUuid,
        applicationType,
        user,
        crossSellManagementEnabled
    } = data;

    const i18n = { ...ProductPricing.defaultProps.data.editionsI18n, ...data.editionsI18n, cart: data?.i18n?.cart };

    const [isIncludedHidden] = parseToBool(settings.isIncludedHidden);

    const hasAddons = !!(addonProducts?.items?.length);

    return (
        <div {...n('container').props}>
            <div {...n('section').props}>
                <div {...n('editions').props}>
                    <ProductEditionList
                        data={{ editions: editions.items,
                            i18n,
                            locale,
                            productUuid,
                            applicationType,
                            user,
                            crossSellManagementEnabled }}
                        hasAddons={hasAddons}
                        hiddenTitle
                        showCode={settings.showCode}
                        isIncludedHidden={isIncludedHidden}
                    />
                </div>
                <div {...n('plans').props}>
                    <ProductPlanComponent data={editions} />
                </div>
            </div>
            { !!addonProducts.items.length && (
                <div {...n('section', 'alt-bg').props}>
                    <div {...n('addons').props}>
                        <ProductAddonList
                            data={{
                                addonProducts: {
                                    items: addonProducts.items,
                                    i18n
                                }
                            }}
                        />
                    </div>
                </div>
            )}
        </div>
    );
};

ProductPricing.propTypes = {
    data: PropTypes.shape({
        editions: PropTypes.shape(editionsPropTypes),
        addonProducts: addonShape,
        editionsI18n: PropTypes.shape({
            optionalAddOns: PropTypes.string
        }),
        locale: PropTypes.string,
        productUuid: PropTypes.string,
        applicationType: PropTypes.string,
        user: PropTypes.object,
        i18n: PropTypes.object,
        crossSellManagementEnabled: PropTypes.bool
    }),
    settings: PropTypes.shape({
        isIncludedHidden: PropTypes.bool,
        showCode: PropTypes.bool
    })
};

ProductPricing.schema = () => ({
    name: 'ProductPricing',
    title: 'uieditor.sfbComponent.productPricing.title',
    form: {
        showCode: {
            defaultValue: false,
            labelOff: 'uieditor.sfbComponent.productPricing.showCode.labelOff',
            labelOn: 'uieditor.sfbComponent.productPricing.showCode.labelOn',
            type: TOGGLE
        },
        isIncludedHidden: {
            defaultValue: false,
            labelOff: 'uieditor.sfbComponent.productPricing.isIncludedHidden.labelOff',
            labelOn: 'uieditor.sfbComponent.productPricing.isIncludedHidden.labelOn',
            type: TOGGLE
        }
    },
    marketplaceData: {
        editions: 'application.editions',
        addonProducts: 'application.addonProducts',
        editionsI18n: 'application.editions.i18n',
        locale: 'config.locale.code',
        productUuid: 'application.config.uuid',
        applicationType: 'application.config.type',
        user: 'config.user',
        i18n: 'i18n',
        crossSellManagementEnabled: 'application.crossSellManagementEnabled'
    }
});

const SETTINGS = ProductPricing.schema().form;

ProductPricing.defaultProps = {
    data: {
        addonProducts: [],
        editions: {},
        editionsI18n: {
            optionalAddOns: 'Optional Add-Ons'
        },
        i18n: {
            cart: {
                addedToCart: 'Added to Cart'
            }
        },
        locale: 'en-US',
        applicationType: 'WEB_APP',
        user: {
            isLoggedIn: false
        },
        crossSellManagementEnabled: false
    },
    settings: {
        isIncludedHidden: SETTINGS.isIncludedHidden.defaultValue
    }
};

export default withListener(ProductPricing);
