#include"Post_Base.h"

#include<string>
#include <fstream>
#include <iostream>
#include <iomanip>

#ifdef _WIN32
#include <direct.h>
static void EnsureOutputDir(const std::string& dir)
{
	_mkdir(dir.c_str());
}
#else
#include <sys/stat.h>
static void EnsureOutputDir(const std::string& dir)
{
	mkdir(dir.c_str(), 0755);
}
#endif

void Post_2D_EigenMode::OutputData(std::string str)
{
	std::ofstream outNeff, outEx, outEy, outEz, outNormE;
	outNeff.open(str+"/neff", std::ios::out | std::ios::trunc);
	outEx.open(str + "/Ex", std::ios::out | std::ios::trunc);
	outEy.open(str + "/Ey", std::ios::out | std::ios::trunc);
	outEz.open(str + "/Ez", std::ios::out | std::ios::trunc);
	outNormE.open(str + "/normE", std::ios::out | std::ios::trunc);
	for (int i = 0; i < _mNeff.rows(); i++)
	{
		outNeff << _mNeff(i).real() << " " << _mNeff(i).imag() << endl;
		outEx << "//";
		outEy << "//";
		outEz << "//";
		outNormE<< "//";
		for (int j = 0; j < _mEx.rows(); j++)
		{
			outEx << _mEx(j, i).real() << " " << _mEx(j, i).imag() << endl;
			outEy << _mEy(j, i).real() << " " << _mEy(j, i).imag() << endl;
			outEz << _mEz(j, i).real() << " " << _mEz(j, i).imag() << endl;
			outNormE << _mNormE(j, i) << endl;
		}
	}
	outNeff.close();
	outEx.close();
	outEy.close();
	outEz.close();
	outNormE.close();
}

void Post_2D_EigenFreq::OutputData(std::string str)
{
	std::ofstream outFreq, outEx, outEy, outEz, outNormE;
	outFreq.open(str + "/freq", std::ios::out | std::ios::trunc);
	outEx.open(str + "/Ex", std::ios::out | std::ios::trunc);
	outEy.open(str + "/Ey", std::ios::out | std::ios::trunc);
	outEz.open(str + "/Ez", std::ios::out | std::ios::trunc);
	outNormE.open(str + "/normE", std::ios::out | std::ios::trunc);
	for (int i = 0; i < _mFreq.rows(); i++)
	{
		outFreq << _mFreq(i).real() << " " << _mFreq(i).imag() << endl;
		outEx << "//";
		outEy << "//";
		outEz << "//";
		outNormE << "//";
		for (int j = 0; j < _mEx.rows(); j++)
		{
			outEx << _mEx(j, i).real() << " " << _mEx(j, i).imag() << endl;
			outEy << _mEy(j, i).real() << " " << _mEy(j, i).imag() << endl;
			outEz << _mEz(j, i).real() << " " << _mEz(j, i).imag() << endl;
			outNormE << _mNormE(j, i) << endl;
		}
	}
	outFreq.close();
	outEx.close();
	outEy.close();
	outEz.close();
	outNormE.close();
}

void Post_2D_Scatter::OutputData(std::string str)
{
	std::ofstream outEx, outEy, outEz, outNormE;
	outEx.open(str + "/Ex", std::ios::out | std::ios::trunc);
	outEy.open(str + "/Ey", std::ios::out | std::ios::trunc);
	outEz.open(str + "/Ez", std::ios::out | std::ios::trunc);
	outNormE.open(str + "/normE", std::ios::out | std::ios::trunc);

	outEx << "//";
	outEy << "//";
	outEz << "//";
	outNormE << "//";
	for (int j = 0; j < _mEx.rows(); j++)
	{
		outEx << _mEx(j).real() << " " << _mEx(j).imag() << endl;
		outEy << _mEy(j).real() << " " << _mEy(j).imag() << endl;
		outEz << _mEz(j).real() << " " << _mEz(j).imag() << endl;
		outNormE << _mNormE(j) << endl;
	}

	outEx.close();
	outEy.close();
	outEz.close();
	outNormE.close();
}

void Post_3D_EigenFreq::OutputData(std::string str)
{
	EnsureOutputDir(str);
	std::ofstream outFreq, outEx, outEy, outEz, outNormE;
	outFreq.open(str + "/freq", std::ios::out | std::ios::trunc);
	outEx.open(str + "/Ex", std::ios::out | std::ios::trunc);
	outEy.open(str + "/Ey", std::ios::out | std::ios::trunc);
	outEz.open(str + "/Ez", std::ios::out | std::ios::trunc);
	outNormE.open(str + "/normE", std::ios::out | std::ios::trunc);
	for (int i = 0; i < _mFreq.rows(); i++)
	{
		outFreq << _mFreq(i).real() << " " << _mFreq(i).imag() << std::endl;
		outEx << "//" << std::endl;
		outEy << "//" << std::endl;
		outEz << "//" << std::endl;
		outNormE << "//" << std::endl;
		for (int j = 0; j < _mEx.rows(); j++)
		{
			outEx << _mEx(j, i).real() << " " << _mEx(j, i).imag() << std::endl;
			outEy << _mEy(j, i).real() << " " << _mEy(j, i).imag() << std::endl;
			outEz << _mEz(j, i).real() << " " << _mEz(j, i).imag() << std::endl;
			outNormE << _mNormE(j, i) << std::endl;
		}
	}
	outFreq.close();
	outEx.close();
	outEy.close();
	outEz.close();
	outNormE.close();
}

void Post_3D_Scatter::OutputData(std::string str)
{
	EnsureOutputDir(str);
	std::ofstream outEx, outEy, outEz, outNormE;
	outEx.open(str + "/Ex", std::ios::out | std::ios::trunc);
	outEy.open(str + "/Ey", std::ios::out | std::ios::trunc);
	outEz.open(str + "/Ez", std::ios::out | std::ios::trunc);
	outNormE.open(str + "/normE", std::ios::out | std::ios::trunc);

	outEx << "//" << std::endl;
	outEy << "//" << std::endl;
	outEz << "//" << std::endl;
	outNormE << "//" << std::endl;
	for (int j = 0; j < _mEx.rows(); j++)
	{
		outEx << _mEx(j).real() << " " << _mEx(j).imag() << std::endl;
		outEy << _mEy(j).real() << " " << _mEy(j).imag() << std::endl;
		outEz << _mEz(j).real() << " " << _mEz(j).imag() << std::endl;
		outNormE << _mNormE(j) << std::endl;
	}

	outEx.close();
	outEy.close();
	outEz.close();
	outNormE.close();
}
