#pragma once

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>

class fileWriter
{
private: 
	std::string filepath;
	std::ofstream file;

public:
	fileWriter(const std::string& filepath) : filepath(filepath)
	{
		file.open(filepath);
		if (!file.is_open())
		{
			std::cerr << "Error: could not open the file for writing. \n";
		}
		else
		{
			file << "Time (s), Position (m), Velocity (m/s)\n";
		}
	}
	~fileWriter()
	{
		if (file.is_open())
		{
			file.close();
		}
		std::cout << "Simulation is completed. Results were saved to: " << filepath;
	}
	void log(float time, float position, float velocity)
	{
		std::cout << "Time: " << time
			<< "s position: " << position
			<< "m, Velocity: " << velocity
			<< std::endl;
	}
	void write(float time, float position, float velocity)
	{
		log(time, position, velocity);

		if (file.is_open())
		{
			file << std::fixed << std::setprecision(2) << time << ","
				<< std::fixed << std::setprecision(4) << position << ","
				<< std::fixed << std::setprecision(4) << velocity << "\n";
		}
	}


};

