import React from 'react';
import PropTypes from 'prop-types';
import withListener from '../withListener';
import { replacePlaceholders } from '../../tools/stringTools';
import { createNamespace } from '../../tools/namingTools';
import { getButtons } from '../../tools/components/orderConfirmation';
import { TEXTAREA, GROUP_HEADER, INPUT, IMAGE_UPLOADER } from '../../constants/schemaComponentTypes';
import ButtonIcon from '../../atoms/button-icon/ButtonIcon';
import Icon from '../../atoms/icon/Icon';
import Image from '../../atoms/image/Image';

import './styles/OrderConfirmation.scss';

const n = createNamespace('OrderConfirmation');

export const OrderConfirmationComponent = ({
    data: {
        links,
        user,
        order,
        purchaseLink,
        i18n
    },
    settings
}) => {
    const orderInfo = {
        ...user,
        orderId: order?.id,
        orderUuid: order?.uuid
    };

    const { title: settingTitle, message: settingMessage, icon: settingIcon } = settings;
    const title = settingTitle ? replacePlaceholders(settingTitle, orderInfo) : i18n.customerName;

    const navigateToPurchase = e => {
        e.preventDefault();
        window.location.assign(purchaseLink.url);
    };

    const getPurchaseLinkMessage = () => {
        const purchaseNumber = (
            // eslint-disable-next-line jsx-a11y/click-events-have-key-events
            <span {...n('purchaseLink').withTestId().props} onClick={e => navigateToPurchase(e)}>
                #
                {orderInfo.orderId}
            </span>
        );

        return (
            <span>
                {i18n.customerPurchase}
                {' '}
                {purchaseNumber}
                {' '}
                {i18n.customerPurchaseProcessing}
                {' '}
                {i18n.customerEmail}
            </span>
        );
    };

    const defaultMessage = orderInfo.orderUuid ? getPurchaseLinkMessage() : `${i18n.confirmationOrderId} ${i18n.customerEmail}`;

    const message = settingMessage ? replacePlaceholders(settingMessage, orderInfo) : defaultMessage;

    const buttons = getButtons(links, settings);

    const renderHeaderIcon = () => {
        const iconProps = { ...n('icon').props };

        return settingIcon && settingIcon.url ? (
            <Image {...iconProps} src={settingIcon.url} />
        ) : (
            <Icon {...iconProps} type="check" />
        );
    };

    const renderButton = ({ key, isLink, url, icon, title: buttonTitle }) => (
        <ButtonIcon
            {...n('cta', isLink && 'link').props}
            key={key}
            type={isLink ? 'link' : 'button'}
            theme={isLink ? 'primary' : 'card'}
            size="large"
            url={url}
            iconSrc={icon && icon.url}
            wide
        >
            {buttonTitle}
        </ButtonIcon>
    );

    return (
        <section>
            <div {...n('container').withTestId().props}>
                <div {...n('heading').props}>
                    {renderHeaderIcon()}
                    {title && <h1 {...n('title').withTestId().props}>{title}</h1>}
                    {message && <p {...n('message').withTestId().props}>{message}</p>}
                </div>
                {buttons && buttons.length ? (
                    <div {...n('btnContainer').withTestId().props}>
                        {buttons.map(renderButton)}
                    </div>
                ) : null}
            </div>
        </section>
    );
};

