using craftsReport.Interfaces;

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

		public rp0454_xbs()
		{
		}

		// Materialauszug
		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", @"     86 - 2", true);
			HostObject.Set("cyZeile", @" 2,54/8", true);
			HostObject.Set("RandX", @"      1,80", true);
			HostObject.Set("RandY", @"      0,80", true);
			HostObject.Set("SizeX", @"      17,50", 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", "BELEGPOSITIONEN", 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-dZeitvorgabe $[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(@"STARTPAGE"));
			HostObject.WriteLine(HostObject.EvalString(@"DEVICE $[mDevice1]"));
			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_loopARTNR()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			//  Datensatzschleife recBP

			HostObject.DefineVariable2("mGesamtMenge", "N,14,3");
			HostObject.Set("mGesamtMenge", @" $[recBP.Menge] * $[recBP.Aufwand]", true);

			HostObject.DefineRecord("recParent", "BELEGPOSITIONEN", 3, 24, HostObject.EvalString(@"$[FltKey<18]$[recBP.ParentRef<6]"));
			if (HostObject.EvalBool("$[recParent.SatzArt] = 6"))
				HostObject.Set("mGesamtMenge", @" $[mGesamtMenge] * ($[recParent.Menge] * $[recParent.Aufwand])", true);
			if (HostObject.EvalBool("$[recParent.SatzArt] = 7"))
				HostObject.Set("mGesamtMenge", @" $[mGesamtMenge] * ($[recParent.Menge] * $[recParent.Aufwand])", true);

			HostObject.DefineRecord("recParent2", "BELEGPOSITIONEN", 3, 24, HostObject.EvalString(@"$[FltKey>18]$[recParent.ParentRef<6]"));
			if (HostObject.EvalBool("$[recParent2.SatzArt] = 6"))
				HostObject.Set("mGesamtMenge", @" $[mGesamtMenge] * ($[recParent2.Menge] * $[recParent2.Aufwand])", true);

			HostObject.Set("mMenge", @" $[mMenge] + $[mGesamtMenge]", true);

			return HostObject.VariablesPop();
		}

		bool SUB_loopMAIN()
		{
			HostObject.VariablesPush();

			//  Wird fuer jeden Satz von recMAIN aufgerufen

			HostObject.DefineVariable2("mArtNr", "C,20,0");
			HostObject.DefineVariable2("mMenge", "N,14,3");
			HostObject.DefineVariable2("mEinheit", "C,6,0");
			HostObject.DefineVariable2("mText", "C,50,0");
			HostObject.DefineVariable2("mAnzahl", "N,10,0");
			HostObject.DefineVariable2("mGebinde", "N,10,0");
			HostObject.DefineVariable2("mRest", "N,10,2");

			HostObject.DefineRecord("recBP", "BELEGPOSITIONEN", 2, 38, HostObject.EvalString(@"$[FltKey<18]$[recPOS.ArtNr<20]"));
			HostObject.DefineRecord("recPE", "PREISEINHEITEN", 0, 2, HostObject.EvalString(@"$[recPOS.Pe<2]"));

			if (!HostObject.CompareString("recPOS.RefKey", "=", HostObject.EvalString(@"$[recBP.RefKey]")))
				return HostObject.VariablesPop();

			if (HostObject.EvalBool("($[recPOS.SatzArt]) = (7)"))
				goto Weiter;
			if (HostObject.EvalBool("($[recPOS.SatzArt]) = (8)"))
				goto Weiter;

			return HostObject.VariablesPop();

			Weiter:

			HostObject.Set("mArtNr", @" $[recPOS.ArtNr]", true);
			HostObject.Set("mEinheit", @" $[recPOS.Me]", true);
			HostObject.Set("mText", @" $[recPOS.Ktxt1]", true);
			HostObject.Set("mGebinde", @" $[recPOS.Gebinde] * $[recPE.Faktor]", true);
			HostObject.Set("mAnzahl", @" 0", true);
			HostObject.Set("mMenge", @" 0", true);

			while(!HostObject.TestEof("recBP"))
			{
				SUB_loopARTNR();
				HostObject.SkipRecord("recBP");
			}

			if (HostObject.EvalBool("$[mGebinde] = 0"))
				goto EndAnzahl;
			HostObject.Set("mAnzahl", @" $[mMenge] / $[mGebinde]", true);
			HostObject.Set("mRest", @" $[mMenge] / $[mGebinde]", true);
			HostObject.Set("mRest", @" $[mRest] - $[mAnzahl]", true);
			if (HostObject.EvalBool("$[mRest] <= 0"))
				goto EndAnzahl;
			if (HostObject.EvalBool("$[mRest] >= 0,5"))
				goto EndAnzahl;
			HostObject.Set("mAnzahl", @" $[mAnzahl] + 1", true);
			EndAnzahl:
			SUB_CheckPage();
			HostObject.ProcessResource("outMAIN");

			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 $[mDevice2]"));
			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_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();
		}
		*/
	}
}
