using craftsReport.Interfaces;

namespace craftsReport.modules
{
	public class rp0450_xbs : IReportModule
	{
		public IReportHost HostObject { set; get; }

		public rp0450_xbs()
		{
		}

		// Kalkulationsliste
		public bool Execute()
		{
			HostObject.DefineVariable2("Seite", "s,2,0");
			HostObject.DefineVariable2("Zeile", "s,2,0");
			HostObject.DefineVariable2("cyZeile", "d,8,2");
			HostObject.DefineVariable2("ZlMax", "s,2,0");
			HostObject.DefineVariable2("RandX", "d,8,2");
			HostObject.DefineVariable2("RandY", "d,8,2");
			HostObject.DefineVariable2("SizeX", "d,8,2");
			HostObject.DefineVariable2("Heute", "D,10,0");
			HostObject.DefineVariable2("mSumme", "d,8,2");

			//  85 Zeilen im Abstand 8 LPI == A4-hoch
			HostObject.Set("ZlMax", @"     60 - 2", true);
			HostObject.Set("cyZeile", @" 2,54/8", true);
			HostObject.Set("RandX", @"      0,80", true);
			HostObject.Set("RandY", @"      0,60", true);
			HostObject.Set("SizeX", @"      27,5", true);
			HostObject.Set("Seite", @"      1", true);
			HostObject.Set("Zeile", @"      0", true);
			HostObject.Set("Heute", @"H", true);

			HostObject.DefineVariable2("LfdNrRoot", "u,6,0");

			HostObject.DefineVariable2("HDR08N", "C,30,0");
			HostObject.Set("HDR08N", @"Times New Roman,8,N", true);
			HostObject.DefineVariable2("HDR08F", "C,30,0");
			HostObject.Set("HDR08F", @"Times New Roman,8,F", true);
			HostObject.DefineVariable2("DET08N", "C,30,0");
			HostObject.Set("DET08N", @"Arial,8,N", true);
			HostObject.DefineVariable2("DET08F", "C,30,0");
			HostObject.Set("DET08F", @"Arial,8,F", true);


			//  Datensatz BELEGVERZEICHNIS

			HostObject.DefineRecord("recPOS", "TMPKALKLIST", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			HostObject.DefineRecord("recBVZ", "BELEGVERZEICHNIS", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			HostObject.DefineRecord("recEXT", "BELEGKOPF_ERWEITERUNG", 0, 18, HostObject.EvalString(@"$[FltKey<18]"));
			HostObject.DefineRecord("recADR", "BELEGADRESSEN", 0, 19, HostObject.EvalString(@"$[FltKey<18]0"));

			//  eigene Anschrift aus Belegvorlauf holen
			HostObject.DefineVariable2("mADRNR", "u,2,0");
			HostObject.Set("mADRNR", @"$[recBVZ.EIGANSCH]", true);
			if (HostObject.EvalBool("$[mADRNR] < 10"))
				HostObject.Set("mADRNR", @"0$[mADRNR>1]", true);
			HostObject.DefineRecord("recEIG", "EIGENE_ANSCHRIFTEN", 0, 2, HostObject.EvalString(@"$[mADRNR>2]"));

			// --------------------------------------------------
			//  Erste Seite initialisieren und PFD Angaben senden

			//  PDF Options Header
			HostObject.DefineRecord("recPDF", "OPTIONEN", 0, 5, HostObject.EvalString(@"20003"));
			HostObject.DefineVariable2("mJobHead", "C,250,0");
			HostObject.Set("mJobHead", @"@saPS2PDF-c$[recPDF.OptDataC]@ @saPS2PDF-p$[recBVZ.PRJNR]@ @saPS2PDF-a$[recBVZ.KDNR]@", true);
			HostObject.Set("mJobHead", @"@saPS2PDF-dKalkulation $[recBVZ.PRJNR]/$[recBVZ.BELEGART]/$[recBVZ.BELEGNR]@ $[mJobHead]", true);

			HostObject.WriteLine(HostObject.EvalString(@"INITPAGESIZE"));
			HostObject.WriteLine(HostObject.EvalString(@"STARTJOB $[mJobHead]"));
			HostObject.WriteLine(HostObject.EvalString(@"TRANSPARENT"));
			HostObject.WriteLine(HostObject.EvalString(@"QUERFORMAT"));

			HostObject.WriteLine(HostObject.EvalString(@"STARTPAGE"));
			HostObject.WriteLine(HostObject.EvalString(@"DEVICE A4Quer"));
			HostObject.WriteLine(HostObject.EvalString(@"CALCBORDER"));

			HostObject.Set("Zeile", @"0", true);
			HostObject.ProcessResource("outTitelZeile");
			HostObject.ProcessResource("KopfBericht");
			HostObject.ProcessResource("KopfDetail");
			while(!HostObject.TestEof("recPOS"))
			{
				SUB_loopMAIN();
				HostObject.SkipRecord("recPOS");
			}

			// --------------------------------------------------
			//  letzte Seite abschliessen
			HostObject.Set("Zeile", @"$[ZlMax]", true);
			HostObject.ProcessResource("FussBericht");
			HostObject.WriteLine(HostObject.EvalString(@"ENDPAGE"));
			HostObject.WriteLine(HostObject.EvalString(@"ENDJOB"));

			return true;
		}

		bool SUB_subTypH()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			if (HostObject.EvalBool("($[recPOS.SATZART]) = (1)"))
				return HostObject.VariablesPop();
			SUB_CheckPageTitel();
			HostObject.ProcessResource("outsubTypH");

			return HostObject.VariablesPop();
		}

		bool SUB_subTypS()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			SUB_subChkPgSumm();
			SUB_subFill();
			HostObject.ProcessResource("outsubTypS");

			return HostObject.VariablesPop();
		}

