/*------------------------------------------------------------------*\
|*			Visualization functions	     	            *|
 \*------------------------------------------------------------------*/
void plotFunction(FunctionENUM function)
    {
    switch (function)
	{
    case FUNCTION1:
	for (double i = LEFT; i <= RIGHT; i += 0.001)
	    {
	    graphWin.plot(i, f1(i), 1);
	    }
	break;

    case FUNCTION2:
	for (double i = LEFT; i <= RIGHT; i += 0.0001)
	    {
	    graphWin.plot(i, f2(i), 1);
	    }
	break;
    case GOLD_FUNCTION:
	for (int i = 0; i < 19; i++)
	    {
	    graphWin.segment(map(i, 0, 19, 0, RIGHT), map(arrayGold[i], 250, 1700, 0, TOP), map(i + 1, 0, 19, 0, RIGHT), map(arrayGold[i + 1], 250, 1700, 0, TOP));
	    }
	break;
    default:
	cout << "The given function does not exist";
	break;
	}
    }
/****************************************************
 *                  Helper functions
 ****************************************************/
void plotCurrentFunction()
{
    for(double i = LEFTLIMIT; i <= RIGHTLIMIT; i = i + 0.01)
    {
#ifdef USING_FIRST_FUNCTION
        graphWin.plot(i,f(i),1);
#else
        graphWin.plot(i,g(i),1);
#endif
    }
}
void plotAdaptedFunction(double lambda)
{
    for(double i = LEFTLIMIT; i < RIGHTLIMIT; i += 0.01)
    {
        graphWin.plot(i, fixedPointAdapterFunction(i, lambda), 1);
    }
}
void drawFunctions()
{
    const float STEP = graphWin.findSmartStepX();
    glPointSize(2.0f);

    //Draw selected  (f(x))
    for(float x=graphWin.xMin(); x<graphWin.xMax(); x+=STEP)
        graphWin.plot(x, f(x), 1.0f,0.5f,0.0f);

    //Draw f(x) = x
    for(float x=graphWin.xMin(); x<graphWin.xMax(); x+=STEP)
        graphWin.plot( x, x, 0.5f,0.5f,0.5f);

    //Draw g(x)
    for(float x=graphWin.xMin()+STEP; x<graphWin.xMax(); x+=STEP)
        graphWin.plot(x, g(x), 1.0f,0.5f,1.0f);

    clear();
    printHeader();
    std::cout << "x est compris dans l'ensemble [" << graphWin.xMin() << ";" << graphWin.xMax() << "[" << std::endl;
    std::cout << "y est compris dans l'ensemble [" << graphWin.yMin() << ";" << graphWin.yMax() << "[" << std::endl << std::endl;
    std::cout << "Information graphique : " << std::endl
              << "- Fonction choisie (f(x)) en orange" << std::endl
              << "- Fonction h(x) = x, en gris" << std::endl
              << "- Fonction g(x) = x + l*f(x), l = 1, en rose" << std::endl
              << "- Les axes x,y en bleu " << std::endl
              << "- Les vecteurs unitaires en rouge " << std::endl
              << "- Les solutions de la fonction en vert" << std::endl << std::endl;

    vector<long double> solutions = findRoot();
    std::cout << "Solutions : " << std::endl;

    glColor3f(0.0f, 1.0f, 0.0f);
    glPointSize(10);

    //Draw solutions
    for(unsigned int i = 0;i < solutions.size(); ++i)
    {
        std::cout << "[" << i << "] -> " << static_cast<double>(solutions[i]) << std::endl;
        glBegin( GL_POINTS );
            glVertex3d(solutions[i], 0, 0.0);
        glEnd();
    }
}
void plotSecondDegreeDerivatedFunction(FunctionENUM function, double h)
    {
    if (function != GOLD_FUNCTION)
	{
	for (double i = LEFT; i <= RIGHT; i += 0.001)
	    {
	    graphWin.plot(i, calculateSecondDegreeDerivative(function, i, h), 1);
	    }
	}
    else
	{
	for (double i = 0; i < 18; i += 1)
	    {
	    double secondDegreeDerivative = calculateSecondDegreeDerivative(function, i, h);
	    double secondDegreeDerivativePlusOne = calculateSecondDegreeDerivative(function, i + 1, h);
	    graphWin.segment(map(i, 0, 19, 0, RIGHT), map(secondDegreeDerivative, 250, 1700, 0, TOP), map(i + 1, 0, 19, 0, RIGHT), map(secondDegreeDerivativePlusOne, 250, 1700, 0, TOP));
	    }
	}
    }
