using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace MapleShark
{
	public sealed class FiestaStream {
        // Fields
		private byte[] _mBuffer = new byte[0x1000];
        private int _mCursor;
        public bool MOutbound;
        public int XorPos;
        public DateTime Created;

		private static readonly byte[] XorTable =
		{
			0x07, 0x59, 0x69, 0x4A, 0x94, 0x11, 0x94, 0x85, 0x8C, 0x88, 0x05, 0xCB,
			0xA0, 0x9E, 0xCD, 0x58, 0x3A, 0x36, 0x5B, 0x1A, 0x6A, 0x16, 0xFE, 0xBD,
			0xDF, 0x94, 0x02, 0xF8, 0x21, 0x96, 0xC8, 0xE9, 0x9E, 0xF7, 0xBF, 0xBD,
			0xCF, 0xCD, 0xB2, 0x7A, 0x00, 0x9F, 0x40, 0x22, 0xFC, 0x11, 0xF9, 0x0C,
			0x2E, 0x12, 0xFB, 0xA7, 0x74, 0x0A, 0x7D, 0x78, 0x40, 0x1E, 0x2C, 0xA0,
			0x2D, 0x06, 0xCB, 0xA8, 0xB9, 0x7E, 0xEF, 0xDE, 0x49, 0xEA, 0x4E, 0x13,
			0x16, 0x16, 0x80, 0xF4, 0x3D, 0xC2, 0x9A, 0xD4, 0x86, 0xD7, 0x94, 0x24,
			0x17, 0xF4, 0xD6, 0x65, 0xBD, 0x3F, 0xDB, 0xE4, 0xE1, 0x0F, 0x50, 0xF6,
			0xEC, 0x7A, 0x9A, 0x0C, 0x27, 0x3D, 0x24, 0x66, 0xD3, 0x22, 0x68, 0x9C,
			0x9A, 0x52, 0x0B, 0xE0, 0xF9, 0xA5, 0x0B, 0x25, 0xDA, 0x80, 0x49, 0x0D,
			0xFD, 0x3E, 0x77, 0xD1, 0x56, 0xA8, 0xB7, 0xF4, 0x0F, 0x9B, 0xE8, 0x0F,
			0x52, 0x47, 0xF5, 0x6F, 0x83, 0x20, 0x22, 0xDB, 0x0F, 0x0B, 0xB1, 0x43,
			0x85, 0xC1, 0xCB, 0xA4, 0x0B, 0x02, 0x19, 0xDF, 0xF0, 0x8B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, 0x81, 0x7F, 0xE3,
			0xC3, 0x04, 0x01, 0x22, 0xEC, 0x27, 0xCC, 0xFA, 0x3E, 0x21, 0xA6, 0x54,
			0xC8, 0xDE, 0x00, 0xB6, 0xDF, 0x27, 0x9F, 0xF6, 0x25, 0x34, 0x07, 0x85,
			0xBF, 0xA7, 0xA5, 0xA5, 0xE0, 0x83, 0x0C, 0x3D, 0x5D, 0x20, 0x40, 0xAF,
			0x60, 0xA3, 0x64, 0x56, 0xF3, 0x05, 0xC4, 0x1C, 0x7D, 0x37, 0x98, 0xC3,
			0xE8, 0x5A, 0x6E, 0x58, 0x85, 0xA4, 0x9A, 0x6B, 0x6A, 0xF4, 0xA3, 0x7B,
			0x61, 0x9B, 0x09, 0x40, 0x1E, 0x60, 0x4B, 0x32, 0xD9, 0x51, 0xA4, 0xFE,
			0xF9, 0x5D, 0x4E, 0x4A, 0xFB, 0x4A, 0xD4, 0x7C, 0x33, 0x02, 0x33, 0xD5,
			0x9D, 0xCE, 0x5B, 0xAA, 0x5A, 0x7C, 0xD8, 0xF8, 0x05, 0xFA, 0x1F, 0x2B,
			0x8C, 0x72, 0x57, 0x50, 0xAE, 0x6C, 0x19, 0x89, 0xCA, 0x01, 0xFC, 0xFC,
			0x29, 0x9B, 0x61, 0x12, 0x68, 0x63, 0x65, 0x46, 0x26, 0xC4, 0x5B, 0x50,
			0xAA, 0x2B, 0xBE, 0xEF, 0x9A, 0x79, 0x02, 0x23, 0x75, 0x2C, 0x20, 0x13,
			0xFD, 0xD9, 0x5A, 0x76, 0x23, 0xF1, 0x0B, 0xB5, 0xB8, 0x59, 0xF9, 0x9F,
			0x7A, 0xE6, 0x06, 0xE9, 0xA5, 0x3A, 0xB4, 0x50, 0xBF, 0x16, 0x58, 0x98,
			0xB3, 0x9A, 0x6E, 0x36, 0xEE, 0x8D, 0xEB
		};


		private static int[] ClientTable { get; set; }
		private static int[] ServerTable { get; set; }

		// Methods
		public FiestaStream(short xorPos, bool outbound) {
            XorPos = xorPos;
            MOutbound = outbound;
            Created = DateTime.Now;

			ClientTable = XorTable.Select(x => (int)x).ToArray();
			ServerTable = XorTable.Select(x => (int)x).ToArray();
		}

        public void Append(byte[] pBuffer) {
            Append(pBuffer, 0, pBuffer.Length);
        }

        public void Append(byte[] pBuffer, int pStart, int pLength) {
            if( _mBuffer.Length - _mCursor < pLength ) {
                var newSize = _mBuffer.Length * 2;
                while( newSize < _mCursor + pLength ) {
                    newSize *= 2;
                }
                Array.Resize(ref _mBuffer, newSize);
            }
            Buffer.BlockCopy(pBuffer, pStart, _mBuffer, _mCursor, pLength);
            _mCursor += pLength;
        }

	    private readonly Mutex _decryptor = new Mutex();
        private void Decrypt(IList<byte> pBuffer, bool outbound) {
            try {
                _decryptor.WaitOne();

	            if (outbound)
	            {
		            var indx = 0;
		            while (indx < 499)
		            {
			            ClientTable[indx] = 3 * XorTable[indx];
			            indx++;
		            }

		            for (var i = 0; i < pBuffer.Count; i++)
		            {
			            pBuffer[i] = (byte)(pBuffer[i] ^ ClientTable[XorPos]);
			            XorPos++;

			            if (XorPos >= 499)
			            {
				            XorPos = 0;
			            }
		            }
				}
	            else
	            {
		            for (var i = 0; i < pBuffer.Count; ++i)
		            {
			            pBuffer[i] = (byte)(pBuffer[i] ^ ServerTable[XorPos]);
			            XorPos++;
			            if (XorPos == 499)
			            {
				            XorPos = 0;
			            }
		            }
				}
            } finally {
                _decryptor.ReleaseMutex();
            }
        }

        public FiestaPacket Read(DateTime pTransmitted) {
            if( _mCursor < 1 ) {
                return null;
            }
            short count = _mBuffer[0];
            var bigPacket = false;
            if( count == 0 ) {
                if( _mCursor < 2 ) return null;
                count = BitConverter.ToInt16(_mBuffer, 1);
                bigPacket = true;
            }
            if( bigPacket && _mCursor < count + 3 ) return null; //00 short
            if( !bigPacket && _mCursor < count + 1 ) return null; //byte
            var dst = new byte[count];
            Buffer.BlockCopy(_mBuffer, !bigPacket ? 1 : 3, dst, 0, count);
            Decrypt(dst, MOutbound);
            _mCursor -= !bigPacket ? count + 1 : count + 3;
            if( _mCursor > 0 ) {
                Buffer.BlockCopy(_mBuffer, !bigPacket ? count + 1 : count + 3, _mBuffer, 0, _mCursor);
            }
            var opcode = (ushort)(dst[0] | (dst[1] << 8));
            Buffer.BlockCopy(dst, 2, dst, 0, count - 2);
            Array.Resize(ref dst, count - 2);
            var definition = Config.Instance.Definitions.Find(d =>
	            d.Outbound == MOutbound && d.Opcode == opcode);

            return new FiestaPacket(pTransmitted, MOutbound, opcode, definition == null ? "" : definition.Name, dst, Created);
        }
    }
}
