import React from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../../../../tools/namingTools';
import { useResponsive } from '../../../../../tools/widths/useResponsive';
import { RIGHT } from '../../../../../constants/alignment';
import { hasSocialNetworks } 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 { NO_LOGO } from '../../../../../constants/logo';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/FooterClassic.scss';

const n = createNamespace('FooterClassic');

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

    const {
        marketplaceName,
        i18n,
        logo
    } = data;

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

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

    const showFooterMenu = Boolean(footerMenu?.length);

    const showSocials = displaySocialMedia && hasSocialNetworks(socials);

    const renderSocialBlock = () => {
        const props = {
            ...socials,
            ...{
                i18n,
                showTitle: false,
                showCustomIcon: !displayDefaultSocialIcons,
                iconAlign: xxLargeUp ? RIGHT.value : null
            }
        };

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

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

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

    const renderLogoBlock = () => selectedLogo !== NO_LOGO && (
        <div {...n('col', 'logo').props}>
            <FooterLogo
                defaultLogo={logo}
                selectedLogo={selectedLogo}
                url="/"
                alt={getLogoAlt(selectedLogo) || marketplaceName || ''}
                width={logoSize}
            />
        </div>
    );

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

    return (
        <div {...n('wrapper').withTestId().props}>
            {(showFooterMenu || showSocials) && (
                <div {...n('section', 'top').props}>
                    <div {...n('row').props}>
                        {renderLogoBlock()}
                        {showFooterMenu && (
                            <div {...n('col', 'nav').props}>
                                {showFooterMenu && renderBottomBarMenu()}
                            </div>
                        )}
                        {showSocials && (
                            <div {...n('col', ['socials', socials.items.length > 4 && 'large']).props}>
                                {showSocials && renderSocialBlock()}
                            </div>
                        )}
                    </div>
                    {copyright && renderCopyrightBlock()}
                </div>

            )}
        </div>
    );
};

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

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

export default FooterClassic;
