#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 "LKFStatContext.h"

namespace lkfxdok {
	namespace c {

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

		unsigned int LKFStatContext::GetStatCount(unsigned int type) {
			return lkf_stat_getLineCount((LKF_CONTEXT *)_contextPtr, type);
		}

		String ^LKFStatContext::GetStatField(unsigned int type, unsigned int index, int field)
		{
			char line[256];
			lkf_stat_getField((LKF_CONTEXT *)_contextPtr, type, index, field, line, 256);
			return gcnew String(line);
		}

		String ^LKFStatContext::GetStatLineName(unsigned int type, unsigned int index) 
		{
			char line[256];
			lkf_stat_getField((LKF_CONTEXT *)_contextPtr, type, index, -1, line, 256);
			return gcnew String(line);
		}

		void LKFStatContext::WriteFile(String^ outputFileName) {
			StreamWriter^ f = gcnew StreamWriter(outputFileName);
			WriteFile(f);
			f->Close();
		}

		void LKFStatContext::WriteFile(StreamWriter^ f) {
			char line[2048];
			for (unsigned int nType = 0; nType < 4; nType++) {
				unsigned int lc = lkf_stat_getLineCount((LKF_CONTEXT *)_contextPtr, nType);
				lkf_stat_getHeadline((LKF_CONTEXT *)_contextPtr, nType, line, 2048);
				f->WriteLine(gcnew String(line, 0, (int)strlen(line), System::Text::Encoding::UTF8));
				for (unsigned int i = 0; i < lc; i++) {
					lkf_stat_getLine((LKF_CONTEXT *)_contextPtr, nType, i, line, 2048);
					f->WriteLine(gcnew String(line, 0, (int)strlen(line), System::Text::Encoding::UTF8));
				}
			}

		}

		int LKFStatContext::GetAmbCounter(LKFStatAmbCount ambIndex) {
			return lkf_get_amb_counter((LKF_CONTEXT *)_contextPtr, (unsigned int) (int) ambIndex);
		}

		int LKFStatContext::GetAmbKostenstelleCount() {
			return lkf_get_amb_kostenstelle_count((LKF_CONTEXT *)_contextPtr);
		}

		String ^LKFStatContext::GetAmbKostenstelleStringValue(unsigned int index, LKFStatAmbCount ambIndex) {
			char buf[64];
			lkf_get_amb_kostenstelle_stringvalue((LKF_CONTEXT *)_contextPtr, index, (unsigned int)(int)ambIndex, buf, 64);
			return gcnew String(buf, 0, (int)strlen(buf), System::Text::Encoding::UTF8);
		}

		int LKFStatContext::GetAmbKostenstelleValue(unsigned int index, LKFStatAmbCount ambIndex) {
			return lkf_get_amb_kostenstelle_value((LKF_CONTEXT *)_contextPtr, index, (unsigned int)(int)ambIndex);
		}

		int LKFStatContext::GetAmbKostenstelleCounter(String ^kostenstelle, LKFStatAmbCount ambIndex) {
			char* abtFC = (char*)Marshal::StringToHGlobalAnsi(kostenstelle).ToPointer();
			int result = lkf_get_amb_kostenstelle_counter((LKF_CONTEXT *)_contextPtr, abtFC, (unsigned int)(int)ambIndex);
			Marshal::FreeHGlobal((IntPtr)abtFC);
			return result;
		}

		int LKFStatContext::GetAmbKostenstelleIndex(String ^kostenstelle) {
			char* abtFC = (char*)Marshal::StringToHGlobalAnsi(kostenstelle).ToPointer();
			int index = lkf_get_amb_kostenstelle_index((LKF_CONTEXT *)_contextPtr, abtFC);
			Marshal::FreeHGlobal((IntPtr)abtFC);
			return index;
		}


//		_SCORING_EXPORT unsigned int lkf_get_amb_kostenstelle_count(LKF_CONTEXT *pContext); /* V2020.1 */
//		_SCORING_EXPORT int lkf_get_amb_kostenstelle_value(LKF_CONTEXT *pContext, unsigned int index, unsigned int ambIndex); /* V2020.1 */
//		_SCORING_EXPORT void lkf_get_amb_kostenstelle_stringvalue(LKF_CONTEXT *pContext, unsigned int index, unsigned int ambIndex, char *buffer, unsigned int buflen); /* V2020.1 */
//		_SCORING_EXPORT int lkf_get_amb_kostenstelle_counter(LKF_CONTEXT *pContext, const char *kostenstelle, unsigned int ambIndex); /* V2020.1 */


	}
}