import React from 'react'
import PropTypes from 'prop-types'
import modalTypes from 'containers/modal-manager/modal-types'
import BEMModule from 'utils/bem'
import styles from './styles.scss'

const bem = new BEMModule(styles)

const PLACEHOLDER = {
    description: '— —',
    name: '— —',
}

const CourseDescription = ({
    className,
    course: _course,
    maxLength,
    showModal,
}) => {
    const isBlank = !_course
    const course = isBlank ? PLACEHOLDER : _course
    const {name, description} = course
    const isTruncated = description?.length > maxLength
    const truncatedDescription = isTruncated
        ? `${description.slice(0, maxLength - 1)}…`
        : description

    const handleClick = () => {
        if (!isTruncated) {
            return
        }
        showModal({
            type: modalTypes.MODAL_INFO,
            modal: {
                info: description,
                title: `About ${name}`,
                type: 'default',
            },
        })
    }

    const rootClassNames = bem.classNames('c-course-description', className)
    const messageClassNames = bem.classNames('c-course-description__message', {
        isTruncated,
    })

    return (
        <div className={rootClassNames}>
            <h3>About {name}</h3>

            <h4 className={messageClassNames} onClick={handleClick}>
                {truncatedDescription || 'No description added'}
            </h4>
        </div>
    )
}

CourseDescription.propTypes = {
    className: PropTypes.string,
    course: PropTypes.object,
    maxLength: PropTypes.number,
    showModal: PropTypes.func,
}

CourseDescription.defaultProps = {
    maxLength: 150,
}

export default CourseDescription
