import React from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../../../tools/namingTools';
import { PRODUCT_DEFAULT_IMG } from '../../../../constants/images';
import { replacePlaceholders } from '../../../../tools/stringTools';
import { i18nShape as compareProductI18nShape } from '../../../../constants/components/prop-types/comparable-products';
import Rating from '../../../../atoms/rating/Rating';
import Pricing from '../../../../atoms/pricing/Pricing';
import Button from '../../../../atoms/button/Button';
import Icon from '../../../../atoms/icon/Icon';
import Image from '../../../../atoms/image/Image';
import { isContentFromOneWorldSync } from '../../../../tools/contentTools';
import { getV2PricingSymbolProps } from '../../../../tools/components/pricing';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/CompareProductDescription.scss';

const n = createNamespace('CompareProductDescription');

export const CompareProductDescription = ({
    iconUrl,
    name,
    description,
    rating,
    numberOfReview,
    startingPrice,
    settings,
    i18n,
    i18nRating,
    locale,
    url,
    referUrl
}) => {
    const imageUrl = iconUrl || PRODUCT_DEFAULT_IMG;
    const shouldRenderRating = (rating && numberOfReview > 0);
    const v2PricingProps = getV2PricingSymbolProps(startingPrice, locale);
    const pricingFrequency = v2PricingProps.isConsistentDisplay && startingPrice?.frequency?.id === 'ONE_TIME'
        ? null
        : startingPrice?.frequency?.label;

    return (
        <div {...n('wrapper').withTestId().props}>
            {name ? (
                <div {...n('container').withTestId().props}>
                    <div {...n('summary').props}>
                        {imageUrl && (
                            <div {...n('imageWrapper').props}>
                                <Image
                                    {...n('image').withTestId().props}
                                    src={imageUrl}
                                    isCopyRighted={isContentFromOneWorldSync(referUrl)}
                                />
                            </div>
                        )}
                        <h2 {...n('title').withTestId().props}>{name}</h2>
                        {description && (
                            <p {...n('description').withTestId().props}>{description}</p>
                        )}
                        {shouldRenderRating && (
                            <div {...n('starsWrapper').props}>
                                <Rating
                                    rating={rating}
                                    numRatings={numberOfReview}
                                    i18nRating={i18nRating}
                                    displayRatingNumber
                                />
                            </div>
                        )}
                        {settings && settings.isStartingPriceVisible && startingPrice && startingPrice.price && (
                            <div {...n('pricing').withTestId().props}>
                                <Pricing
                                    price={startingPrice.price}
                                    frequency={pricingFrequency}
                                    caption={startingPrice.priceCaption}
                                    symbol={startingPrice.symbol}
                                    {...v2PricingProps}
                                    isEmphasis
                                />
                            </div>
                        )}
                    </div>
                    <div {...n('btnContainer').props}>
                        <Button
                            url={url}
                            type="emphasis"
                            aria-label={replacePlaceholders(i18n.buyProduct, { productName: name })}
                        >
                            {i18n.buyNow}
                        </Button>
                    </div>
                </div>
            ) : (
                <>
                    <div {...n('imageWrapper', 'add').props}>
                        <Icon type="star" fill="#c0c0c0" width="60px" />
                    </div>

                    <div {...n('btnContainer', 'add').props}>
                        <Button url={url} aria-label={i18n.addProductToComparison}>{i18n.add}</Button>
                    </div>
                </>
            )}
        </div>
    );
};

CompareProductDescription.propTypes = {
    id: PropTypes.number,
    name: PropTypes.string,
    description: PropTypes.string,
    iconUrl: PropTypes.string,
    iconAlt: PropTypes.string,
    rating: PropTypes.number,
    numberOfReview: PropTypes.number,
    startingPrice: PropTypes.shape({
        formattedPrice: PropTypes.string,
        price: PropTypes.string,
        priceCaption: PropTypes.string,
        symbol: PropTypes.string,
        currency: PropTypes.string,
        frequency: PropTypes.shape({
            label: PropTypes.string,
            shortLabel: PropTypes.string,
            id: PropTypes.string
        }),
        consistentPriceDisplayV2Enabled: PropTypes.bool
    }),
    i18n: PropTypes.shape(compareProductI18nShape).isRequired,
    i18nRating: PropTypes.shape({
        label: PropTypes.string
    }),
    locale: PropTypes.oneOfType([
        PropTypes.string,
        PropTypes.shape({
            code: PropTypes.string
        })
    ]),
    url: PropTypes.string,
    referUrl: PropTypes.string,
    settings: PropTypes.shape({
        isStartingPriceVisible: PropTypes.bool
    })
};

CompareProductDescription.defaultProps = {
    id: null,
    name: '',
    description: '',
    iconUrl: '',
    iconAlt: null,
    rating: null,
    numberOfReview: 0,
    startingPrice: {},
    settings: {},
    url: '',
    referUrl: '',
    i18nRating: null,
    locale: null
};
