#include "finite_integral_calculation.h"
#include "../distribution/normal_distribution.h"
#include <assert.h>
#include <ion.h>
#include <cmath>

namespace Probability {

FiniteIntegralCalculation::FiniteIntegralCalculation() :
  Calculation(),
  m_lowerBound(0.0),
  m_upperBound(1.0),
  m_result(0.0)
{
  compute(0);
}

I18n::Message FiniteIntegralCalculation::legendForParameterAtIndex(int index) {
  assert(index >= 0 && index < 3);
  if (index == 0) {
    return I18n::Message::FiniteIntegralFirstLegend;
  }
  if (index == 1) {
    return I18n::Message::FiniteIntegralLegend;
  }
  return I18n::Message::LeftIntegralSecondLegend;
}

void FiniteIntegralCalculation::setParameterAtIndex(double f, int index) {
  assert(index >= 0 && index < 3);
  if (index == 0) {
    m_lowerBound = f;
  }
  if (index == 1) {
    m_upperBound = f;
  }
  if (index == 2) {
    m_result = f;
  }
  compute(index);
}


double FiniteIntegralCalculation::parameterAtIndex(int index) {
  assert(index >= 0 && index < 3);
  if (index == 0) {
    return m_lowerBound;
  }
  if (index == 1) {
    return m_upperBound;
  }
  return m_result;
}

void FiniteIntegralCalculation::compute(int indexKnownElement) {
  if (m_distribution == nullptr) {
    return;
  }
  if (indexKnownElement == 2) {
    assert(m_distribution->type() == Distribution::Type::Normal);
    double p = (1.0+m_result)/2.0;
    m_upperBound = ((NormalDistribution *)m_distribution)->cumulativeDistributiveInverseForProbability(&p);
    m_lowerBound = 2.0*m_distribution->parameterValueAtIndex(0)-m_upperBound;
  }
  m_result = m_distribution->finiteIntegralBetweenAbscissas(m_lowerBound, m_upperBound);
}

}