OrderConfirmationComponent.propTypes = {
    data: PropTypes.shape({
        links: PropTypes.shape({
            viewApps: PropTypes.shape({
                title: PropTypes.string,
                url: PropTypes.string
            }),
            continueShopping: PropTypes.shape({
                title: PropTypes.string,
                url: PropTypes.string
            }),
            assignProducts: PropTypes.shape({
                title: PropTypes.string,
                url: PropTypes.string,
                isVisible: PropTypes.bool
            })
        }),
        user: PropTypes.shape({
            customerName: PropTypes.string,
            customerEmail: PropTypes.string
        }),
        order: PropTypes.shape({
            id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
            uuid: PropTypes.string
        }),
        purchaseLink: PropTypes.shape({
            url: PropTypes.string
        }),
        i18n: PropTypes.shape({
            customerName: PropTypes.string,
            confirmationOrderId: PropTypes.string,
            customerPurchase: PropTypes.string,
            customerPurchaseProcessing: PropTypes.string,
            customerEmail: PropTypes.string
        })
    }),
    settings: PropTypes.shape({
        icon: PropTypes.shape({
            filename: PropTypes.string,
            type: PropTypes.oneOf([IMAGE_UPLOADER]),
            url: PropTypes.string
        }),
        title: PropTypes.string,
        message: PropTypes.string
    })
};
OrderConfirmationComponent.schema = () => ({
    name: 'OrderConfirmation',
    title: 'uieditor.sfbComponent.orderConfirmation.title',
    iconName: '',
    form: {
        icon: {
            type: IMAGE_UPLOADER,
            title: 'uieditor.sfbComponent.orderConfirmation.icon.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.icon.placeholder',
            defaultValue: {
                filename: 'icon',
                type: 'IMAGE_UPLOADER',
                url: ''
            }
        },
        title: {
            type: TEXTAREA,
            title: 'uieditor.sfbComponent.orderConfirmation.title.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.title.placeholder',
            defaultValue: 'Thanks for your purchase, {customerName}.'
        },
        message: {
            type: TEXTAREA,
            title: 'uieditor.sfbComponent.orderConfirmation.message.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.message.placeholder',
            defaultValue: 'Your purchase {orderId} is processing. You will receive an email with a confirmation shortly at {customerEmail}.'
        },
        header: {
            type: GROUP_HEADER,
            title: 'uieditor.sfbComponent.orderConfirmation.viewApps.title',
            symbol: 'style'
        },
        viewApps: {
            type: INPUT,
            title: 'uieditor.sfbComponent.orderConfirmation.viewAppsLabel.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.viewAppsLabel.placeholder',
            defaultValue: 'View my apps'
        },
        viewAppsIcon: {
            type: IMAGE_UPLOADER,
            title: 'uieditor.sfbComponent.orderConfirmation.viewAppsIcon.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.viewAppsIcon.placeholder',
            defaultValue: {
                filename: 'icon',
                type: 'IMAGE_UPLOADER',
                url: ''
            }
        },
        header2: {
            type: GROUP_HEADER,
            title: 'uieditor.sfbComponent.orderConfirmation.continueShopping.title',
            symbol: 'style'
        },
        continueShopping: {
            type: INPUT,
            title: 'uieditor.sfbComponent.orderConfirmation.continueShoppingLabel.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.continueShoppingLabel.placeholder',
            defaultValue: 'Continue Shopping'
        },
        continueShoppingIcon: {
            type: IMAGE_UPLOADER,
            title: 'uieditor.sfbComponent.orderConfirmation.continueShoppingIcon.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.continueShoppingIcon.placeholder',
            defaultValue: {
                filename: 'icon',
                type: 'IMAGE_UPLOADER',
                url: ''
            }
        },
        header3: {
            type: GROUP_HEADER,
            title: 'uieditor.sfbComponent.orderConfirmation.assignProducts.title',
            symbol: 'style'
        },
        assignProducts: {
            type: INPUT,
            title: 'uieditor.sfbComponent.orderConfirmation.assignProductsLabel.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.assignProductsLabel.placeholder',
            defaultValue: 'Assign products'
        },
        assignProductsIcon: {
            type: IMAGE_UPLOADER,
            title: 'uieditor.sfbComponent.orderConfirmation.assignProductsIcon.title',
            placeholder: 'uieditor.sfbComponent.orderConfirmation.assignProductsIcon.placeholder',
            defaultValue: {
                filename: 'icon',
                type: 'IMAGE_UPLOADER',
                url: ''
            }
        }
    },
    marketplaceData: {
        links: 'orderConfirmation.links',
        i18n: 'orderConfirmation.i18n',
        order: 'orderConfirmation.order',
        purchaseLink: 'orderConfirmation.purchaseLink',
        user: 'orderConfirmation.user'
    }
});

OrderConfirmationComponent.defaultProps = {
    data: {
        links: null,
        user: null,
        order: null,
        i18n: {
            customerName: 'Thanks for your purchase, {customerName}.',
            confirmationOrderId: 'Your purchase {orderId} is processing.',
            customerEmail: 'You will receive an email with a confirmation shortly at {customerEmail}.'
        },
        purchaseLink: null
    },
    settings: {}
};

export default withListener(OrderConfirmationComponent);
