import React from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../../../../tools/namingTools';
import { useResponsive } from '../../../../../tools/widths/useResponsive';
import { LEFT, RIGHT } from '../../../../../constants/alignment';
import { hasMenu, hasSocialNetworks, hasLogo, hasNavigationBar } from '../../../../../tools/footerHelpers';
import { footerDataPropTypes, footerSettingsPropTypes } from '../../../../../constants/components/prop-types/footer';
import FooterLogo from '../../common/footer-logo/FooterLogo';
import FooterNavigation from '../../common/footer-navigation/FooterNavigation';
import FooterSocials from '../../common/footer-socials/FooterSocials';
import FooterNewsletter from '../../common/footer-newsletter/FooterNewsletter';
import FooterCopyright from '../../common/footer-copyright/FooterCopyright';
import FooterMenu from '../../common/footer-menu/FooterMenu';

import './styles/FooterNewsletterSocial.scss';

const n = createNamespace('FooterNewsletterSocial');

const FooterNewsletterSocial = ({
    data: {
        logo,
        locale,
        marketplaceName,
        i18n
    },
    settings
}) => {
    const { xxLargeUp } = useResponsive();

    const {
        menu,
        socials,
        copyright,
        navigation,
        displayLogo,
        logoPosition,
        displayLanguageSwitcher,
        displayNavSeparator,
        displaySocialMedia,
        displayDefaultSocialIcons,
        newsletterTitle: settingNewsletterTitle,
        newsletterText: settingNewsletterText,
        newsletterButtonLabel: settingNewsletterButtonLabel,
        newsletterButtonLink: settingNewsletterButtonLink,
        newsletterButtonStyle: settingNewsletterButtonStyle,
        newsletterSocialPosition: settingNewsletterSocialPosition
    } = settings;

    const showMenu = hasMenu(menu);
    const showNavigation = hasNavigationBar(navigation, locale);
    const showSocials = displaySocialMedia && hasSocialNetworks(socials);
    const showLogo = displayLogo && hasLogo(logo);

    const renderNewsletterBlock = () => {
        const { newsletterTitle, newsletterDescription, newsletterButton } = i18n;

        const props = {
            url: settingNewsletterButtonLink,
            buttonTheme: settingNewsletterButtonStyle,
            i18n: {
                newsletterTitle: settingNewsletterTitle || newsletterTitle,
                newsletterDescription: settingNewsletterText || newsletterDescription,
                newsletterButton: settingNewsletterButtonLabel || newsletterButton
            }
        };

        return (
            <div {...n('newsletter').props}>
                <FooterNewsletter {...props} />
            </div>
        );
    };

    const renderSocialBlock = () => {
        const props = {
            ...socials,
            ...{
                i18n,
                showCustomIcon: !displayDefaultSocialIcons
            }
        };

        return (
            <div {...n('socials').props}>
                <FooterSocials {...props} />
            </div>
        );
    };

    const renderCopyrightBlock = () => (
        <div {...n('copyright').props}>
            <FooterCopyright text={copyright} />
        </div>
    );

    const renderMenuBlock = () => {
        const props = {
            ...menu,
            ...{
                navAlign: xxLargeUp && settingNewsletterSocialPosition === LEFT.value ? RIGHT.value : null
            }
        };

        return (
            <div {...n('menu').props}>
                <FooterMenu {...props} />
            </div>
        );
    };

    const renderNavigationBlock = () => {
        const props = {
            i18n,
            locale,
            navigation,
            withSeparator: displayNavSeparator,
            withLanguageSwitcher: displayLanguageSwitcher
        };

        return (
            <div {...n('nav').props}>
                <FooterNavigation {...props} />
            </div>
        );
    };

    return (
        <div {...n('wrapper').props}>
            <div {...n('section', 'top').props}>
                <div {...n('row').props}>
                    {showMenu && (
                        <div {...n('col', 'menu').props}>
                            {renderMenuBlock()}
                        </div>
                    )}
                    <div {...n('col', ['socials', settingNewsletterSocialPosition && `align-${settingNewsletterSocialPosition}`]).props}>
                        <div {...n('newsletterSocials').props}>
                            {renderNewsletterBlock()}
                            {showSocials && renderSocialBlock()}
                        </div>
                    </div>
                </div>
            </div>
            <div {...n('section', 'bottom').props}>
                <div {...n('row', 'bottom').props}>
                    {showNavigation && (
                        <div {...n('col', 'nav').props}>
                            {renderNavigationBlock()}
                        </div>
                    )}
                    {copyright && (
                        <div {...n('col', 'copyright').props}>
                            {renderCopyrightBlock()}
                        </div>
                    )}
                    {showLogo && (
                        <div {...n('col', ['logo', logoPosition && `align-${logoPosition}`]).props}>
                            <div {...n('logo').props}>
                                <FooterLogo src={logo} url="/" alt={marketplaceName} />
                            </div>
                        </div>
                    )}
                </div>
            </div>
        </div>
    );
};

FooterNewsletterSocial.propTypes = {
    data: PropTypes.shape(footerDataPropTypes),
    settings: PropTypes.shape(footerSettingsPropTypes)
};

FooterNewsletterSocial.defaultProps = {
    data: {},
    settings: {}
};

export default FooterNewsletterSocial;
