import React from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../../../../tools/namingTools';
import { useResponsive } from '../../../../../tools/widths/useResponsive';
import { TOP_LEFT, BOTTOM_LEFT, RIGHT } from '../../../../../constants/alignment';
import { hasSocialNetworks, hasLanguageSwitch } from '../../../../../tools/footerHelpers';
import { getLogoAlt } from '../../../../../tools/themeSettings';
import { footerDataPropTypes, footerSettingsPropTypes } from '../../../../../constants/components/prop-types/footer';
import { menusPropTypes } from '../../../../../constants/components/prop-types/menu';
import FooterLogo from '../../../../../atoms/logo/Logo';
import BottomBarMenu from '../../common/bottom-bar-menu/BottomBarMenu';
import FooterSocials from '../../common/footer-socials/FooterSocials';
import FooterCopyright from '../../common/footer-copyright/FooterCopyright';
import FooterMenu from '../../common/footer-menu/FooterMenu';
import { NO_LOGO } from '../../../../../constants/logo';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/FooterSiteMap.scss';

const n = createNamespace('FooterBottomBar');

const FooterBottomBar = ({
    data = {},
    settings = {},
    menus = {}
}) => {
    const { xxLargeUp } = useResponsive();

    const {
        logo,
        locale,
        marketplaceName,
        i18n
    } = data;

    const {
        socials,
        copyright,
        logoSize,
        selectedLogo,
        logoPosition,
        displayNavSeparator,
        displaySocialMedia,
        socialIconPosition,
        displayDefaultSocialIcons
    } = settings;

    const footerMenu = menus?.FOOTER_MENU?.menu;
    const bottomBarMenu = menus?.BOTTOM_BAR_MENU?.menu;

    const showFooterMenu = Boolean(footerMenu?.length);
    const showBottomBarMenu = Boolean(bottomBarMenu?.length || hasLanguageSwitch(locale));
    const showSocials = displaySocialMedia && hasSocialNetworks(socials);
    const renderSocialBlock = () => {
        const props = {
            ...socials,
            ...{
                i18n,
                showTitle: false,
                showCustomIcon: !displayDefaultSocialIcons,
                iconAlign: xxLargeUp ? socialIconPosition : null
            }
        };

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

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

    const renderFooterMenu = () => {
        const isLogoLeftAligned = logoPosition === TOP_LEFT.value || logoPosition === BOTTOM_LEFT.value;
        const props = {
            i18n,
            menu: footerMenu,
            ...{
                navAlign: xxLargeUp && isLogoLeftAligned ? RIGHT.value : null
            }
        };
        return (
            <div {...n('menu').props}>
                <FooterMenu {...props} />
            </div>
        );
    };

    const renderBottomBarMenu = () => {
        const props = {
            ...data,
            menu: bottomBarMenu,
            withSeparator: displayNavSeparator
        };

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

    return (
        <div {...n('wrapper').props}>
            <div {...n('section', 'top').withE2EId().props}>
                <div {...n('row').props}>
                    {showFooterMenu && (
                        <div {...n('col', 'menu').props}>
                            {renderFooterMenu()}
                        </div>
                    )}
                    {selectedLogo !== NO_LOGO && (
                        <div {...n('col', ['logo', logoPosition && `align-${logoPosition}`]).withE2EId().props}>
                            <div {...n('logo').props}>
                                <FooterLogo
                                    defaultLogo={logo}
                                    selectedLogo={selectedLogo}
                                    url="/"
                                    alt={getLogoAlt(selectedLogo) || marketplaceName || ''}
                                    width={logoSize}
                                />
                            </div>
                        </div>
                    )}
                </div>
            </div>
            <div {...n('section', 'bottom').withE2EId().props}>
                <div {...n('row', 'bottom').props}>
                    {showBottomBarMenu && (
                        <div {...n('col', 'nav').props}>
                            {renderBottomBarMenu()}
                        </div>
                    )}
                    {copyright && (
                        <div {...n('col', 'copyright').props}>
                            {renderCopyrightBlock()}
                        </div>
                    )}
                    {showSocials && (
                        <div
                            {...n('col', [
                                'socials',
                                socialIconPosition && `align-${socialIconPosition}`,
                                socials.items.length > 4 && 'large'
                            ]).props}
                        >
                            {renderSocialBlock()}
                        </div>
                    )}
                </div>
            </div>
        </div>
    );
};

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

FooterBottomBar.defaultProps = {
    data: {},
    settings: {},
    menus: {}
};

export default FooterBottomBar;
