using craftsReport.Interfaces;

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

		public rp0451_xbs()
		{
		}

		// Zeitvorgabeliste
		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);

			if (HostObject.IsVar("mFaktor"))
				goto EndFaktor;
			HostObject.DefineVariable2("mFaktor", "N,10,2");
			HostObject.Set("mFaktor", @"1", true);
			EndFaktor:
			//  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-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_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
			HostObject.DefineVariable2("cZeit", "C,12,0");
			HostObject.DefineVariable2("mZeit", "N,14,2");
			HostObject.DefineVariable2("mMinuten", "N,14,2");

			SUB_subFmtZeit();
			SUB_subChkPgSumm();
			// SUB #subFill
			HostObject.ProcessResource("outsubTypS");

			return HostObject.VariablesPop();
		}

		bool SUB_subTypJ()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			HostObject.DefineVariable2("cZeit", "C,12,0");
			HostObject.DefineVariable2("mZeit", "N,14,2");
			HostObject.DefineVariable2("mMinuten", "N,14,2");

			SUB_subFmtZeit();
			SUB_CheckPage();
			// SUB #subFill
			HostObject.ProcessResource("outsubTypJ");

			return HostObject.VariablesPop();
		}

		bool SUB_subTypP()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			HostObject.DefineVariable2("cZeit", "C,12,0");
			HostObject.DefineVariable2("mZeit", "N,14,2");
			HostObject.DefineVariable2("mMinuten", "N,14,2");

			SUB_subFmtZeit();
			SUB_CheckPage();
			// SUB #subFill
			HostObject.ProcessResource("outsubTypJ");

			return HostObject.VariablesPop();
		}

		bool SUB_subFmtZeit()
		{
			HostObject.VariablesPush();

			//  Formatieren der Zeit

			HostObject.Set("mMinuten", @"$[recPOS.MINUTEN] * $[recPOS.AUFWAND] * $[mFaktor]", true);
			HostObject.Set("mZeit", @"$[recPOS.ZEIT]", true);
			// cZeit=$[recPOS.STDMIN]

			if (HostObject.EvalBool("$[mZeit] = 0"))
				return HostObject.VariablesPop();

			HostObject.DefineVariable2("nTmp1", "N,8,0");
			HostObject.DefineVariable2("nTmp2", "N,8,0");
			HostObject.DefineVariable2("cStd", "C,8,0");
			HostObject.DefineVariable2("cMin", "C,2,0");
			HostObject.DefineVariable2("bNeg", "L,1,0");

			HostObject.Set("bNeg", @"0", true);

			HostObject.Set("mZeit", @"$[mZeit] * $[mFaktor]", true);
			if (HostObject.EvalBool("$[mZeit] >= 0"))
				goto EndNeg;
			HostObject.Set("bNeg", @"1", true);
			HostObject.Set("mZeit", @"0-$[mZeit]", true);
			EndNeg:

			HostObject.Set("nTmp1", @"$[mZeit] / 60", true);
			HostObject.Set("nTmp2", @"$[nTmp1] * 60", true);
			if (HostObject.EvalBool("$[nTmp2] > $[mZeit]"))
				HostObject.Set("nTmp1", @"$[nTmp1] - 1", true);
			HostObject.Set("nTmp2", @"$[nTmp1] * 60", true);
			HostObject.Set("nTmp2", @"$[mZeit] - $[nTmp2]", true);
			HostObject.Set("cStd", @"$[nTmp1]", true);
			HostObject.Set("cMin", @"$[nTmp2]", true);
			if (!HostObject.EvalBool("$[nTmp2] >= 10"))
				HostObject.Set("cMin", @"0$[cMin]", true);
			HostObject.Set("cZeit", @"$[cStd]:$[cMin] ", true);
			if (HostObject.EvalBool("$[bNeg]"))
				HostObject.Set("cZeit", @"-$[cZeit]", true);

			if (HostObject.EvalBool("$[recPOS.NEP] + $[recPOS.Entfaellt] + $[recPOS.Bedarf]"))
				HostObject.Set("cZeit", @"($[cZeit])", true);

			return HostObject.VariablesPop();
		}

		bool SUB_subTypU()
		{
			HostObject.VariablesPush();

			//  Unterprogramm von ReportMain/loopMAIN
			HostObject.DefineVariable2("cZeit", "C,12,0");
			HostObject.DefineVariable2("mZeit", "N,14,2");
			HostObject.DefineVariable2("mMinuten", "N,14,2");

			SUB_subFmtZeit();
			SUB_CheckPage();
			// SUB #subFill
			HostObject.ProcessResource("outsubTypU");

			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.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();

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