import React from 'react';
import PropTypes from 'prop-types';
import './styles/CarouselSlideText.scss';
import { createNamespace } from '../../tools/namingTools';
import Button from '../button/Button';
import { COLORPICKER, DROPDOWN, INPUT, TEXTAREA } from '../../constants/schemaComponentTypes';
import { SMALL, MEDIUM, LARGE } from '../../constants/sizes';
import { LEFT, RIGHT, CENTER } from '../../constants/alignment';
import { newlineToBr } from '../../tools/stringTools';
import { navigateTo } from '../../tools/urlTools';

const n = createNamespace('CarouselSlideText');

export const TEXT_SLIDE_TYPE = 'IMAGE_WITH_TEXT';
export const PRODUCT_TYPE = 'PRODUCT';

const CarouselSlideText = ({
    data: {
        title,
        text,
        image,
        textColor,
        textSize,
        textAlignment,
        backgroundColor,
        imagePosition,
        slideLink,
        buttonLabel
    },
    i18n: {
        learnMore
    },
    settings
}) => {
    const imageUrl = image ? image.url : '';
    const componentVariables = {
        height: settings.height ? `${settings.height}px` : null,
        'mobile-height': settings.mobileHeight ? `${settings.mobileHeight}px` : null,
        'text-color': textColor,
        'background-color': backgroundColor,
        'text-alignment': textAlignment
    };

    const wrapperModifiers = [
        imagePosition && `image-${imagePosition}`,
        textSize && `text-size-${textSize}`,
        backgroundColor && 'has-background'
    ];
    const handleButtonClick = () => navigateTo(slideLink);

    const ConditionalAnchorWrapper = ({ isClickable, anchorWrapper, children }) => (
        isClickable ? anchorWrapper(children) : children
    );

    // if slide is type image, the whole slide becomes the link instead of having a button
    let callToAction = null;
    if (slideLink) {
        callToAction = (
            <div {...n('button').props}>
                <Button type="emphasisAlt" onClick={handleButtonClick}>
                    {buttonLabel || learnMore}
                </Button>
            </div>
        );
    }

    const shouldRenderContentText = title || text;

    return (
        <div {...n('wrapper', wrapperModifiers).withTestId().withVariables(componentVariables).props}>
            <div {...n('content').props}>
                {shouldRenderContentText && (
                    <div {...n('text').props}>
                        {title && (
                            <div {...n('title').withTestId().props}>
                                {newlineToBr(title)}
                            </div>
                        )}
                        {text && (
                            <div {...n('description').withTestId().props}>
                                {newlineToBr(text)}
                            </div>
                        )}
                        {callToAction}
                    </div>
                )}
                {imageUrl ? (
                    <div {...n('image').props}>
                        <ConditionalAnchorWrapper
                            anchorWrapper={children => (
                                <a {...n().withTestId('contentImage').props} href={slideLink}>
                                    {children}
                                </a>
                            )}
                        >
                            <img src={imageUrl} alt={title} />
                        </ConditionalAnchorWrapper>
                    </div>
                ) : ''}
            </div>
        </div>
    );
};

const TEXT_SIZES_OPTIONS = [SMALL, MEDIUM, LARGE];
const TEXT_ALIGN_OPTIONS = [LEFT, RIGHT, CENTER];
const IMAGE_ALIGN_OPTIONS = [LEFT, RIGHT];

CarouselSlideText.schema = () => ({
    name: 'Carousel Slide Text',
    iconName: 'carousel',
    form: {
        title: {
            title: 'uieditor.sfbComponent.carouselSlideText.title.title',
            type: INPUT,
            defaultValue: 'Input slide title here'
        },
        text: {
            title: 'uieditor.sfbComponent.carouselSlideText.blurb.title',
            type: TEXTAREA,
            defaultValue: 'Input slide text here'
        },
        textSize: {
            title: 'uieditor.sfbComponent.carouselSlideText.textSize.title',
            type: DROPDOWN,
            defaultValue: TEXT_SIZES_OPTIONS[0].value,
            options: TEXT_SIZES_OPTIONS,
            validation: {
                availableOptions: TEXT_SIZES_OPTIONS.map(({ value }) => value),
                message: 'uieditor.sfbComponent.dropdown.validation.message',
                default: 'uieditor.sfbComponent.dropdown.defaultValue'
            }
        },
        textColor: {
            title: 'uieditor.sfbComponent.carouselSlideText.textColor.title',
            defaultValue: '#474747',
            type: COLORPICKER
        },
        textAlignment: {
            title: 'uieditor.sfbComponent.carouselSlideText.textAlignment.title',
            type: DROPDOWN,
            defaultValue: TEXT_ALIGN_OPTIONS[0].value,
            options: TEXT_ALIGN_OPTIONS,
            validation: {
                availableOptions: TEXT_ALIGN_OPTIONS.map(({ value }) => value),
                message: 'uieditor.sfbComponent.dropdown.validation.message',
                default: 'uieditor.sfbComponent.dropdown.defaultValue'
            }
        },
        imageUrl: {
            title: 'uieditor.sfbComponent.carouselSlideText.imageUrl.title',
            type: INPUT,
            defaultValue: ''
        },
        imagePosition: {
            title: 'uieditor.sfbComponent.carouselSlideText.imagePosition.title',
            type: DROPDOWN,
            defaultValue: IMAGE_ALIGN_OPTIONS[0].value,
            options: IMAGE_ALIGN_OPTIONS,
            validation: {
                availableOptions: IMAGE_ALIGN_OPTIONS.map(({ value }) => value),
                message: 'uieditor.sfbComponent.dropdown.validation.message',
                default: 'uieditor.sfbComponent.dropdown.defaultValue'
            }
        },
        slideLink: {
            title: 'uieditor.sfbComponent.carouselSlideText.url.title',
            type: INPUT,
            defaultValue: ''
        },
        buttonLabel: {
            title: 'uieditor.sfbComponent.carouselSlideText.buttonLabel.title',
            type: INPUT,
            defaultValue: 'Input link label here'
        },
        backgroundColor: {
            title: 'uieditor.sfbComponent.carouselSlideText.backgroundColor.title',
            defaultValue: '#eee',
            type: COLORPICKER
        }
    }
});

CarouselSlideText.propTypes = {
    settings: PropTypes.shape({
        height: PropTypes.number,
        mobileHeight: PropTypes.number
    }),
    data: PropTypes.shape({
        layout: PropTypes.string,
        title: PropTypes.string,
        text: PropTypes.string,
        image: PropTypes.shape({
            url: PropTypes.string
        }),
        textColor: PropTypes.string,
        textSize: PropTypes.string,
        textAlignment: PropTypes.string,
        backgroundColor: PropTypes.string,
        imagePosition: PropTypes.string,
        slideLink: PropTypes.string,
        buttonLabel: PropTypes.string
    }),
    i18n: PropTypes.shape({
        learnMore: PropTypes.string
    }),
    isPreviewMode: PropTypes.bool
};

CarouselSlideText.defaultProps = {
    settings: {
    },
    data: {
        title: '',
        text: '',
        imageUrl: null,
        textColor: null,
        textSize: null,
        textAlignment: null,
        backgroundColor: null,
        imagePosition: null,
        slideLink: null,
        buttonLabel: null
    },
    i18n: {
        learnMore: 'Learn More'
    },
    isPreviewMode: false
};

export default CarouselSlideText;
