#pragma once

#include "NetUtils.h"
#include "NetFrags.h"

#include "ehs/Str.h"
#include "ehs/Vector.h"
#include "ehs/Serializer.h"
#include "ehs/io/socket/Socket.h"

namespace ehs
{
	class EHC;

	class NetEnd
	{
	private:
		friend class EHC;

		EHC* owner;
		EndDisp disposition;
		UInt_64 hashName;
		Str_8 name;
		Status status;
		Architecture arch;
		Char_8 token[64];
		UInt_64 nextSendId;
		Vector<Insurance> sent;
		UInt_64 nextRecvId;
		Vector<NetFrags> received;
		AddrType type;
		Str_8 address;
		UInt_16 port;
		float deltaDuration;
		float deltaRate;
		float timeout;
		float lastPing;
        float oldLatency;
		float latency;
		UInt_64 queueSlot;

	public:
		NetEnd();

		NetEnd(EndDisp disposition, Str_8 id, Architecture arch, AddrType type, Str_8 address, UInt_16 port);

		NetEnd(AddrType type, Str_8 address, UInt_16 port);

		NetEnd(NetEnd &&end) noexcept;

		NetEnd(const NetEnd &end);

		NetEnd &operator=(NetEnd &&end) noexcept;

		NetEnd &operator=(const NetEnd &end);

		EndDisp GetDisposition() const;

		UInt_64 GetHashName() const;

		Str_8 GetName() const;

		Status GetStatus() const;

		Architecture GetArchitecture() const;

		UInt_64 GetNextSendId() const;

		/// Sends data to the remote endpoint.
		/// @param [in] deltaLocked Whether or not to match the remote endpoint's delta time to prevent overloading the client. This will drop data if delta time does not match.
		/// @param [in] encHashId The hash id of the encryption to use. Can be zero for none.
		/// @param [in] ensure Whether or not to ensure the data was received by the remote endpoint.
		/// @param [in] sys The system hash id to execute an operation from.
		/// @param [in] op The operation hash id in the system to execute.
		/// @param [in] payload Additional parameters and data to send to the remote endpoint.
		void Send(bool deltaLocked, UInt_64 encHashId, bool ensure, UInt_64 sys, UInt_64 op, const Serializer<UInt_64>& payload);

		/// Sends data to the remote endpoint.
		/// @param [in] deltaLocked Whether or not to match the remote endpoint's delta time to prevent overloading the client. This will drop data if delta time does not match.
		/// @param [in] encId The id of the encryption to use. Can be empty for none.
		/// @param [in] ensure Whether or not to ensure the data was received by the remote endpoint.
		/// @param [in] sys The system string id to execute an operation from.
		/// @param [in] op The operation string id in the system to execute.
		/// @param [in] payload Additional parameters and data to send to the remote endpoint.
		void Send(bool deltaLocked, const Str_8 &encID, bool ensure, const Str_8& sys, const Str_8& op, const Serializer<UInt_64>& payload);

		UInt_64 GetNextRecvId() const;

		Str_8 GetAddress() const;

		UInt_16 GetPort() const;

		void SetDeltaRate(float newDeltaRate);

		float GetDeltaRate() const;

		float GetTimeout() const;

		float GetLastPing() const;

		void SendLatency();

		float GetLatency() const;

		UInt_64 GetQueueSlot() const;

	private:
		void Poll(float delta);

		void SetStatus(Status newStatus);

		void RemoveInsurance(UInt_64 msgId, UInt_64 fragment);

		void AddReceived(const Header& header, const Serializer<>& payload);

		Vector<NetFrags>* GetReceived();

		void Ping(float delta);

		void Pong(float delta);

		void SetLatency(float newLatency);

		void SetQueueSlot(UInt_64 slot);

		NetFrags FragmentData(const Header& header, const Serializer<>& data);

		void Send(const Header& header, const Serializer<>& payload);

		bool SortingNeeded() const;

		void SortReceived();
	};
}