#pragma once
#pragma unmanaged
#include <windows.h>
#include "EpdCommon.h"
#include "Epd3.h"
#include "Epd2.h"

#include "TimeFunctions.h"

#define MaxPeakRates 10
#define MaxSensitivities 10
#define MaxDoses 4
#define MaxCounts 6
#define MaxAlarmConfig 10
#define MaxTresholds 10


namespace MK3
{
	class Epd2U
	{
	public:
		char  comPort[5] = {"COM4"};
		CommsHandle epdComsHandle;
		uint32_t CurrentDeviceID;
		EpdGeneration EpdGen;
		DiscoveryId Epd;
		CompletionToken token;
		WORD port = 3;
		const WORD MaxDevices = 10;
		WORD DeviceCount = 0;
		DWORD DeviceIDs[10];
		//DWORD CurrentDeviceID;
		//WORD AlarmCtrl = 0b01111110;
		WORD AlarmCtrl = 0b01111010; // NO sound
		WORD EndSessionControl = 0b00010010;
		BYTE displayConfig[56] = { 0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
		DWORD EpdClock;
		DWORD RealTime;
		WORD IssueCount;
		DWORD OffTime;
		OpStatusFlags OperatingStatus;
		FaultStatusFlags ErrorStatus;
		AlarmStatusFlags AlarmStatus;
		WORD OtherAlarms;
		WORD EEPROMBadSectors;

		WORD CommsInitialized = 0;
		WORD InitRv = 0;
		WORD DiscRV = 0;
		WORD ConRV = 0;
		WORD OpenRV = 0;
		WORD ReadStatusRV = 0;
		WORD CommitRV = 0;
		WORD OffRV = 0;
		WORD OnRV = 0;
		WORD EndSessionRV = 0;
		WORD ReadConfigRV = 0;
		WORD ReadDoseTresholdRV = 0;
		WORD ReadAlarmRateThresholdsRV = 0;
		WORD ReadCalibrationDataRV = 0;
		WORD ReadWearerRV = 0;
		WORD ReadEpdIDRV = 0;
		WORD ReadDosesQualityRV;
		WORD ReadPeaksRV = 0;

		WORD ClearDoseRV;
		WORD ClearTotalDoseRV;
		WORD ClearPeakRatesRV;
		WORD DeIssueEpdRV = 0;

		WORD WriteAlarmDoseThresholdsRV = 0;
		WORD WriteAlarmRateThresholdsRV = 0;
		WORD WriteWearerNameRV = 0;

		WORD IssueEpdRV = 0;

		WORD Error = 0;

		WORD ErrorSource;
		WORD ErrorReason;
		WORD ErrorData;

		//Identification

		DWORD EpdID = 0;
		WORD HardVersion;
		WORD SoftVersion;
		WORD FunctionFlags;

		EpdTypes EpdType = EpdTypes::Mk2Gamma;
		bool isBG = false;
		SYSTEMTIME tRealTime;

		//Wearer
#define WearerIDLength  12
		BYTE WearerID[(WearerIDLength)+1];
#define WearerNameLength  22
		BYTE WearerName[WearerNameLength + 1];


		// Dose

		float Hp10Dose;
		float Hp07Dose;
		float Hp10Total;
		float Hp07Total;
		WORD Knocks;
		WORD Resets;
		WORD QualityData;

		//Peaks
		float Hp10Peak;
		float Hp07Peak;
		DWORD Hp10Time;
		DWORD Hp07Time;


		//AlarmDoseTreshold
		float Hp10THDose1;
		float Hp10THDose2;
		float Hp07THDose;

		float Hp10Rate1On;
		float Hp10Rate2On;
		float Hp07RateOn;
		float Hp10Rate1Off;
		float Hp10Rate2Off;
		float Hp07RateOff;


		float K1;
		float K2;
		float K3;
		float K4;
		float K5;
		float K6;
		uint32_t SG;
		uint32_t BC;
		uint32_t FB;
		uint32_t HG;

		DWORD	D1;
		DWORD	D2;
		DWORD	D3;
		DWORD	D4;
		DWORD  CountsClock;

		DWORD	BaseD1;
		DWORD	BaseD2;
		DWORD	BaseD3;
		DWORD	BaseD4;
		DWORD  BaseCountsClock;


		//Config
		WORD CountDownTime;
		DWORD ReturnTime;
		WORD ChirpRate;
		WORD TeleBaudSetting;
		WORD SelfTestInterval;
		WORD BatteryLoadTestInt;
		WORD GeneralControl;
		WORD CommsTimeout;
		WORD InhibitTimeout;
		WORD MinTxInterval;
		WORD MaxTxIntervals;
		WORD RandomiseWindow;
		WORD DoseIncThresh;

		//OperatingStatus
		BYTE DetectorsOn = 0;
		BYTE EPDIssued = 0;
		BYTE EPDLocked = 0;
		BYTE EPDTesting = 0;
		BYTE EPDProcSec = 0;
		BYTE EPDFaulty = 0;


		//ERrrostatus
		BYTE EPDNewFault = 0;
		BYTE EPDComFault = 0;
		BYTE EPDCalFault = 0;
		BYTE EPDDetFault = 0;
		BYTE EPDEprFault = 0;
		BYTE EPDTrhFault = 0;
		BYTE EPDOthFault = 0;


		//AlarmStatus
		BYTE AlarmDoseHP101G = 0;
		BYTE AlarmDoseHP102G = 0;
		BYTE AlarmDoseHP07N = 0;
		BYTE AlarmRateHP101G = 0;
		BYTE AlarmRateHP102G = 0;
		BYTE AlarmRateHP07N = 0;

		//OtherAlarm
		BYTE OtherAlarmRateHp101G = 0;
		BYTE OtherAlarmRateHp102G = 0;
		BYTE OtherAlarmRateHp07N = 0;
		BYTE OtherAlarmReturn = 0;
		BYTE OtherAlarmBatLow = 0;
		BYTE OtherAlarmCarrier = 0;
		BYTE OtherAlarmBatVolt = 0;



		DWORD Password = 9;


		BYTE byBatADC;
		BYTE bySupplyADC;
		BYTE byStatus3;
		BYTE byStatus4;

		MeasurementId mIDs[MAX_MEASUREMENTS];
		uint8_t nmIDs = 0;

		MeasValue_t doses[MaxDoses];
		MeasValue_t totdoses[MaxDoses];
		PeakRate_t peakRates[MaxPeakRates];
		AlarmConfig_t alarmConfig[MaxAlarmConfig];
		CalSensitivity_t sensitivities[MaxSensitivities];
		Counter_t Counts[MaxCounts];
		uint8_t countsRead;
		uint32_t seconds;
		uint8_t numDoses, numtotDoses, numRates, numSensitivities, numTresholds;
		uint16_t numAlarm;
		uint32_t utc;
		Mk3QualityData_t quality;
		Mk3AlarmThresholds_t hp07tresholds;
		Mk3RatePercentage_t RatePct[2];
		Mk3AlarmThresholds_t hp10tresholds;

		DetectorThreshold_t dTresholds[MaxTresholds];
		int StartDiscover();
		void Open(DiscoveryId DeviceID);
		void CloseDev();

		void DecodeFunctionFlags();

		void DecodeStatus();
		void decodeTreshold(Mk3AlarmThresholds_t *th);
		void encodeTreshold(Mk3AlarmThresholds_t* th);

		void decodedTresholds(int numdTresholds);

	

		Epd2U(int comport);
		WORD WriteConfig();
		DWORD StartCom();
		void EndCom();

		WORD ReadStatus();
		//WORD EPDReadExtraStatus();
		WORD EPDReadConfig();
		WORD TurnOff();
		WORD TurnOn();
		WORD WriteCal();
		WORD ReadSensitivitiesAndTresholds();
		//WORD ReadTresHolds();
		WORD ReadDoses();
		void decodetotDoses(int count);
		void decodeDoses(int count);
		void decodeSensitivities(int count);
		void decodePeakRates(int count);
		void decodeCounters(int count);

		void decodeBaseCounters(int count);

		void SetTime();
		WORD ReadAlarmConfig();
		WORD ReadAlarmTresholds();
		WORD WriteAlarmTresholds();
		WORD WriteWearer();

		void ClearDoses();
		void ClearAlarms();

		WORD PrepareForIssue();
		WORD DeIssue();
		WORD Issue();

		~Epd2U();

		StatusData_t epd3status;
		uint8_t counterIds[MaxCounts]; //max 6 for mk3
		uint8_t counterIdLen;
		
		//Voor read counters
		
		WORD EPDReadExtraStatus();
		

		//static HANDLE Ev1 ;
		//static Epd2U* current;
		//void WINAPI discovery (CommsHandle hComms, DiscoveryId const discovered[], int32_t count);
	};
}




