import React from 'react';
import PropTypes from 'prop-types';
import { summaryPropTypes } from '../../constants/prop-types/application/summary';
import { ratingsSummaryDefaults } from '../../constants/default-props/ratings';
import { PRODUCT_DEFAULT_IMG } from '../../constants/images';
import { XSMALL, SMALL, MEDIUM, LARGE } from '../../constants/sizes';
import { PRODUCT_CARD_MAX_MERCHANDISING_BADGES } from '../../constants/components/product-card';
import { ratingsSummaryShape } from '../../constants/prop-types/common';
import { lowerFirst } from '../../tools/stringTools';
import { createNamespace } from '../../tools/namingTools';
import { generateUniqueID } from '../../tools/helpers';
import { renderBadge } from '../../tools/rendering/merchandisingBadges';
import Icon from '../icon/Icon';
import Image from '../image/Image';
import Rating from '../rating/Rating';
import Badge from '../badge/Badge';
import { isContentFromOneWorldSync } from '../../tools/contentTools';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/ProductCard.scss';

const sizeValues = [XSMALL.value, SMALL.value, MEDIUM.value, LARGE.value];

const n = createNamespace('ProductCard');

const ProductCard = ({
    cardData: { title, details, size, logoSrcset = {}, logoAlt, merchandisingBadges = [],
        hasRequiredProduct, i18n, useCheckoutForSpecialPricingMessage },
    reviewData: { details: reviewDetails },
    settings,
    referUrl,
    i18nRating
}) => {
    const i18nSummaryWithDefaults = { ...ProductCard.defaultProps.cardData.i18n, ...i18n };
    const { productNameSize, descTextSize, showRatings } = settings;
    const {
        reviews: { rating, numRatings, caption: ratingCaption, title: ratingTitle } = ratingsSummaryDefaults,
        vendor: { title: vendorTitle, items }
    } = reviewDetails;

    const shouldRenderRating = Boolean(showRatings && rating && numRatings && numRatings > 0);

    const renderVendorLabel = ({ label: vendorLabel }) => (
        <span
            key={generateUniqueID([vendorLabel])}
            {...n('vendorLabel').withTestId().withE2EId().props}
        >
            {vendorLabel}
        </span>
    );

    const merchandisingBadgesBlock = merchandisingBadges && merchandisingBadges.length ? (
        <ul {...n('merchandisingBadges').withTestId().props} aria-label={i18nSummaryWithDefaults.merchandisingBadges}>
            {merchandisingBadges.slice(0, PRODUCT_CARD_MAX_MERCHANDISING_BADGES).map(badge => (
                <li {...n('badgeItem').props} key={badge.uuid}>
                    {renderBadge(badge)}
                </li>
            ))}
        </ul>
    ) : null;

    const shouldRenderVendor = !!vendorTitle;

    const get2xProfileLogoImage = (logoSrcset && logoSrcset['2x']) || (logoSrcset && logoSrcset.default) || ProductCard.defaultProps.cardData.logoSrc;

    const is3rdPartyContent = get2xProfileLogoImage !== PRODUCT_DEFAULT_IMG && isContentFromOneWorldSync(referUrl);

    const aspectRatio = is3rdPartyContent && {
        '--profileImg-aspect-ratio': '4/3'
    };

    return (
        <div {...n('container').withTestId().props}>
            <div {...n('imageWrapper', [size, is3rdPartyContent ? 'customAspectRatio' : null]).withVariables(aspectRatio).props}>
                <Image
                    {...n('image').withTestId().withE2EId().props}
                    alt={logoAlt}
                    src={get2xProfileLogoImage}
                />
            </div>
            <div {...n('content').props}>
                <div {...n('contentText').props}>
                    <h2 {...n('title', lowerFirst(productNameSize)).withTestId().withE2EId().props}>{title}</h2>
                    {details && (
                        <h3 {...n('caption', lowerFirst(descTextSize)).withTestId().withE2EId().props}>{details}</h3>
                    )}
                    {hasRequiredProduct && (
                        <div {...n('hasRequiredProduct').props}>
                            <Icon {...n('tooltipIcon').props} type="tooltip" />
                            <span {...n('text').props}>{i18nSummaryWithDefaults.hasRequiredProduct}</span>
                        </div>
                    )}
                </div>
                {merchandisingBadgesBlock}
                {useCheckoutForSpecialPricingMessage && (
                    <Badge
                        {...n('promoBadge').withTestId().props}
                        type="success"
                        label={i18n?.useCheckoutForSpecialPricingMessageProfile}
                    />
                )}
                {(shouldRenderRating || shouldRenderVendor) && (
                    <div {...n('ratingWrapper').props}>
                        {shouldRenderRating && (
                            <div {...n('starsWrapper').props}>
                                <Rating
                                    rating={rating}
                                    numRatings={numRatings}
                                    caption={ratingCaption}
                                    title={ratingTitle}
                                    displayRatingNumber
                                    i18nRating={i18nRating}
                                />
                            </div>
                        )}
                        {shouldRenderVendor && (
                            <div {...n('details').props}>
                                <span {...n('vendorTitle').withTestId().withE2EId().props}>{`${vendorTitle}:`}</span>
                                {items.map(renderVendorLabel)}
                            </div>
                        )}
                    </div>
                )}
            </div>
        </div>
    );
};

ProductCard.schema = () => ({
    name: 'Product Card',
    form: {}
});

ProductCard.propTypes = {
    cardData: PropTypes.shape(summaryPropTypes),
    reviewData: PropTypes.shape({
        details: PropTypes.shape({
            reviews: PropTypes.shape(ratingsSummaryShape),
            vendor: PropTypes.shape({
                title: PropTypes.string,
                items: PropTypes.arrayOf(PropTypes.shape({
                    label: PropTypes.string
                }))
            })
        })
    }),
    settings: PropTypes.shape({
        showRatings: PropTypes.bool,
        productNameSize: PropTypes.oneOf(sizeValues),
        descTextSize: PropTypes.oneOf(sizeValues)
    }),
    referUrl: PropTypes.string,
    i18nRating: PropTypes.shape({
        label: PropTypes.string
    })
};

ProductCard.defaultProps = {
    cardData: {
        title: '',
        details: '',
        logoSrc: PRODUCT_DEFAULT_IMG,
        logoSrcset: null,
        logoAlt: 'Product\'s logo',
        size: LARGE.value,
        hasRequiredProduct: false,
        i18n: {
            merchandisingBadges: 'Merchandising Badges',
            hasRequiredProduct: 'This product may require additional purchases',
            useCheckoutForSpecialPricingMessage: false
        }
    },
    reviewData: {
        details: {
            reviews: ratingsSummaryDefaults,
            vendor: {}
        }
    },
    settings: {
        showRatings: false,
        productNameSize: MEDIUM.value,
        descTextSize: MEDIUM.value
    },
    referUrl: '',
    i18nRating: null
};

export default ProductCard;
