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

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

const ChallengeBanner = ({
    className,
    data,
    metric: Metric,
    progress,
    subtitle,
    title,
    total,
}) => {
    const rootClassNames = bem.classNames('challenge-banner', className)
    const titleClassNames = bem.classNames('challenge-banner__title')
    const subtitleClassNames = bem.classNames('challenge-banner__subtitle')
    const graphClassNames = bem.classNames('challenge-banner__graph')
    const labelClassNames = bem.classNames('challenge-banner__label')
    const progressClassNames = bem.classNames('challenge-banner__progress')
    const progressValueClassNames = bem.classNames(
        'challenge-banner__progress-value'
    )

    const messageIndex =
        progress >= total ? 5 : Math.floor((progress / total) * 5)

    const message = [
        'You are at the beginning of your journey.',
        'Great start!',
        'Half way through the journey…',
        'You are onto something.',
        'Last push!',
        'You have mastered the challenge!',
    ][messageIndex]

    const width = progress >= total ? 100 : (progress / total) * 100

    return (
        <React.Fragment>
            <div className={rootClassNames}>
                <section>
                    <h1 className={titleClassNames}>{title}</h1>
                    <h2 className={subtitleClassNames}>{subtitle}</h2>
                </section>
                <aside className={graphClassNames}>
                    <Metric data={data} />
                </aside>
            </div>
            <div className={labelClassNames}>
                <p>{message}</p>
                <p>{`${progress} / ${total}`}</p>
            </div>
            <div className={progressClassNames}>
                <div
                    className={progressValueClassNames}
                    style={{width: `${width}%`}}
                >
                    &nbsp;
                </div>
            </div>
        </React.Fragment>
    )
}

ChallengeBanner.propTypes = {
    className: PropTypes.string,
    data: PropTypes.object,
    metric: PropTypes.element,
    progress: PropTypes.number,
    subtitle: PropTypes.string,
    title: PropTypes.string,
    total: PropTypes.number,
}

export default ChallengeBanner
