import React from 'react'
import cx from 'classnames'
import { isNil } from 'ramda'
import MTAcronym from './mt-acronym'

const Image = (props) => {
  const {
    image,
    round,
    baseStyles,
    className,
    ...otherProps
  } = props

  return (
    <div
      { ...otherProps }
      className={ cx('mt-image', round && 'mt-image--round', className) }
      style={ baseStyles }
    >
      <img className="mt-image__image" src={ image } alt="" />
    </div>
  )
}

const Icon = (props) => {
  const {
    icon,
    round,
    className,
    baseStyles,
    ...otherProps
  } = props

  return (
    <div
      { ...otherProps }
      className={ cx('mt-image', round && 'mt-image--round', className) }
      style={ baseStyles }
    >
      <span className="mt-image__image">
        <span className="mt-image__letter">
          <span className={ `icon-${icon}` } />
        </span>
      </span>
    </div>
  )
}

const MTImage = (props) => {
  const {
    width = 36,
    height = 36,
    round = false,
    backgroundColor = '#f6634b',
    showBorderIfImage = false,
    item,
    iconType,
    defaultImage,
    ...otherProps
  } = props

  const baseStyles = {
    width: `${width}px`,
    height: `${height}px`,
    lineHeight: `${height}px`,
    backgroundColor: item.image ? 'transparent' : backgroundColor,
    border: item.image && showBorderIfImage ? `3px solid ${backgroundColor}` : 'none',
  }

  if (!isNil(item.image)) {
    return (
      <Image { ...otherProps } image={ item.image } round={ round } baseStyles={ baseStyles } />
    )
  }
  if (!isNil(iconType)) {
    return <Icon { ...otherProps } icon={ iconType } round={ round } baseStyles={ baseStyles } />
  }
  if (!isNil(defaultImage)) {
    return <Image { ...otherProps } image={ defaultImage } round={ round } />
  }

  return (
    <MTAcronym
      { ...otherProps }
      item={ item }
      round={ round }
      height={ height }
      baseStyles={ baseStyles }
    />
  )
}

export default MTImage
