import React from 'react'
import cx from 'classnames'
import PropTypes from 'prop-types'
import { isNil } from 'ramda'

function MTModal (props) {
  const {
    show,
    className,
    close,
    modalTitle,
    ModalTitleComponent,
    children,
    width,
    height,
    headerHeight,
    ...otherProps
  } = props

  function stop (e) {
    e.stopPropagation()
  }

  const overlayClasses = cx({
    'is-visible': show,
    'mt-modal__overlay': true,
  })

  const modalClasses = `mt-modal ${className}`
  const modalStyle = { width }
  const modalContentStyle = { maxHeight: height }

  return (
    <div
      { ...otherProps }
      className={ overlayClasses }
      onClick={ close }
    >
      <div
        className={ modalClasses }
        onClick={ (e) => stop(e) }
        style={ modalStyle }
      >
        <div className="mt-modal__header" style={ { height: headerHeight } }>
          { isNil(ModalTitleComponent)
            ? (
              <h1 className="h1">{ modalTitle }</h1>
            )
            : (
              <ModalTitleComponent />
            )
          }
          <span
            className="icon-close"
            onClick={ close }
          />
        </div>
        <div
          className="mt-modal__content"
          style={ modalContentStyle }
        >
          { children }
        </div>
      </div>
    </div>
  )
}

MTModal.defaultProps = {
  className: '',
  width: '670px',
  height: '505px',
  headerHeight: '43px',
  ModalTitleComponent: null,
  modalTitle: '',
  children: null,
}

MTModal.propTypes = {
  className: PropTypes.string,
  modalTitle: PropTypes.string,
  children: PropTypes.node,
  ModalTitleComponent: PropTypes.func,
  width: PropTypes.string,
  height: PropTypes.string,
  headerHeight: PropTypes.string,
}

export default MTModal