		bool SUB_subTypJ()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			SUB_CheckPage();
			SUB_subFill();
			HostObject.ProcessResource("outsubTypJ");

			return HostObject.VariablesPop();
		}

		bool SUB_subTypP()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			SUB_CheckPage();
			SUB_subFill();
			HostObject.ProcessResource("outsubTypJ");

			return HostObject.VariablesPop();
		}

		bool SUB_subTypU()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			SUB_CheckPage();
			HostObject.ProcessResource("outsubTypU");

			return HostObject.VariablesPop();
		}

		bool SUB_subTypZ()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			SUB_CheckPage();
			HostObject.ProcessResource("outsubTypZ");

			return HostObject.VariablesPop();
		}

		bool SUB_subTypX()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			SUB_CheckPage();
			HostObject.ProcessResource("outsubTypX");

			return HostObject.VariablesPop();
		}

		bool SUB_loopMAIN()
		{
			HostObject.VariablesPush();

			//  Wird fuer jeden Satz von recMAIN aufgerufen
			HostObject.DefineVariable2("mPosNr", "C,50,0");
			HostObject.DefineVariable2("mMenge", "N,14,3");
			HostObject.Set("mPosNr", @"$[recPOS.POSNR]", true);
			HostObject.Set("mMenge", @"$[recPOS.Menge] * $[recPOS.Aufwand] * $[recPOS.PARMENGE]", true);

			if (HostObject.EvalBool("$[recPOS.NEP]"))
				HostObject.Set("mPosNr", @"$[mPosNr], NEP", true);
			if (HostObject.EvalBool("$[recPOS.Fremd]"))
				HostObject.Set("mPosNr", @"$[mPosNr], Fremd", true);

			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"H")))
				SUB_subTypH();

			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"S")))
				SUB_subTypS();

			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"J")))
				SUB_subTypJ();

			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"P")))
				SUB_subTypP();

			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"U")))
				SUB_subTypU();

			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"Z")))
				SUB_subTypZ();

			if (HostObject.CompareString("recPOS.VKZ", "=", HostObject.EvalString(@"X")))
				SUB_subTypX();

			return HostObject.VariablesPop();
		}

		bool SUB_CheckPage()
		{
			HostObject.VariablesPush();

			//  Fussbereich ausgeben, wenn letzte Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] >= $[ZlMax]"))
				SUB_Fuss();
			//  Kopfbereich ausgeben, wenn erste Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] = 0"))
				SUB_Kopf();

			return HostObject.VariablesPop();
		}

		bool SUB_Fuss()
		{
			HostObject.VariablesPush();

			HostObject.Set("Zeile", @"$[ZlMax]+1", true);
			// OUT ##FussSeite
			HostObject.ProcessResource("FussBericht");
			HostObject.WriteLine(HostObject.EvalString(@"ENDPAGE"));
			//  naechste Seite anfangen
			HostObject.Set("Seite", @"$[Seite]+1", true);
			HostObject.Set("Zeile", @"0", true);
			HostObject.WriteLine(HostObject.EvalString(@"STARTPAGE"));
			HostObject.WriteLine(HostObject.EvalString(@"DEVICE A4Quer"));
			HostObject.WriteLine(HostObject.EvalString(@"CALCBORDER"));

			return HostObject.VariablesPop();
		}

		bool SUB_Kopf()
		{
			HostObject.VariablesPush();

			HostObject.Set("Zeile", @"0", true);
			HostObject.ProcessResource("outTitelZeile");
			HostObject.ProcessResource("KopfSeite");
			HostObject.ProcessResource("KopfDetail");
			if (HostObject.EvalBool("$[mSumme]=0"))
				return HostObject.VariablesPop();
			// OUT ##Uebertrag

			return HostObject.VariablesPop();
		}

		bool SUB_CheckPageTitel()
		{
			HostObject.VariablesPush();

			//  Fussbereich ausgeben, wenn letzte Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] >= ($[ZlMax]-6)"))
				SUB_Fuss();
			//  Kopfbereich ausgeben, wenn erste Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] = 0"))
				SUB_Kopf();

			return HostObject.VariablesPop();
		}

		bool SUB_subChkPgSumm()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			if (HostObject.EvalBool("$[Zeile] >= ($[ZlMax]-3)"))
				SUB_Fuss();
			//  Kopfbereich ausgeben, wenn erste Zeile erreicht
			if (HostObject.EvalBool("$[Zeile] = 0"))
				SUB_Kopf();
			else
				HostObject.ProcessResource("outLINE");

			return HostObject.VariablesPop();
		}

		bool SUB_subFill()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain
			HostObject.DefineVariable2("mFillStart", "d,8,2");
			HostObject.DefineVariable2("mFillCY", "d,8,2");
			HostObject.Set("mFillStart", @" $[RandY] + ($[cyZeile] * $[Zeile])", true);
			HostObject.Set("mFillCY", @"$[cyZeile]", true);

			HostObject.WriteLine(HostObject.EvalString(@"RGBFILL $[RandX];$[mFillStart];$[SizeX];$[mFillCY];14211288"));

			return HostObject.VariablesPop();
		}
	}
}
