#ifndef _DebugFile_h_
#define _DebugFile_h_

#include <fstream>
#include <iostream>

void WriteDebugInformationInput(PointToPoint_Input_Parameters& input){
	try{
		std::fstream debugFile;
		debugFile.open(input.Debug_file, std::ios::in | std::ios::out);
		if (!debugFile.is_open()) return;


		debugFile << "\n                       R e s u l t s\n\n";
		debugFile << "  Mode of calculation            : " << input.C_Mode << " (";
		switch (input.C_Mode) {
			case 99: debugFile << "Point to point like line calc.)\n"; break;
			case 12: debugFile << "p2p non strict HCM t%=1.)\n"; break;
			case 11: debugFile << "p2p non strict HCM t%=50.)\n"; break;
			case 10: debugFile << "p2p non strict HCM t%=10.)\n"; break;
			case 0: debugFile << "Normal Agreement)\n"; break;
			case -1: debugFile << "Agreement line calc. h2=10m)\n"; break;
			case -9: debugFile << "p2l non strict HCM t%=10 h2=3.)\n"; break;
			case -10: debugFile << "p2l non strict HCM t%=10 h2=10.)\n"; break;
			case -11: debugFile << "p2l non strict HCM t%=50 h2=3.)\n"; break;
		}
		debugFile << "\n                     Input data :\n\n";


		debugFile << " Geographical co-ordinates of Tx: " << input.Tx_Longitude << " " << input.Tx_Latitude << "\n";
		debugFile << " Geographical co-ordinates of Tx: " << CoorToString(input.Tx_Longitude,input.Tx_Latitude).substr(0, 8);
		debugFile << " " << CoorToString(input.Tx_Longitude,input.Tx_Latitude).substr(8, 7) << "\n";

		if (input.C_Mode >= 0) {
			debugFile << " Geographical co-ordinates of Rx: " << input.Rx_Longitude << " " << input.Rx_Latitude << "\n";
			debugFile << " Geographical co-ordinates of Rx: " << CoorToString(input.Rx_Longitude,input.Rx_Latitude).substr(0, 8);
			debugFile << " " << CoorToString(input.Rx_Longitude,input.Rx_Latitude).substr(8, 7) << "\n";
		}
		debugFile << " Height above sea level of Tx   : " << input.Tx_H_Site_Ant << " m\n";
		if (input.C_Mode >= 0) {
			debugFile << " Height above sea level of Rx   : " << input.Rx_H_Site_Ant << " m\n";
		}
		debugFile << " Type of antenna horiz. of Tx   : " << input.Tx_Ant_typ_H << "\n";
		debugFile << " Type of antenna verti. of Tx   : " << input.Tx_Ant_typ_V << "\n";
		debugFile << " Azimuth of max. radiation of Tx: " << input.Tx_Azi << " degr.\n";
		debugFile << " Elevation of max. radia. of Tx : " << input.Tx_Elev << " degr.\n";
		debugFile << " Antenna height of Tx           : " << input.Tx_H_Ant << " m\n";
		if (input.C_Mode >= 0) {
			debugFile << " Antenna height of Rx           : " << input.Rx_H_Ant << " m\n";
		}
		input.Tx_Type_of_Ant == 0 ? debugFile << " Type of antenna Tx             : E\n" : debugFile << " Type of antenna Tx             : I\n";

		debugFile << " Maximum radiated power         : " << input.Tx_Power << " dBW\n";
		debugFile << " Transmitting frequency         : " << input.Tx_Freq << "MHz\n";
		debugFile << " Designation of emission Tx     : " << input.Tx_Desig_of_emis << "\n";
		debugFile << " Channel occupation             : " << input.Chan_occup << "\n";
		debugFile << " Cold or warm sea               : " << input.Sea_temperature << "\n";
		debugFile << " Radius of the service area Tx  : " << input.Tx_Serv_area << " km\n";
		debugFile << " Country code of Tx station     : " << input.Land_from << "\n";
		if (input.C_Mode >= 0) {
			debugFile << " Reception frequency            : " << input.Rx_Freq << "MHz\n";
			debugFile << " Designation of emission Rx     : " << input.Rx_Desig_of_emis << "\n";
			debugFile << " Type of antenna horiz. of Rx   : " << input.Tx_Ant_typ_H << "\n";
			debugFile << " Type of antenna verti. of Rx   : " << input.Tx_Ant_typ_V << "\n";
			debugFile << " Azimuth of max. radiation of Rx: " << input.Tx_Azi << " degr.\n";
			debugFile << " Elevation of max. radia. of Rx : " << input.Tx_Elev << " degr.\n";
			input.Rx_Type_of_Ant == 0 ? debugFile << " Type of antenna Rx             : E\n" : debugFile << " Type of antenna Rx             : I\n";
			debugFile << " Gain of Rx-antenna             : " << input.Rx_Ant_gain << " dB\n";
			debugFile << " Depolarization loss            : " << input.Depol_loss << " dB\n";
			debugFile << " Country code of Rx station     : " << input.Land_to << "\n";
			debugFile << " Input value of corr. f.delta f.: " << input.Cor_fact_frequ_diff << " dB\n";
			debugFile << " Radius of the service area Rx  : " << input.Rx_Serv_area << " km\n";
		} else {
			debugFile << " Country (code) to calc. to     : " << input.Land_to << "\n";
			debugFile << " Input value of dist. to border : " << input.D_to_border << " km\n";
			debugFile << " Input value of max. crossb. r. :  " << input.Max_CBR_D_input << " km\n";
		}
		debugFile << " Input value of permissible fs. : " << input.Perm_FS_input << " dBuV/m\n\n";






		debugFile.close();
	}catch(...){
	}
}


