#include <cmath>
#include <fstream>
#include <GL/glut.h>
#include <iostream>
#include <string>
#include <vector>

#include "globale.h"
#include "info.h"
#include "calcul.h"
#include "display.h"
double Derive(double x, int ordre, const type_formule& Formule);

std::vector<double> Derivees;
std::vector<double> Ordonnees;
type_information LesInfos;


int main(int argc, char** argv)
{
	std::ifstream Fic("info.txt");
	unsigned int i, j;
	double Point;
	double Temp;
		
	if (Fic.fail())
		exit(1);

	LesInfos = type_information(Fic);

	Derivees.push_back(LesInfos.y0);
	Derivees.push_back(LesInfos.y1);

	type_formule LaFormule(LesInfos.Formule);

	std::cout << "y0 :\t" << Derivees[0] << std::endl;
	std::cout << "y1 :\t" << Derivees[1] << std::endl;

	for (i = 2; i <= LesInfos.ordre; i++)
	{
		Derivees.push_back(Derive(LesInfos.x0, i, LaFormule));
	}

	for (j = 0; j < Derivees.size(); j++)
		std::cout << "y" << j << " :\t" << Derivees[j] << std::endl;

	for (double x = LesInfos.xmin; x < LesInfos.xmax; x+=LesInfos.dx)
	{
		Point = Derivees[0];

		for (i = 1; i < Derivees.size(); i++)
		{
			Temp = Derivees[i]*pow(x-LesInfos.x0, i);

			for (j = i - 1; j > 1; j--)
				Temp /= double(j);

			Point += Temp;
		}

		Ordonnees.push_back(Point);
	}

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - WIN_X) >> 1,
						   (glutGet(GLUT_SCREEN_HEIGHT) - WIN_Y) >> 1);
	glutInitWindowSize(WIN_X, WIN_Y);
	glutCreateWindow("Solution en série");

	glClearColor(0,0,0.3,0);

	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	
	glutMainLoop();

	return 0;
}
