import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { CSSTransition, SwitchTransition } from 'react-transition-group';
import ModalContents from './atoms/modal-contents/ModalContents';
import { createNamespace } from '../../tools/namingTools';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/Modal.scss';

const n = createNamespace('Modal');

// for global modals, i.e. not rendered as child of another element, we render in a portal
// and there could exist only one modal at a time
let portalDomNode;

const animTime = 200; // ms
const ModalComponent = ({
    currentKey,
    currentItem,
    onClose,
    isGlobal,
    left,
    top,
    ariaLabelledBy,
    ariaLabel,
    ariaModal,
    showBackdrop,
    backdropBlur
}) => {
    const wrapperProps = n('transition').withVariables({ animTime: `${animTime}ms` }).props;
    const canUseDom = typeof document !== 'undefined';

    if (isGlobal && !currentItem && !showBackdrop) {
        return null;
    }

    if (isGlobal) {
        if (!canUseDom) {
            return null;
        }
        if (!portalDomNode) {
            portalDomNode = document.createElement('div');
            portalDomNode.classList.add(n('portal').props.className);
            document.body.append(portalDomNode);
        }
    }
    const content = (
        <SwitchTransition>
            <CSSTransition
                key={currentKey}
                {...wrapperProps}
                appear
                in
                timeout={currentItem ? animTime : 0}
                classNames={wrapperProps.className}
            >
                <div>
                    {currentItem && (
                        <ModalContents
                            onClose={onClose}
                            left={left}
                            top={top}
                            animTime={animTime}
                            ariaLabelledBy={ariaLabelledBy}
                            ariaLabel={ariaLabel}
                            ariaModal={ariaModal}
                        >
                            {currentItem}
                        </ModalContents>
                    )}
                </div>
            </CSSTransition>
        </SwitchTransition>
    );

    // on desktop, element is placed normally in the flow
    if (!isGlobal) {
        return content;
    }

    const backdrop = showBackdrop && currentItem ? (
        <div
            {...n('backdrop', backdropBlur && 'blur').props}
            onClick={onClose}
            aria-hidden="true"
        />
    ) : null;

    return ReactDOM.createPortal(
        <>
            {backdrop}
            <div {...n('layer').props}>
                {content}
            </div>
        </>,
        portalDomNode
    );
};

ModalComponent.propTypes = {
    currentKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
    currentItem: PropTypes.node,
    isGlobal: PropTypes.bool,
    onClose: PropTypes.func,
    left: PropTypes.number,
    top: PropTypes.number,
    ariaLabelledBy: PropTypes.string,
    ariaLabel: PropTypes.string,
    ariaModal: PropTypes.bool,
    showBackdrop: PropTypes.bool,
    backdropBlur: PropTypes.bool
};

ModalComponent.defaultProps = {
    onClose: () => null,
    currentItem: null,
    isGlobal: true,
    left: 0,
    top: 0,
    ariaLabelledBy: undefined,
    ariaLabel: undefined,
    ariaModal: undefined,
    showBackdrop: false,
    backdropBlur: false
};

export default ModalComponent;
