using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FiestaBot.Instances.Networking;
using FiestaBot.Utilities;
using FiestaBot.Zone.ClientPacket;
using FiestaBot.Zone.Game;
using FiestaBot.Zone.Networking;

namespace FiestaBot.Zone.Handlers
{
    internal class Zone08Handler
    {
        [ZonePacketHandler(8, 2)]
        public static void NormalChat(ZoneClient Socket, Packet Packet)
        {
            Packet.ReadUShort(out ushort ObjectID);
            Packet.ReadByte(out byte Length);
            Packet.ReadByte(out byte Color);
            Packet.ReadString(out string Message, Length);

            string Name = "";

            if (Socket.ObjectList.Where(x => x.ObjectID == ObjectID).FirstOrDefault() == null) { Name = Socket.Manager.Character.Name; }
            else { Name = Socket.ObjectList.Find(x => x.ObjectID == ObjectID).Name; }

            Logger.Log(LogType.ChatNormal, "[{0}]: {1}", Name, Message);

            Chat.HandleCommand(Socket, ObjectID, Message);

            Packet.Dispose();
        }

        [ZonePacketHandler(8, 17)]
        public static void MapNotice(ZoneClient Socket, Packet Packet)
        {
            Packet.ReadByte(out byte Length);
            Packet.ReadString(out string Message, Length);
            Logger.Log(LogType.Notice, Message);
        }

        [ZonePacketHandler(8, 24)]
        public static void ObjectWalk(ZoneClient Socket, Packet Packet)
        {
            Packet.ReadUShort(out ushort ObjectID);
            Packet.ReadInt(out int OldY);
            Packet.ReadInt(out int OldX);
            Packet.ReadInt(out int NewX);
            Packet.ReadInt(out int NewY);
            Packet.ReadUInt(out uint Speed);

            if (Socket.ObjectList.Where(x => x.ObjectID == ObjectID).FirstOrDefault() != null)
            {
                Socket.ObjectList.Find(x => x.ObjectID == ObjectID).Position.X = NewX;
                Socket.ObjectList.Find(x => x.ObjectID == ObjectID).Position.Y = NewY;
            }

            if (Socket.FollowMode && Socket.FollowUser == ObjectID)
            {
                ZonePacket.SendRun(Socket, NewX, NewY);
            }

            Packet.Dispose();
        }

        [ZonePacketHandler(8, 26)]
        public static void ObjectRun(ZoneClient Socket, Packet Packet)
        {
            Packet.ReadUShort(out ushort ObjectID);
            Packet.ReadInt(out int OldY);
            Packet.ReadInt(out int OldX);
            Packet.ReadInt(out int NewX);
            Packet.ReadInt(out int NewY);
            Packet.ReadUInt(out uint Speed);

            if (Socket.ObjectList.Where(x => x.ObjectID == ObjectID).FirstOrDefault() != null)
            {
                Socket.ObjectList.Find(x => x.ObjectID == ObjectID).Position.X = NewX;
                Socket.ObjectList.Find(x => x.ObjectID == ObjectID).Position.Y = NewY;
            }

            if (Socket.FollowMode && Socket.FollowUser == ObjectID)
            {
                ZonePacket.SendRun(Socket, NewX, NewY);
            }

            Packet.Dispose();
        }

        [ZonePacketHandler(8, 62)]
        public static void Handle862(ZoneClient Socket, Packet Packet)
        {
            //TODO: Find out what this does
            Packet.Dispose();
        }
    }
}
