import React, { useRef } from 'react';
import PropTypes from 'prop-types';
import { CSSTransition } from 'react-transition-group';

const Modal = ({ className, innerClassName, openModal, toggleModal, children, outerClick = true, isCrossBtnDisabled = false }) => {
    const modalRef = useRef(null);
    const modalInnerRef = useRef(null);
  
    return (
        <CSSTransition
          in={openModal}
          timeout={300}
          classNames="arcm-modal"
          unmountOnExit
          nodeRef={modalRef}
        >
            <div ref={modalRef} className={`arcm-modal ${className} arcm-fixed arcm-top-0 arcm-left-0 arcm-h-full arcm-w-full arcm-z-[9999] arcm-bg-[#374151] arcm-bg-opacity-30 arcm-overflow-x-hidden arcm-overflow-y-auto`}>
                <div className="arcm-modal-container arcm-absolute arcm-top-0 arcm-left-0 arcm-h-full arcm-w-full">
                    <div className="arcm-modal-content arcm-flex">
                        {openModal && outerClick ? (<div className="arcm-fixed arcm-left-0 arcm-top-0 arcm-z-40 arcm-w-full arcm-h-full" onClick={toggleModal}></div>) : (
                            <div className="arcm-fixed arcm-left-0 arcm-top-0 arcm-z-40 arcm-w-full arcm-h-full"></div>
                        ) }

                        <CSSTransition
                            in={openModal}
                            timeout={300}
                            classNames="arcm-modal-inner"
                            unmountOnExit
                            nodeRef={modalInnerRef}
                        >
                            <div ref={modalInnerRef} className={`arcm-modal-inner arcm-m-auto arcm-relative arcm-z-50 arcm-flex arcm-flex-col arcm-max-w-[460px] arcm-w-full arcm-bg-white arcm-border arcm-rounded-[20px] arcm-border-[#E6E7EC] ${innerClassName || ''}`}
                                style={undefined}
                            >
                                {children.header ?  (
                                    <div className="arcm-modal-header arcm-relative arcm-items-center arcm-px-6 arcm-pt-5 arcm-pb-2 arcm-mb-auto">
                                        {children.header}
                                        <button disabled={isCrossBtnDisabled} className="arcm-close-modal !arcm-shadow-none arcm-absolute arcm-top-5 arcm-right-4 arcm-z-10 arcm-ml-auto !arcm-bg-transparent !arcm-border-none" onClick={toggleModal}>
                                            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
                                                <path fillRule="evenodd" clipRule="evenodd" d="M4.29289 4.29289C4.68342 3.90237 5.31658 3.90237 5.70711 4.29289L10 8.58579L14.2929 4.29289C14.6834 3.90237 15.3166 3.90237 15.7071 4.29289C16.0976 4.68342 16.0976 5.31658 15.7071 5.70711L11.4142 10L15.7071 14.2929C16.0976 14.6834 16.0976 15.3166 15.7071 15.7071C15.3166 16.0976 14.6834 16.0976 14.2929 15.7071L10 11.4142L5.70711 15.7071C5.31658 16.0976 4.68342 16.0976 4.29289 15.7071C3.90237 15.3166 3.90237 14.6834 4.29289 14.2929L8.58579 10L4.29289 5.70711C3.90237 5.31658 3.90237 4.68342 4.29289 4.29289Z" fill="#6B7280"/>
                                            </svg>
                                        </button>
                                    </div>
                                ): null}
                                <div className="arcm-modal-body arcm-px-6 arcm-py-3 arcm-mt-auto arcm-mb-auto">
                                    {children.body}
                                </div>
                                {children.footer ? (
                                    <div className="arcm-modal-footer arcm-px-6 arcm-pt-3 arcm-pb-6 arcm-mt-auto">
                                        {children.footer}
                                    </div>
                                ) : ''}
                            </div>
                        </CSSTransition>
                    </div>
                </div>
            </div>
        </CSSTransition>
    );
  };
  
  Modal.propTypes = {
    openModal: PropTypes.bool.isRequired,
    toggleModal: PropTypes.func.isRequired,
    className: PropTypes.string,
    innerClassName: PropTypes.string,
    children: PropTypes.shape({
      header: PropTypes.node,
      body: PropTypes.node,
      footer: PropTypes.node,
    }).isRequired,
  };

export default Modal;
