import React from 'react'
import PropTypes from 'prop-types'
import StockTimeseriesChart from 'components/stock-timeseries-chart'
import BEMModule from 'utils/bem'
import styles from './style.scss'

const bem = new BEMModule(styles)

const StatisticList = ({list}) => (
    <dl className={bem.classNames('c-stock-overview__list')}>
        {list.map((statistic) => (
            <div
                className={bem.classNames('c-stock-overview__row')}
                key={statistic.label}
            >
                <dt>{statistic.label}</dt>
                <dd>{statistic.value}</dd>
            </div>
        ))}
    </dl>
)

StatisticList.propTypes = {
    list: PropTypes.arrayOf(PropTypes.object),
}

class StockOverview extends React.PureComponent {
    get statistics() {
        if (!this.props.stock) {
            return []
        }

        const {
            avgVol10DayDisplay,
            avgVol50DayDisplay,
            dayHighDisplay,
            dayLowDisplay,
            dividendPayDate,
            dividendYieldPercentageDisplay,
            exDividendDate,
            lastClosePriceDisplay,
            openPriceDisplay,
            priceToEarningsRatioDisplay,
            yearHighPriceDisplay,
            yearLowPriceDisplay,
        } = this.props.stock.statistics

        const stats = {}

        stats.left = [
            {label: 'Open', value: openPriceDisplay},
            {label: 'High', value: dayHighDisplay},
            {label: 'Low', value: dayLowDisplay},
            {label: 'P/E ratio', value: priceToEarningsRatioDisplay},
            {label: 'Div yield', value: dividendYieldPercentageDisplay},
            {label: 'Ex-dividend date', value: exDividendDate},
        ]

        stats.right = [
            {label: 'Prev close', value: lastClosePriceDisplay},
            {label: '52-wk high', value: yearHighPriceDisplay},
            {label: '52-wk low', value: yearLowPriceDisplay},
            {label: 'Avg vol (10d)', value: avgVol10DayDisplay},
            {label: 'Avg vol (50d)', value: avgVol50DayDisplay},
            {label: 'Dividend date', value: dividendPayDate},
        ]

        return stats
    }

    render() {
        const {getSecurityIntradayMarketValueTimeseries} = this.props
        const headingClassNames = bem.classNames('c-stock-overview__heading')
        const statisticsClassNames = bem.classNames(
            'c-stock-overview__statistics'
        )
        const graphClassNames = bem.classNames('c-stock-overview__graph')

        return (
            <React.Fragment>
                <StockTimeseriesChart
                    className={graphClassNames}
                    getIntradayTimeseries={
                        getSecurityIntradayMarketValueTimeseries
                    }
                    timeseries={this.props.timeseries}
                />
                <h2 className={headingClassNames}>Statistics</h2>
                <section className={statisticsClassNames}>
                    <StatisticList list={this.statistics.left} />
                    <StatisticList list={this.statistics.right} />
                </section>
            </React.Fragment>
        )
    }
}

StockOverview.propTypes = {
    getSecurityIntradayMarketValueTimeseries: PropTypes.func,
    stock: PropTypes.shape({
        statistics: PropTypes.object.isRequired,
    }),
    timeseries: PropTypes.shape({
        currency: PropTypes.string,
        date: PropTypes.array,
        close: PropTypes.array,
    }),
}

export default StockOverview