void plotDerivatedFunction(FunctionENUM function, DerivationMethodENUM derivationMethod, double h)
    {
    if (function != GOLD_FUNCTION)
	{
	for (double i = LEFT; i <= RIGHT; i += 0.001)
	    {
	    graphWin.plot(i, calculateDerivative(function, i, h, derivationMethod), 1);
	    }
	}
    else
	{
	double leftLimit = derivationMethod == CENTRAL_DIFFERENCE ? 1 : 0;

	for (double i = leftLimit; i < 18; i += 1)
	    {
	    double derivative = calculateDerivative(function, i, h, derivationMethod);
	    double derivativePlusOne = calculateDerivative(function, i + 1, h, derivationMethod);
	    graphWin.segment(map(i, 0, 19, 0, RIGHT), map(derivative, 250, 1700, 0, TOP), map(i + 1, 0, 19, 0, RIGHT), map(derivativePlusOne, 250, 1700, 0, TOP));
	    }
	}

    }
void fixedPoint(double epsilon, double lambda, double startingPoint,bool isFirst)
{
    double previousPoint = startingPoint;
    int loopCounter = 0;
    while (fabs(fixedPointAdapterFunction(previousPoint) - previousPoint) > epsilon && loopCounter < LOOP_LIMIT)
    {
        previousPoint=fixedPointAdapterFunction(previousPoint, lambda);
        if(numberLine == 2)
        {
            plotAdaptedFunction(lambda);//we plot it here to have the proper lamda drawn
            graphWin.segment(previousPoint, fixedPointAdapterFunction(previousPoint, lambda), fixedPointAdapterFunction(previousPoint, lambda), fixedPointAdapterFunction(previousPoint, lambda));
            graphWin.segment(previousPoint, previousPoint, previousPoint, fixedPointAdapterFunction(previousPoint, lambda));
        }
        loopCounter++;
    }

#ifdef USING_FIRST_FUNCTION
    if(f(previousPoint) <= epsilon)
#else
        if(g(previousPoint) <= epsilon)
#endif
        {
            graphWin.plot(previousPoint,fixedPointAdapterFunction(previousPoint, lambda),5);
            cout << "x = " << previousPoint << endl;
        }
        else
        {
            cout << "x MAUVAIS = " << previousPoint << endl;
        }

    double tester = 2.0;
    if(lambda > tester)
    {
        numberLine++;
        lambda -= tester;
        fixedPoint(epsilon, lambda,startingPoint);
    }
}
void plotDerivationMethod(FunctionENUM function, double x, double h, DerivationMethodENUM derivativeMethod)
    {
    switch (function)
	{
    case FUNCTION1:
	switch (derivativeMethod)
	    {
	case PROGRESSIVE_DIFFERENCE:
	    for (double delta = 0; delta <= h; delta += h)
		{
		graphWin.plot(x + delta, f1(x + delta), 4);
		graphWin.segment(x + delta, 0, x + delta, f1(x + delta));
		graphWin.segment(0, f1(x + delta), x + delta, f1(x + delta));
		}

	    //Segment between two points
	    graphWin.segment(x, f1(x), x + h, f1(x + h));

	    break;
	case CENTRAL_DIFFERENCE:
	    for (double delta = -h; delta <= h; delta += h)
		{
		//Points
		graphWin.plot(x + delta, f1(x + delta), 4);
		//Lines from X axe
		graphWin.segment(x + delta, 0, x + delta, f1(x + delta));
		//Lines from Y axe
		graphWin.segment(0, f1(x + delta), x + delta, f1(x + delta));
		}
	    //Segment between two points
	    graphWin.segment(x - h, f1(x - h), x + h, f1(x + h));
	    break;
	case FOURTH_DEGREE_POLYNOM:
	    for (double delta = -h; delta <= h; delta += h / 2)
		{
		graphWin.segment(x + delta, 0, x + delta, f1(x + delta));
		graphWin.plot(x + delta, f1(x + delta), 4);
		}
	    break;
	default:
	    break;
	    }
	break;
    case FUNCTION2:
	switch (derivativeMethod)
	    {
	case PROGRESSIVE_DIFFERENCE:
	    for (double delta = 0; delta <= h; delta += h)
		{
		graphWin.plot(x + delta, f2(x + delta), 4);
		graphWin.segment(x + delta, 0, x + delta, f2(x + delta));
		graphWin.segment(0, f2(x + delta), x + delta, f2(x + delta));
		}

	    //Segment between two points
	    graphWin.segment(x, f2(x), x + h, f2(x + h));
	    break;
	case CENTRAL_DIFFERENCE:
	    for (double delta = -h; delta <= h; delta += h)
		{
		//Points
		graphWin.plot(x + delta, f2(x + delta), 4);
		//Lines from X axe
		graphWin.segment(x + delta, 0, x + delta, f2(x + delta));
		//Lines from Y axe
		graphWin.segment(0, f2(x + delta), x + delta, f2(x + delta));
		}
	    //Segment between two points
	    graphWin.segment(x - h, f2(x - h), x + h, f2(x + h));
	    break;
	case FOURTH_DEGREE_POLYNOM:
	    for (double delta = -h; delta <= h; delta += h / 2)
		{
		graphWin.segment(x + delta, 0, x + delta, f2(x + delta));
		graphWin.plot(x + delta, f2(x + delta), 4);
		}
	    break;
	default:
	    break;
	    }
	break;
    default:
	break;
	}
    }