import React from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../tools/namingTools';
import { stripSlash } from '../../tools/stringTools';

import Icon from '../icon/Icon';
import Pricing from '../pricing/Pricing';
import './styles/SelectedAddon.scss';
import { getPropValue } from '../../tools/objectTools';

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 = usageRange[0].formattedFullPriceElements // eslint-disable-line
    } else {
        formattedFullPriceElements = flatPrice.formattedFullPriceElements;  // eslint-disable-line
    }

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

        const [price, unit, frequency] = formattedFullPriceElements;

        return (
            <div {...n('price').props}>
                <Pricing
                    isEmphasis
                    isMultipleLine
                    price={price}
                    caption={startingAt}
                    unit={unit && stripSlash(unit)}
                    frequency={frequency && stripSlash(frequency)}
                />
            </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: 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;
