import React from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../tools/namingTools';
import { LAYOUT_VERTICAL, LAYOUT_HORIZONTAL } from '../../constants/layouts';
import { generateUniqueID } from '../../tools/helpers';
import Image from '../../atoms/image/Image';
import ButtonIcon from '../../atoms/button-icon/ButtonIcon';

import './styles/ProductFeaturedQuotes.scss';

const n = createNamespace('ProductFeaturedQuotes');

const ProductFeaturedQuotes = ({
    data: {
        featured,
        layout,
        i18n,
        logoSize,
        logoSpacing
    }
}) => {
    const i18nWithDefaults = { ...ProductFeaturedQuotes.defaultProps.data.i18n, ...i18n };

    const componentVariables = {
        'logo-size': Boolean(logoSize) && `${logoSize}px`,
        'logo-spacing': Boolean(logoSpacing) && `${logoSpacing}px`
    };

    const renderFeaturedQuote = ({ link, linkDescription, source }) => {
        const { id, name, logoUrl } = source;

        return (
            <div {...n('quote', layout).withVariables(componentVariables).props} key={generateUniqueID([id, name])}>
                {logoUrl && (
                    <Image {...n('image').props} src={logoUrl} alt={name} />
                )}
                <div {...n('content').props}>
                    {linkDescription && (
                        <blockquote {...n('description').props}>{linkDescription}</blockquote>
                    )}
                    {link && (
                        <ButtonIcon theme="secondary" url={link} extraClass={n('button').props.className}>{i18nWithDefaults.labels.readMore}</ButtonIcon>
                    )}
                </div>
            </div>
        );
    };

    return featured && featured.length ? (
        <div {...n('wrapper').props}>
            {featured.map(renderFeaturedQuote)}
        </div>
    ) : '';
};

export const productQuoteShape = {
    link: PropTypes.string,
    linkDescription: PropTypes.string,
    source: PropTypes.shape({
        id: PropTypes.number,
        name: PropTypes.string,
        logoUrl: PropTypes.string
    })
};

ProductFeaturedQuotes.propTypes = {
    data: PropTypes.shape({
        featured: PropTypes.arrayOf(PropTypes.shape(productQuoteShape)),
        layout: PropTypes.oneOf([LAYOUT_VERTICAL, LAYOUT_HORIZONTAL]),
        logoSize: PropTypes.number,
        logoSpacing: PropTypes.number,
        i18n: PropTypes.shape({
            labels: PropTypes.shape({
                readMore: PropTypes.string
            })
        })
    })
};

ProductFeaturedQuotes.defaultProps = {
    data: {
        featured: null,
        layout: LAYOUT_VERTICAL,
        logoSize: null,
        logoSpacing: null,
        i18n: {
            labels: {
                readMore: 'Read More'
            }
        }
    }
};

export default ProductFeaturedQuotes;
