#pragma once

#include "NetUtils.h"
#include "NetFrag.h"

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

namespace ehs
{
	class NetServerCh;

	class NetEnd
	{
	private:
		friend class NetServerCh;

		NetServerCh* owner;
		UInt_64 id;
		Str_8 name;
		NetStatus status;
		Char_8 token[64];
		UInt_64 nextSendId;
		Vector<Insurance> sent;
		UInt_64 nextRecvId;
		Vector<NetFrag> received;
		Endpoint endpoint;
		float deltaDuration;
		float deltaRate;
		float timeout;
		float lastPing;
        float oldLatency;
		float latency;
		UInt_64 queueSlot;

	public:
		NetEnd();

		NetEnd(Str_8 id, Endpoint endpoint);

		NetEnd(Endpoint endpoint);

		NetEnd(NetEnd &&end) noexcept;

		NetEnd(const NetEnd &end);

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

		NetEnd &operator=(const NetEnd &end);

		UInt_64 GetId() const;

		Str_8 GetName() const;

		NetStatus GetStatus() 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 encId, bool ensure, UInt_64 sysId, UInt_64 opId, 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 &encName, bool ensure, const Str_8& sysName, const Str_8& opName, const Serializer<UInt_64>& payload);

		UInt_64 GetNextRecvId() const;

		Endpoint GetEndpoint() 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(NetStatus newStatus);

		void RemoveInsurance(UInt_64 msgId, UInt_64 fragment);

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

		Vector<NetFrag>* GetReceived();

		void Ping(float delta);

		void SetLatency(float newLatency);

		void SetQueueSlot(UInt_64 slot);

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

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

		bool SortingNeeded() const;

		void SortReceived();
	};
}