#include <sstream>
#include <string>
#include "node.hpp"
#include "parser.hpp"
#include "Vector3D.hpp"

int main(int argc, char *argv[])
{
	std::string lin;
	lin= "";
	std::cout << "x(t)= \n";
	while (!std::cin.eof() && std::cin.peek()!=';')
		lin+= char(std::cin.get());
	lin+= std::cin.get();
	Parser px(lin, NULL, NULL);
	Node* x= px.Tree();
	Node* xp= x->deriv();
	std::cout << "x(t):= " <<  x->print() << std::endl;
	std::cout << "xp(t):= " <<  xp->print() << std::endl;

	lin= "";
	std::cout << "y(t)= \n";
	while (!std::cin.eof() && std::cin.peek()!=';')
		lin+= char(std::cin.get());
	lin+= std::cin.get();
	Parser py(lin, NULL, NULL);
	Node* y= py.Tree();
	Node* yp= y->deriv();
	std::cout << "y(t):= " <<  y->print() << std::endl;
	std::cout << "yp(t):= " <<  yp->print() << std::endl;

	lin= "";
	std::cout << "z(t)= \n";
	while (!std::cin.eof() && std::cin.peek()!=';')
		lin+= char(std::cin.get());
	lin+= std::cin.get();
	Parser pz(lin, NULL, NULL);
	Node* z= pz.Tree();
	Node* zp= z->deriv();
	std::cout << "z(t):= " <<  z->print() << std::endl;
	std::cout << "zp(t):= " <<  zp->print() << std::endl;

	std::cout << "deriv: " <<  xp->print() << std::endl;
	std::cout << "deriv: " <<  yp->print() << std::endl;
	std::cout << "deriv: " <<  zp->print() << std::endl;

	Vector3D sky;
	Vector3D xc, yc, zc;
	
	double a, b;
	std::cout << "Dar a b:\n";
	std::cin >> a >> b;
	int N;
	std::cout << "N:\n";
	std::cin >> N;
	double h= (a+b)/N;
	std::cout << "M:\n";
	std::cin >> M;

	Vector3D sky(0,0,1);
	for (int k=0; k<=N; ++k) {
		double t= a + t*h;
		Vector3D p(x(t), y(t), z(t));
		Vector3D u(xp->eval(t), yp->eval(t), zp->eval(t));
		u= unit(u);
		Vector3D v= sky - (sky*u)*u;
		v= unit(v);
		Vector3D w= u % v;
		double ta= 0.0;
		double tb= 2.0*M_PI;
		double ht= (tb-ta)/M;
		for (int i=0; i<M; ++i) {
			double th= ta + i * ht;
			std::cout << p + cos(th)*w + sin(th)*v<< std::endl;
		}
	}

	
	delete zp;
	delete yp;
	delete xp;
	delete z;
	delete y;
	delete x;

	return EXIT_SUCCESS;
}
