#include"Mesh_Base.h"

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

#include "../nlohmann/json.hpp"
using json = nlohmann::json;

void Mesh_2D::GetMesh(std::string str,std::string jstr)
{
	std::ifstream fd;
	fd.open(str.c_str());

	if (fd.is_open())
	{
		std::string data;
		//vertex
		fd >> data;
		if (data == "NbrVertex")
			fd >> _mNbrVertex;
		fd >> data;
		if (data == "Vertex")
		{
			_mVertex = Eigen::MatrixXd(_mNbrVertex, 3);
			for (int i = 0; i < _mNbrVertex; i++)
			{
				fd >> _mVertex(i, 0);
				fd >> _mVertex(i, 1);
				_mVertex(i, 2) = 0.;
			}
		}
		//edge
		fd >> data;
		if (data == "NbrEdge")
			fd >> _mNbrEdge;
		fd >> data;
		if (data == "Edge")
		{
			_mEdge = Eigen::MatrixXi(_mNbrEdge, 2);
			for (int i = 0; i < _mNbrEdge; i++)
			{
				fd >> _mEdge(i, 0);
				fd >> _mEdge(i, 1);
			}
		}
		//tri
		fd >> data;
		if (data == "NbrTri")
			fd >> _mNbrTri;
		fd >> data;
		if (data == "Tri")
		{
			_mTri = Eigen::MatrixXi(_mNbrTri, 3);
			for (int i = 0; i < _mNbrTri; i++)
			{
				fd >> _mTri(i, 0);
				fd >> _mTri(i, 1);
				fd >> _mTri(i, 2);
			}
		}
		fd >> data;
		if (data == "EdgeOfTri")
		{
			_mEdgeOfTri = Eigen::MatrixXi(_mNbrTri, 3);
			for (int i = 0; i < _mNbrTri; i++)
			{
				fd >> _mEdgeOfTri(i, 0);
				fd >> _mEdgeOfTri(i, 1);
				fd >> _mEdgeOfTri(i, 2);
			}
		}
		fd >> data;
		if (data == "DomainOfTri")
		{
			_mDomainOfTri = Eigen::VectorXi(_mNbrTri);
			for (int i = 0; i < _mNbrTri; i++)
			{
				fd >> _mDomainOfTri(i);
				_mDomainOfTri(i) = _mDomainOfTri(i) + 1;
			}
		}
		//edges
		fd >> data;
		if (data == "NbrEdges")
			fd >> _mNbrEdges;
		fd >> data;
		if (data == "CoonOfEdges")
		{
			_mCoonOfEdges = Eigen::MatrixXi(_mNbrEdges, 3);
			for (int i = 0; i < _mNbrEdges; i++)
			{
				fd >> _mCoonOfEdges(i, 0);
				fd >> _mCoonOfEdges(i, 1);
				_mCoonOfEdges(i, 0) = _mCoonOfEdges(i, 0);
				_mCoonOfEdges(i, 1) = _mCoonOfEdges(i, 1);
			}
		}
		fd >> data;
		if (data == "DomainOfEdges")
		{
			_mDomainOfEdges = Eigen::VectorXi(_mNbrEdges);
			for (int i = 0; i < _mNbrEdges; i++)
			{
				fd >> _mDomainOfEdges(i);
				_mDomainOfEdges(i) = _mDomainOfEdges(i) + 1;
			}
		}
		fd >> data;
		json js = nlohmann::json::parse(jstr);
		int nbrBoundary = js.at("NbrBoundary");
		if (data == "normal")
		{
			_mNormOfEdges = Eigen::MatrixXd(nbrBoundary, 3);
			for (int i = 0; i < nbrBoundary; i++)
			{
				fd >> _mNormOfEdges(i, 0);
				fd >> _mNormOfEdges(i, 1);
				fd >> _mNormOfEdges(i, 2);
			}
		}
		fd >> data;
		if (data == "NbrCopyOfEdges")
			fd >> _mNbrCopyOfEdges;
		fd >> data;
		if (data == "CopyOfEdges")
		{
			_mCopyOfEdges = Eigen::MatrixXi::Zero(_mNbrCopyOfEdges, 2);
			for (int i = 0; i < _mNbrCopyOfEdges; i++)
			{
				fd >> _mCopyOfEdges(i, 0);
				fd >> _mCopyOfEdges(i, 1);
				_mCopyOfEdges(i, 0) = _mCopyOfEdges(i, 0);
				_mCopyOfEdges(i, 1) = _mCopyOfEdges(i, 1);
			}
		}
		fd >> data;
		if (data == "NbrCopyOfVertexs")
			fd >> _mNbrCopyOfVertexs;
		fd >> data;
		if (data == "CopyOfVertexs")
		{
			_mCopyOfVertex = Eigen::MatrixXi::Zero(_mNbrCopyOfVertexs, 2);
			for (int i = 0; i < _mNbrCopyOfVertexs; i++)
			{
				fd >> _mCopyOfVertex(i, 0);
				fd >> _mCopyOfVertex(i, 1);
				_mCopyOfVertex(i, 0) = _mCopyOfVertex(i, 0);
				_mCopyOfVertex(i, 1) = _mCopyOfVertex(i, 1);
			}
		}
	}
	else
	{
		std::cout << "mesh read fail" << std::endl;
		//¶ÁÈ¡Ê§°Ü
	}

}
