using Fiesta.Networking.Encryption;
using Fiesta.Networking.Packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Villain.Networking;
using Villain.Networking.Packets;

namespace Fiesta.Networking
{
	public class Client : Villain.Networking.Client
	{
		public IEncryption Encryption;

		public new Action<InPacket> OnRecieve;
		public new Action<Client> OnDisconnect;

		public bool UseEncryption = true;

		public Client(Socket socket) : base(socket)
		{
			Encryption = new XorEncryption();
			
			base.OnDisconnect = (client) =>
			{
				OnDisconnect?.Invoke(this);
			};
		}

		public override void Recieve(IPacket packet)
		{
			Recieve((InPacket)packet);
		}

		public void Recieve(InPacket packet)
		{
			if (UseEncryption)
			{
				packet.Decrypt(Encryption);
			}
			
			packet.ReadType();

			OnRecieve?.Invoke(packet);
		}

		public override IPacket CreatePacket(byte[] data)
		{
			return new InPacket(data);
		}
	}
}