#include "Stdafx.h"

extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lkf5defs.h"
}

using namespace System::IO;

#include "LKFContext.h"
#include "LKFNeoPaedContext.h"

namespace lkfxdok {
	namespace c {

		LKFNeoPaedContext::LKFNeoPaedContext(LKFContextWithData ^context)
		{
			_context = context;
			_contextPtr = context->_contextPtr;
		}

		unsigned int LKFNeoPaedContext::WarningLineCount::get(void) {
			return lkf_npplaus_getWarningLineCount((LKF_CONTEXT *)_contextPtr);
		}

		String ^LKFNeoPaedContext::GetWarningLine(unsigned int index) {
			char buf[2048];
			if (lkf_npplaus_getWarningLine((LKF_CONTEXT *)_contextPtr, index, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		String ^LKFNeoPaedContext::GetWarningInfoLine(unsigned int index) {
			char buf[2048];
			if (lkf_npplaus_getInfoLine((LKF_CONTEXT *)_contextPtr, index, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		String ^LKFNeoPaedContext::GetProtocolHeadline() {
			char buf[2048];
			if (lkf_npplaus_getProtokollHeadline((LKF_CONTEXT *)_contextPtr, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		String ^LKFNeoPaedContext::GetProtocolLine(unsigned int index) {
			char buf[2048];
			if (lkf_npplaus_getProtokollLine((LKF_CONTEXT *)_contextPtr, index, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		String ^LKFNeoPaedContext::GetSumAbtHeadline() {
			char buf[2048];
			if (lkf_npplaus_sum_headline((LKF_CONTEXT *)_contextPtr, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		String ^LKFNeoPaedContext::GetSumAbtLine(unsigned int index) {
			char buf[2048];
			if (lkf_npplaus_sum_abtline((LKF_CONTEXT *)_contextPtr, index, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		unsigned int LKFNeoPaedContext::GetReasonCount(unsigned int nWarnIndex) {
			return lkf_npplaus_getReasonCount((LKF_CONTEXT *)_contextPtr, nWarnIndex);
		}

		String ^LKFNeoPaedContext::GetReasonLine(unsigned int nWarnIndex, unsigned int reasonIndex) {
			char buf[2048];
			if (lkf_npplaus_getReasonLine((LKF_CONTEXT *)_contextPtr, nWarnIndex, reasonIndex, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}


		unsigned int LKFNeoPaedContext::ProtocolLineCount::get(void) {
			return lkf_npplaus_getProtokollLineCount((LKF_CONTEXT *)_contextPtr);
		}

		unsigned int LKFNeoPaedContext::AbtCount::get(void) {
			return lkf_npplaus_sum_abtcount((LKF_CONTEXT *)_contextPtr);
		}

		void LKFNeoPaedContext::WriteWarningFile(String ^outputFileName, bool includeReason) {
			StreamWriter ^f = gcnew StreamWriter(outputFileName);

			unsigned int lineCount = WarningLineCount;

			for (unsigned int index = 0; index < lineCount; index++) {
				String ^warningLine = GetWarningLine(index);
				String ^infoLine = GetWarningInfoLine(index);
				f->WriteLine(warningLine);
				f->WriteLine(infoLine);
				unsigned int reasonCount = GetReasonCount(index);
				for (unsigned int reasonIndex = 0; reasonIndex < reasonCount; reasonIndex++) {
					String ^reasonLine = GetReasonLine(index, reasonIndex);
					f->WriteLine(reasonLine);
				}
			}
			f->Close();
		}

		void LKFNeoPaedContext::WriteProtokollFile(String ^outputFileName) {
			StreamWriter ^f = gcnew StreamWriter(outputFileName);

			unsigned int lineCount = ProtocolLineCount;
			String ^headline = GetProtocolHeadline();
			f->WriteLine(headline);

			for (unsigned int index = 0; index < lineCount; index++) {
				String ^line = GetProtocolLine(index);
				f->WriteLine(line);
			}
			f->Close();
		}

		void LKFNeoPaedContext::WriteAbtFile(String ^outputFileName, bool abtsum, bool tfsum, bool critsum) {
			StreamWriter ^f = gcnew StreamWriter(outputFileName);

			unsigned int lineCount = AbtCount;
			String ^headline;

			if (abtsum) {
				headline = GetSumAbtHeadline();
				f->WriteLine(headline);

				for (unsigned int index = 0; index < lineCount; index++) {
					String ^line = GetSumAbtLine(index);
					f->WriteLine(line);
				}
			}

			if (tfsum) {
				headline = GetTFSumHeadline();
				f->WriteLine(headline);

				for (unsigned int index = 0; index < lineCount; index++) {
					String ^line = GetTFSumLine(index);
					f->WriteLine(line);
				}
			}

			if (critsum) {
				headline = GetCritSumHeadline();
				f->WriteLine(headline);

				for (unsigned int index = 0; index < lineCount; index++) {
					String ^line = GetCritSumLine(index);
					f->WriteLine(line);
				}
			}
			f->Close();
		}

		String ^LKFNeoPaedContext::GetTFSumHeadline() {
			char buf[2048];
			if (lkf_npplaus_tfsum_headline((LKF_CONTEXT *)_contextPtr, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		String ^LKFNeoPaedContext::GetTFSumLine(unsigned int index) {
			char buf[2048];
			if (lkf_npplaus_tfsum((LKF_CONTEXT *)_contextPtr, index, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		String ^LKFNeoPaedContext::GetCritSumHeadline() {
			char buf[2048];
			if (lkf_npplaus_critsum_headline((LKF_CONTEXT *)_contextPtr, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}

		String ^LKFNeoPaedContext::GetCritSumLine(unsigned int index) {
			char buf[2048];
			if (lkf_npplaus_critsum((LKF_CONTEXT *)_contextPtr, index, buf, 2048))
				return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
			return nullptr;
		}
	}
}