import React from 'react';
import PropTypes from 'prop-types';
import DOMPurify from 'dompurify';
import { createNamespace } from '../../tools/namingTools';
import { stripSlash } from '../../tools/stringTools';
import Icon from '../icon/Icon';
import Pricing from '../pricing/Pricing';
import { getPropValue } from '../../tools/objectTools';
import { getFullPriceSegments } from '../../tools/components/pricing';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/SelectedAddon.scss';

const n = createNamespace('SelectedAddon');

const SelectedAddon = ({
    data: { edition, name, description },
    i18n: { startingAt },
    showPricing,
    showDescription
}) => {
    const [{ plans }] = edition;
    const unitPrice = getPropValue(plans[0], 'costs.unitPrice');
    const flatPrice = getPropValue(plans[0], 'costs.flatPrice', { formattedFullPriceElements: '' });

    let formattedFullPriceElements;
    if (unitPrice && unitPrice.length) {
        const [{ usageRange = [{}] }] = unitPrice;
        formattedFullPriceElements = getFullPriceSegments(usageRange[0]); // eslint-disable-line
    } else {
        formattedFullPriceElements = getFullPriceSegments(flatPrice);  // eslint-disable-line
    }

    const renderPrice = () => {
        if (!formattedFullPriceElements || !formattedFullPriceElements.length) {
            return '';
        }

        const [price, unit, frequency, feeType = ''] = formattedFullPriceElements;
        const isConsistentDisplay = formattedFullPriceElements.length === 4;

        return (
            <div {...n('price').props}>
                <Pricing
                    isEmphasis
                    isMultipleLine
                    price={price}
                    caption={startingAt}
                    unit={unit && stripSlash(unit)}
                    frequency={frequency && stripSlash(frequency)}
                    feeType={feeType && stripSlash(feeType)}
                    isConsistentDisplay={isConsistentDisplay}
                />
            </div>
        );
    };

    return (
        <div {...n('wrapper').props}>
            <div {...n('details', formattedFullPriceElements && 'with-pricing').props}>
                <div {...n('thumbnailWrapper').props}>
                    <Icon type="addon" {...n('thumbnail').withTestId().props} />
                </div>
                <div {...n('content').props}>
                    <h3 {...n('title').withTestId().props}>{name}</h3>
                    {showDescription && description && (
                        <div {...n('description').withTestId().props} dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(description) }} />
                    )}
                </div>
            </div>
            {showPricing && renderPrice()}
        </div>
    );
};

SelectedAddon.propTypes = {
    data: PropTypes.shape({
        name: PropTypes.string,
        description: PropTypes.string,
        edition: PropTypes.arrayOf(PropTypes.shape({
            name: PropTypes.string,
            costs: PropTypes.shape({
                unitPrice: PropTypes.arrayOf(PropTypes.shape({
                    usageRange: PropTypes.arrayOf(PropTypes.shape({
                        formattedFullPriceElements: PropTypes.array
                    }))
                }))
            })
        }))
    }),
    i18n: PropTypes.shape({
        startingAt: PropTypes.string
    }),
    showPricing: PropTypes.bool,
    showDescription: PropTypes.bool
};

SelectedAddon.defaultProps = {
    data: {
        name: '',
        description: '',
        edition: [{
            name: '',
            plans: [{
                costs: {
                    unitPrice: [{
                        usageRange: [{
                            formattedFullPriceElements: []
                        }]
                    }]
                }
            }]
        }]
    },
    i18n: {
        startingAt: 'Starting at'
    },
    showPricing: true,
    showDescription: true
};

export default SelectedAddon;
