import React from 'react';
import PropTypes from 'prop-types';
import withListener from '../withListener';
import { generateUniqueID } from '../../tools/helpers';
import { createNamespace } from '../../tools/namingTools';
import { DROPDOWN } from '../../constants/schemaComponentTypes';
import { NAMESPACES } from '../../constants/namespaces';
import { profilePageDataStore } from '../../store/profilePageData';
import Button from '../../atoms/button/Button';
import Grid from '../../atoms/grid/Grid';
import Icon from '../../atoms/icon/Icon';
import FeatureCard from '../../atoms/feature-card/FeatureCard';
import './styles/TopFeatures.scss';

const n = createNamespace('TopFeatures');

const onButtonHandler = (e, url) => {
    e.preventDefault();

    if (url) {
        window.location = url;
    }
};

const renderShowMore = button => {
    const buttonProps = {
        type: button.color,
        extraClass: 'btn-show-more',
        onClick: e => onButtonHandler(e, button.href)
    };

    return (
        <React.Fragment>
            <div {...n('button', 'desktop').withE2EId('buttonDesktop').props}>
                <Button {...buttonProps}>
                    {button.alternateLabel}
                </Button>
            </div>
            <div {...n('button', 'mobile').props}>
                <Button {...buttonProps} buttonType="link">
                    {button.alternateLabel}
                    <Icon type="caretDown" {...n('buttonIcon').props} />
                </Button>
            </div>
        </React.Fragment>
    );
};

// @todo: change this when the dropdown allows object values
export const columnOptions = {
    one: 1,
    two: 2,
    three: 3
};

export const TopFeaturesComponent = ({ data, settings }) => {
    const settingsWithDefaults = { ...TopFeaturesComponent.defaultProps.settings, ...settings };
    const { overview } = data;
    const { benefits, callToActions, i18n } = overview;
    const { isFeaturesLinkEnabled } = profilePageDataStore();

    if (!benefits || !benefits.length) {
        return null;
    }

    const { featuresAndBenefits } = i18n;
    const { numberOfColumns } = settingsWithDefaults;
    const maxCol = columnOptions[numberOfColumns.toLowerCase()] || 2;

    const renderItem = ({ title, description, descriptionLong }, key) => (
        <div {...n('feature').withTestId().withE2EId().props} key={generateUniqueID([title, key])}>
            <FeatureCard title={title} description={descriptionLong || description} withHeading />
        </div>
    );

    return (
        <section {...n('container').withE2EId().props}>
            <h1 {...n('title').withTestId().withE2EId().props}>{featuresAndBenefits}</h1>
            <div {...n('gridWrapper').withTestId().props}>
                <Grid maxCol={maxCol} itemClass={n('item').props.className}>
                    {benefits.map(renderItem)}
                </Grid>
            </div>
            {isFeaturesLinkEnabled && callToActions && (
                <div {...n('buttonWrapper').props}>
                    {callToActions.tour && renderShowMore(callToActions.tour)}
                </div>
            )}
        </section>
    );
};

const NUMBER_COLUMNS_OPTIONS = [
    { value: 'one', label: 'uieditor.sfbComponent.topFeatures.numberOfColumns.option.one' },
    { value: 'two', label: 'uieditor.sfbComponent.topFeatures.numberOfColumns.option.two' },
    { value: 'three', label: 'uieditor.sfbComponent.topFeatures.numberOfColumns.option.three' }
];

TopFeaturesComponent.schema = () => ({
    name: 'ProductTopFeatures',
    title: 'uieditor.sfbComponent.topFeatures.title',
    anchor: 'product-section-features',
    iconName: 'special',
    orderableSection: [NAMESPACES.profile],
    form: {
        numberOfColumns: {
            title: 'uieditor.sfbComponent.topFeatures.numberOfColumns.title',
            type: DROPDOWN,
            required: false,
            defaultValue: NUMBER_COLUMNS_OPTIONS[1].value,
            options: NUMBER_COLUMNS_OPTIONS,
            validation: {
                availableOptions: NUMBER_COLUMNS_OPTIONS.map(({ value }) => value),
                message: 'uieditor.sfbComponent.dropdown.validation.message',
                default: 'uieditor.sfbComponent.dropdown.defaultValue'
            }
        }
    },
    marketplaceData: {
        overview: 'application.overview'
    },
    requiredDataFunc: ({ overview: { benefits = [] } }) => (benefits && benefits.length)
});

export const topFeatureOverviewShape = {
    benefits: PropTypes.arrayOf(
        PropTypes.shape({
            title: PropTypes.string,
            description: PropTypes.string,
            descriptionLong: PropTypes.string
        })
    ),
    callToActions: PropTypes.shape({
        tour: PropTypes.shape({
            label: PropTypes.string,
            alternateLabel: PropTypes.string,
            color: PropTypes.string,
            href: PropTypes.string
        })
    }),
    i18n: PropTypes.shape({
        featuresAndBenefits: PropTypes.string,
        seeAllFeatures: PropTypes.string
    })
};

TopFeaturesComponent.propTypes = {
    settings: PropTypes.shape({
        numberOfColumns: PropTypes.string
    }),
    data: PropTypes.shape({
        overview: PropTypes.shape(topFeatureOverviewShape)
    })
};

const SETTINGS = TopFeaturesComponent.schema().form;

TopFeaturesComponent.defaultProps = {
    settings: {
        numberOfColumns: SETTINGS.numberOfColumns.defaultValue
    },
    data: {
        overview: {
            benefits: [],
            callToActions: null,
            i18n: {
                featuresAndBenefits: 'Top Features',
                seeAllFeatures: 'See All Features'
            }
        }
    }
};

export default withListener(TopFeaturesComponent);
