package net.pokeranalytics.android.calculus

import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType

/**
 * An enum representing all the types of Session statistics
 */
enum class Stat : RowRepresentable {

    NETRESULT,
    HOURLY_RATE,
    AVERAGE,
    NUMBER_OF_SETS,
    NUMBER_OF_GAMES,
    DURATION,
    AVERAGE_DURATION,
    NET_BB_PER_100_HANDS,
    HOURLY_RATE_BB,
    AVERAGE_NET_BB,
    WIN_RATIO,
    AVERAGE_BUYIN,
    ROI,
    STANDARD_DEVIATION,
    STANDARD_DEVIATION_HOURLY,
    STANDARD_DEVIATION_BB_PER_100_HANDS,
    HANDS_PLAYED;

    /**
     * Returns whether the stat evolution values requires a distribution sorting
     */
    fun hasDistributionSorting() : Boolean {
        when (this) {
            STANDARD_DEVIATION, STANDARD_DEVIATION_HOURLY, STANDARD_DEVIATION_BB_PER_100_HANDS -> return true
            else -> return false
        }
    }

    companion object {

        fun returnOnInvestment(netResult: Double, buyin: Double) : Double {
            return netResult / buyin
        }

        fun netBBPer100Hands(netBB: Double, numberOfHands: Double) : Double {
            return netBB / numberOfHands * 100
        }

    }

    override val resId: Int?
        get() {
            return when (this) {
                NETRESULT -> R.string.net_result
                HOURLY_RATE -> R.string.average_hour_rate
                AVERAGE -> R.string.average
                NUMBER_OF_SETS -> R.string.number_of_groups
                NUMBER_OF_GAMES -> R.string.number_of_games
                DURATION -> R.string.duration
                AVERAGE_DURATION -> R.string.average_duration
                NET_BB_PER_100_HANDS -> R.string.net_bb_per_100_hands
                HOURLY_RATE_BB -> R.string.hourly_rate_bb
                AVERAGE_NET_BB -> R.string.average_net_bb
                WIN_RATIO -> R.string.win_ratio
                AVERAGE_BUYIN -> R.string.average_buyin
                ROI -> R.string.roi
                STANDARD_DEVIATION -> R.string.standard_deviation
                STANDARD_DEVIATION_HOURLY -> R.string.standard_deviation_hourly
                STANDARD_DEVIATION_BB_PER_100_HANDS -> R.string.standard_deviation_bb_per_100_hands
                HANDS_PLAYED -> R.string.hands_played
            }
        }

    override val viewType: Int = RowViewType.TITLE_VALUE.ordinal
}

/**
 * ComputedStat contains a [stat] and their associated [value]
 */
class ComputedStat(stat: Stat, value: Double) {

    constructor(stat: Stat, value: Double, previousValue: Double?) : this(stat, value) {
        if (previousValue != null) {
            this.variation = (value - previousValue) / previousValue
        }
    }

    /**
     * The statistic type
     */
    var stat: Stat = stat

    /**
     * The stat value
     */
    var value: Double = value

    /**
     * The variation of the stat
     */
    var variation: Double? = null

    /**
     * Formats the value of the stat to be suitable for display
     */
    fun format() : StatFormat {
        return StatFormat()
    }

    /**
     * Returns a StatFormat instance for an evolution value located at the specified [index]
     */
    fun evolutionValueFormat(index: Int) : StatFormat {
        return StatFormat()
    }

}
