#include "calculate.hpp"

#include <limits>

namespace phosphor::virtual_sensor
{

double calculateModifiedMedianValue(std::vector<double>& values)
{
    size_t size = values.size();
    std::sort(values.begin(), values.end());
    switch (size)
    {
        case 2:
            /* Choose biggest value */
            return values.at(1);
        case 0:
            return std::numeric_limits<double>::quiet_NaN();
        default:
            /* Choose median value */
            if (size % 2 == 0)
            {
                // Average of the two middle values
                return (values.at(size / 2) + values.at(size / 2 - 1)) / 2;
            }
            else
            {
                return values.at((size - 1) / 2);
            }
    }
}

double calculateMaximumValue(std::vector<double>& values)
{
    auto maxIt = std::max_element(values.begin(), values.end());
    if (maxIt == values.end())
    {
        return std::numeric_limits<double>::quiet_NaN();
    }
    return *maxIt;
}

double calculateMinimumValue(std::vector<double>& values)
{
    auto maxIt = std::min_element(values.begin(), values.end());
    if (maxIt == values.end())
    {
        return std::numeric_limits<double>::quiet_NaN();
    }
    return *maxIt;
}

std::map<Interface, CalculationFunc> calculationIfaces{
    {"xyz.openbmc_project.Configuration.Maximum", calculateMaximumValue},
    {"xyz.openbmc_project.Configuration.Minimum", calculateMinimumValue},
    {"xyz.openbmc_project.Configuration.ModifiedMedian",
     calculateModifiedMedianValue}};

} // namespace phosphor::virtual_sensor
