const formatPercent = (decimal = 0) => Number((decimal || 0).toFixed(2))

export const getSharpeRatioGrade = ({sharpeRatio}) => {
    const gradeScale = {
        [sharpeRatio < 0]: {value: 'low', label: 'Neg. Return', grade: 0},
        [sharpeRatio >= 0 && sharpeRatio < 0.5]: {
            value: 'average',
            label: 'Average',
            grade: 1,
        },
        [sharpeRatio >= 0.5 && sharpeRatio < 1]: {value: 'good', label: 'Good'},
        [sharpeRatio >= 1 && sharpeRatio < 1.5]: {
            value: 'great',
            label: 'Great',
            grade: 2,
        },
        [sharpeRatio >= 1.5 && sharpeRatio < 2]: {
            value: 'great',
            label: 'Distinction',
            grade: 3,
        },
        [sharpeRatio >= 2]: {value: 'brilliant', label: 'Brilliant', grade: 3},
        [typeof sharpeRatio !== 'number']: {value: 'unknown', label: 'Unknown'},
        default: {value: 'unknown', label: 'Unknown'},
    }

    return gradeScale.true || gradeScale.default
}

export const getDiversificationGrade = ({diversification}) => {
    const gradeScale = {
        [diversification < 0.5]: {
            value: 'low',
            label: 'Needs work',
            grade: 0,
        },
        [diversification >= 0.5 && diversification < 0.7]: {
            value: 'average',
            label: 'Average',
            grade: 1,
        },
        [diversification >= 0.7 && diversification < 1]: {
            value: 'good',
            label: 'Good',
        },
        [diversification >= 0.7 && diversification < 0.9]: {
            value: 'great',
            label: 'Great',
            grade: 2,
        },
        [diversification >= 0.9]: {
            value: 'great',
            label: 'Distinction',
            grade: 3,
        },
        [typeof diversification !== 'number']: {
            value: 'unknown',
            label: 'Unknown',
        },
        default: {value: 'unknown', label: 'Unknown'},
    }

    return gradeScale.true || gradeScale.default
}

export const makeDiversificationBarChartData = (data) => {
    if (!data || !Object.keys(data).length) {
        return []
    }
    const x = Object.keys(data).map((label) => {
        return label === 'currencies' ? 'Cash' : label
    })
    const result = [...Object.values(data)].reduce(
        (result, {longPercent, shortPercent, netPercent}) => {
            const [long, short, net] = result
            long.y.push(formatPercent(longPercent * 100))
            short.y.push(formatPercent(shortPercent * 100))

            // Create a horizontal marker to indicate the net value
            const height = 0.5 // A bit of a hack to make horizontal marker appear
            net.y.push(height)
            net.base.push(formatPercent(netPercent * 100 - height))

            return result
        },
        [
            {
                name: 'Long',
                x,
                y: [],
                color: '#43A39C',
                hoverinfo: 'name+y',
                mode: 'markers',
                opacity: 0.9,
            },
            {
                name: 'Short',
                x,
                y: [],
                color: '#B7E6E1',
                hoverinfo: 'name+y',
                mode: 'markers',
                opacity: 0.9,
            },
            {
                name: 'Net',
                x,
                y: [],
                base: [],
                color: '#FF6B6B',
                marker: {color: '#FF6B6B', line: {color: '#FF6B6B'}},
                hoverinfo: 'name+y',
            },
        ]
    )

    const hasData = Object.values(result)
        .slice(0, 2)
        .map((axis) => !axis.y.findIndex((el) => el > 0))
        .includes(true)

    if (!hasData) {
        result[2] = {
            name: 'Net',
            x: result[2].x,
            y: result[1].y,
            color: '#B3B3B2',
            mode: 'markers',
        }
    }
    return result
}

export const makeAssetBarChartData = (data) => {
    if (!data || !Object.keys(data).length) {
        return []
    }

    // Re-map the labels of the x-axis
    return makeDiversificationBarChartData({
        Equities: data.equities,
        Bonds: data.bonds,
        Options: data.options,
        ETFs: data.etfs,
    })
}

export const makeDiversificationRadarChartData = (data) => {
    const theta = ['Geography', 'Industry', 'Asset Class']
    const defaultResult = [{theta, r: []}]

    if (!data) {
        return defaultResult
    }

    const {assetClassScore, regionScore, sectorScore} = data

    const scores = [regionScore, sectorScore, assetClassScore]

    if (!scores.some((total) => !!total)) {
        return defaultResult
    }

    const result = scores.reduce((result, total) => {
        const [totalData] = result

        if (typeof total !== 'number') {
            total = 0
        }

        totalData.r.push(formatPercent(total * 100))

        return result
    }, defaultResult)

    return result
}
