import React from 'react'
import PropTypes from 'prop-types'

import styles from './styles.scss'
import BEMModule from 'utils/bem'
const bem = new BEMModule(styles)

const CourseNotFound = ({className, errors, isActive = true}) => {
    const rootClassNames = bem.classNames('c-class-not-found', className)
    const titleClassNames = bem.classNames('c-class-not-found__title')
    const descriptionClassNames = bem.classNames(
        'c-class-not-found__description'
    )

    const errorTypes = {
        notFound: {
            title: 'Was that the correct Challenge ID?',
            description:
                'We could’t find that challenge. Please, double check the Challenge ID or contact the organizer. If you need additional help, click the chat bubble in the bottom-right corner!',
        },
        duplicatePortfolio: {
            title: 'You have already registered',
            description:
                'It seems like you already have a portfolio associated with this challenge. You can only have one portfolio per challenge.',
        },
        courseEnded: {
            title: 'This challenge has ended',
            description:
                'It looks like this challenge has already ended. Double-check the Challenge ID or ask the organizer for help.',
        },
        courseDeleted: {
            title: 'This challenge  was deleted',
            description:
                'The organizer has deleted this challenge. Check with them for the correct Challenge ID.',
        },
        unknown: {
            title: 'Unknown error…',
            description:
                'Something went wrong on our side. We are taking a look!',
        },
    }

    const getError = () => {
        if (errors && errors.some((e) => e.code === 1202)) {
            return errorTypes.notFound
        }

        if (errors && errors.some((e) => e.code === 'has-ended')) {
            return errorTypes.courseEnded
        }

        if (errors && errors.some((e) => e.code === 'has-registered')) {
            return errorTypes.duplicatePortfolio
        }

        if (!isActive) {
            return errorTypes.courseDeleted
        }

        return errorTypes.unknown
    }

    const error = getError()

    return (
        <div className={rootClassNames}>
            <h3 className={titleClassNames}>{error.title}</h3>
            <p className={descriptionClassNames}>{error.description}</p>
        </div>
    )
}

CourseNotFound.propTypes = {
    className: PropTypes.string,
    errors: PropTypes.array,
    isActive: PropTypes.bool,
}

export default CourseNotFound