void WriteDebugInformationOutput(PointToPoint_Input_Parameters& input,
						PointToPoint_Output_Parameters& output,
						HcmTerrain& terra){
	try{
		std::fstream debugFile;
		debugFile.open(input.Debug_file, std::ios::app);
		if (!debugFile.is_open()) return;

		debugFile << "\n";
		debugFile << "\n";
		debugFile << "\n Version of the HCM module      : " << output.Version << "\n\n";
		debugFile << "               Important output :\n\n";
		debugFile << "  Error value                    : " << output.HCM_error << "\n";
		if (output.HCM_error != 0) {
			switch (output.HCM_error) {
				case 999: debugFile << " Error in Point_heigt or Point_type subroutine (database).\n"; break;
				case 1000: debugFile << " Distance is 0.0 km. Calculations not possible !\n"; break;
				case 1001: case 1002: case 1003: case 1004:
				case 1005: case 1006: case 1007: case 1008:
					debugFile << " Error in geographical Tx co-ordinates !\n"; break;
				case 1009: debugFile << " Error in input data of Tx antenna height !\n"; break;
				case 1010: debugFile << " Error in transmitting frequency value !\n"; break;
				case 1011: debugFile << " Error in transmitting frequency unit !\n"; break;
				case 1012: debugFile << " Error in input data of service area Tx!\n"; break;
				case 1013: debugFile << " Error in input data of height of transmitter !\n"; break;
				case 1014: case 1015: case 1016: case 1017:
				case 1018: case 1019: case 1020: case 1021:
					debugFile << " Error in geographical Rx co-ordinates !\n"; break;
				case 1022: debugFile << " Error in input data of Rx antenna height !\n"; break;
				case 1023: debugFile << " Error in reception frequency value !\n"; break;
				case 1024: debugFile << " Error in reception frequency unit !\n"; break;
				case 1025: debugFile << " Mode of calculation is not available !\n"; break;
				case 1026: debugFile << " Error in input data of permissible field strength !\n"; break;
				case 1027: debugFile << " Error in input value of maximum crossborder range!\n"; break;
				case 1028: debugFile << " Distance greater than 1000 km. Calculations not possible !\n"; break;
				case 1029: debugFile << " Error in input data of service area Rx!\n"; break;
				case 1030: debugFile << " Error in input data of height of receiver !\n"; break;
				case 1031: debugFile << " Error in input data of Tx elevation !\n"; break;
				case 1032: debugFile << " Error in input-data of Tx azimuth !\n"; break;
				case 1033: debugFile << " Error in input data of Tx antenna type (E or I) !\n"; break;
				case 1034: debugFile << " Error in input data of maximal radiated power !\n"; break;
				case 1035: debugFile << " Error in input value of distance over sea !\n"; break;
				case 1036: debugFile << " xxx.ALL borderline file for Tx is missing!\n"; break;
				case 1037: debugFile << " xxx.ALL borderline file for Rx is missing!\n"; break;
				case 1038: debugFile << " Error in input data of antenna type (TR25-08), (Tx or Rx) !\n"; break;
				case 1039: debugFile << " Error in input data of correction factor according frequency difference !\n"; break;
				case 1040: debugFile << " Channel spacing outside definition range (Rx) !\n"; break;
				case 1041: debugFile << " Channel spacing outside definition range (Tx) !\n"; break;
				case 1042: debugFile << " Error in input data of elevation (Rx) !\n"; break;
				case 1043: debugFile << " Error in input data of azimuth (Rx) !\n"; break;
				case 1044: debugFile << " Error in type of antenna Rx (E/I)!\n"; break;
				case 1045: debugFile << " Error in input data of gain of Rx antenna !\n"; break;
				case 1046: debugFile << " Error in input data of depolarization loss !\n"; break;
				case 1047: debugFile << " Error in input value of borderline distance  (max. 999 km)!\n"; break;
				case 1048: debugFile << " Selected (border-) line data not available !\n"; break;
				case 1049: debugFile << " Error in line data !\n"; break;
				case 1050: debugFile << " No Agreement frequency or C_Mode and important input is missing !\n"; break;
				case 2000: debugFile << " Error in looking up FS-figures !\n"; break;
			}
		} else {
			debugFile << " Tx co-ordinates calculated     : " << output.Tx_Longitude_Calc << "  " << output.Tx_Latitude_Calc << "\n";
			debugFile << " Tx co-ordinates calculated     : " << CoorToString(output.Tx_Longitude_Calc,output.Tx_Latitude_Calc).substr(0, 8);
			debugFile << " " << CoorToString(output.Tx_Longitude_Calc,output.Tx_Latitude_Calc).substr(8, 7) << "\n";

			
			if (input.C_Mode < 0) {
				if (input.D_to_border == 0) {
					debugFile << " The maximum field strength at\n";
					debugFile << "  the border line                : " << output.Calculated_FS << " dBuV/m\n";
				}
				if (input.D_to_border > 0) {
					debugFile << " The maximum field strength at\n";
					debugFile << "  the " << input.D_to_border << " km line                : " << output.Calculated_FS << " dBuV/m\n";
				}
				if (input.D_to_border < 0) {
					debugFile << " The maximum field strength at\n";
					debugFile << "  the cross border range         : " << output.Calculated_FS << " dBuV/m\n";
				}
				debugFile << " The co-ordinates of the (border-) line point are       : " << output.Rx_Longitude_Calc << "  " << output.Rx_Latitude_Calc << "\n";
				debugFile << " The co-ordinates of the (border-) line point are       : " << CoorToString(output.Rx_Longitude_Calc,output.Rx_Latitude_Calc).substr(0, 8);
				debugFile << " " << CoorToString(output.Rx_Longitude_Calc,output.Rx_Latitude_Calc).substr(8, 7) << "\n";
				
				debugFile << " The distance to the (border-) line point is            : " << output.Distance << " km.\n";
				debugFile << " Direction to the (border-)\n";
				debugFile << "  line point                     : " << output.Dir_Tx_Rx << " degr.\n";
				debugFile << "  Permissible field strength     : " << output.Perm_FS << " dBuV/m.\n";
				debugFile << "  The protection margin is       : " << output.Prot_margin << " dB.\n";
				debugFile << "  Calculation is performed on the " << input.D_to_border << " km-line.\n";
				debugFile << "  E.R.P of reference transmitter : " << output.ERP_ref_Tx << " dBW\n";
			} else {
				debugFile << " Rx co-ordinates calculated     : " << output.Rx_Longitude_Calc << "  " << output.Rx_Latitude_Calc << "\n";
				debugFile << " Rx co-ordinates calculated     : " << CoorToString(output.Rx_Longitude_Calc,output.Rx_Latitude_Calc).substr(0, 8);
				debugFile << " " << CoorToString(output.Rx_Longitude_Calc,output.Rx_Latitude_Calc).substr(8, 7) << "\n";
				debugFile << "  Calculated field strength      : " << output.Calculated_FS << " dBuV/m\n";
				debugFile << "  Permissible field strength     : " << output.Perm_FS << " dBuV/m\n";
				debugFile << "  Protection margin              : " << output.Prot_margin << " dB\n";
				debugFile << "  Distance Tx -> Rx              : " << output.Distance << " km\n";
				debugFile << "  Azimut Tx -> Rx              : " << output.Dir_Tx_Rx << "\n";
			}
			debugFile << "  Distance over sea              : " << output.D_sea_calculated << " km\n\n";
			debugFile << "                    Information :\n\n";

			if (output.Info[1]) {
				debugFile << " No height of Tx is given or Tx is mobile !  The height is taken from terrain database !\n\n";
			}
			if (output.Info[2]) {
				debugFile << " Height of transmitter differs from terrain database !\n\n";
			}
			if (output.Info[3]) {
				debugFile << " The transmitter height differs more than 10% from the terrain database! Calculations may be (extremely) wrong !\n\n";
			}
			if (output.Info[4]) {
				debugFile << " No HCM TX frequency or non strict C_Mode!  Permissible field strength, max CBR and  ERP of the ref. TX are taken from input!\n\n";
			}
			if (output.Info[5]) {
				debugFile << " Input value of permissible field strength is used !\n\n";
			}
			if (output.Info[6]) {
				debugFile << " Input value for the maximum crossborder range is used !\n\n";
			}
			if (output.Info[7]) {
				debugFile << " The field stength is set to 999.9, because of service area overlapping.\n\n";
			}
			if (output.Info[8]) {
				debugFile << " No height of Rx is given or Rx is mobile / line! The height is taken from terrain database !\n\n";
			}
			if (output.Info[9]) {
				debugFile << " Height of receiver differs from terrain database !\n\n";
			}
			if (output.Info[10]) {
				debugFile << " The receiver height differs more than 10% from the terrain database! Calculations may be (extremely) wrong !\n\n";
			}
			if (output.Info[11]) {
				debugFile << " Free space field strength is used because distance is less than 1 km !\n\n";
			}
			if (output.Info[12]) {
				debugFile << " Free space field strength is used because first fresnel zone is free !\n\n";
			}
			if (output.Info[13]) {
				debugFile << " Distance over sea is greater than total distance; distance between  Tx and Rx is used !\n\n";
			}
			if (output.Info[14]) {
				debugFile << " Input value of corr. factor accord. frequency difference is used !\n\n";
			}
			if (output.Info[15]) {
				debugFile << " Frequency difference outside definition range ! 82 dB value is used for correction factor !\n\n";
			}
			if (output.Info[16]) {
				debugFile << " Calculated distance over sea is set to 0 because of missing morphological data !\n\n";
			}
			if (output.Info[17]) {
				debugFile << " Channel spacing outside curve range, broadband formula is used!\n";
			}
			if (output.Info[18]) {
				debugFile << " Correction factors for the band 380 - 400 MHz are used.\n";
			}
			debugFile << "\n\n";
			debugFile << "              Additional output :\n\n";
			debugFile << "  Transmitter clearance angle    : " << output.Tx_TCA << " degree\n";
			debugFile << "  Tx clearance angle corr. factor: " << output.Tx_TCA_corr << " dB\n";
			debugFile << "  Effective antenna height of Tx : " << output.Heff_Tx << " m\n";
			debugFile << "  Effective antenna height of Rx : " << output.Heff_Rx << " m\n";
			debugFile << "  Effective antenna height (CCIR): " << output.Heff << " m\n";
			debugFile << "  Delta height  (CCIR)           : " << output.Dh << " m\n";
			debugFile << "  Correct. factor delta h (CCIR) : " << output.Dh_corr << " dB\n";
			debugFile << "  Values from the " << "Time_percentage" << "% curves\n";
			debugFile << "   Land field strength           : " << output.Land_FS << " dBuV/m\n";
			debugFile << "   Land field strength (OLD)     : " << output.Land_FS_old << " dBuV/m\n";
			
			debugFile << "   Sea field strength (temp=" << input.Sea_temperature << ")   : " << output.Sea_FS << " dBuV/m\n";
			debugFile << "   Correction factor Tx antenna   : " << output.Tx_ant_corr << " dB\n";
			debugFile << "   Corr. factor antenna type Tx   : " << output.Tx_ant_type_corr << " dB\n";
			debugFile << "   Direction Tx -> Rx             : " << output.Dir_Tx_Rx << " degree\n";
			debugFile << "   Angle vertical Tx to Rx        : " << output.V_angle_Tx_Rx << " degree\n";
			debugFile << "   Diff.angle hori. (Tx->Rx - Azi): " << output.H_diff_angle_Tx_Rx << " degree\n";
			debugFile << "   Diff.angle vert. (Tx->Rx - Ele): " << output.V_diff_angle_Tx_Rx << " degree\n";
			if (input.C_Mode >= 0) {
				debugFile << "   Receiver clearance angle       : " << output.Rx_TCA << " degree\n";
				debugFile << "   Rx clearance angle corr. factor: " << output.Rx_TCA_corr << " dB\n";
				debugFile << "   Direction Rx -> Tx             : " << output.Dir_Rx_Tx << " degree\n";
				debugFile << "   Angle vertical Rx to Tx        : " << output.V_angle_Rx_Tx << " degree\n";
				debugFile << "   Diff.angle hori.(Rx->Tx - AziR): " << output.H_diff_angle_Rx_Tx << " degree\n";
				debugFile << "   Diff.angle vert.(Rx->Tx - EleR): " << output.V_diff_angle_Rx_Tx << " degree\n";
				debugFile << "   Correction factor Rx antenna   : " << output.Rx_ant_corr << " dB\n";
				debugFile << "   Corr. factor antenna type Rx   : " << output.Rx_ant_type_corr << " dB\n";
				debugFile << "   Delta frequency                : " << output.Delta_frequency / 1000.0 << " kHz\n";
				debugFile << "   Corr. factor according delta f : " << output.Corr_delta_f << " dB\n";
				debugFile << "   Depolarization loss            : " << input.Depol_loss << " dB\n";
			}
			
			
			if (input.useAnnex1){
				if (output.AnnexInFreqRange)
					debugFile << "   !!! Annex1. Flag to use Annex1 Enabled. Tx Frequency exists in table. PermFS used from table\n";
				else 
					debugFile << "   !!! Annex1. Flag to use Annex1 Enabled. Tx Frequency NOT exists in table. PermFS used from Input\n";
					
			}
			debugFile << "   Permissible field str. of table\n";
			debugFile << "   (0, if there is an input)      : " << output.Perm_FS_from_table << " dBuV/m\n";
			debugFile << "   Power in direction of Rx       : " << (output.Power_to_Rx - output.Tx_ant_corr) << " dBW\n";
			debugFile << "   Free space field strength      : " << output.Free_space_FS << " dBuV/m\n\n\n\n";

				debugFile << "\n Number of profile points  " << output.ProfilePoints << "\n";
				debugFile << " profile sampling distance " << input.PointDistance << " km\n\n";
				try{
					if (output.HCM_error == 0 && terra.T_Prof.size() > 0){
						for (int i = 0; i < terra.T_Prof.size(); i++) {
							debugFile << i << "\t" << terra.T_Prof[i] << "\n" ;
						}
					}
				} catch(...){

				};

		}
		debugFile.close();
	}catch(...){
	}
}
#endif
