using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParticleFramework.Communication;
using ParticleFramework.Storage;
using ParticleFramework;
using Mobiles.Rooms;
using MySql.Data.MySqlClient;

namespace Mobiles.Users
{
    static class ChatHandler
    {

        public static void chat(TcpClient client, string msg)
        {
            msg = msg.Replace("#", "");

            if (!parseCommands(client, msg)) return;

            double unixTime = ConvertToUnixTimestamp(DateTime.UtcNow);
            string dateTime = String.Format("{0:dd, MMMM, yyyy}", DateTime.UtcNow);
            client.userObject.addToStatus("talk");
            client.userObject.sendStatus();
            client.userObject.removeFromStatus("talk");
            client.userObject.sendStatus();

            client.userObject.toRoom("CHAT" + (char)13 + client.userObject.username + " " + msg);
            botParse(msg, client);
        }

        public static void shout(TcpClient client, string msg)
        {
            msg.Replace("#", "");

            if (!parseCommands(client, msg)) return;

            double unixTime = ConvertToUnixTimestamp(DateTime.UtcNow);
            string dateTime = String.Format("{0:dd, MMMM, yyyy}", DateTime.UtcNow);
            client.userObject.addToStatus("talk");
            client.userObject.sendStatus();
            client.userObject.removeFromStatus("talk");
            client.userObject.sendStatus();

            client.userObject.toRoom("SHOUT" + (char)13 + client.userObject.username + " " + msg);
            botParse(msg, client);
        }

        public static void whisper(TcpClient client, string msg)
        {
            msg.Replace("#", "");
            double unixTime = ConvertToUnixTimestamp(DateTime.UtcNow);
            string dateTime = String.Format("{0:dd, MMMM, yyyy}", DateTime.UtcNow);

            Handler.toUser(msg.Split(' ')[1], "WHISPER" + (char)13 + client.userObject.username + " " + msg.Substring(msg.Split(' ')[0].Length+1));
        }

        private static void botParse(string msg, TcpClient client)
        {
            //Let bots do some shit
            foreach (Bot roomBot in RoomManager.roomBots)
            {
                if (roomBot.userObject.roomID == client.userObject.roomID)
                {
                    roomBot.parseCommands(msg, client);
                }
            }
        }

        private static bool parseCommands(TcpClient client, string msg)
        {
            //parse commands
            if (client.userObject.clientRank > 2)
            {
                if (msg.Substring(0, 1) == ":")
                {
                    switch (msg.Substring(1).Split(' ')[0])
                    {
                        case "discoball":
                            client.userObject.toRoom("DISCOBALL " + msg.Split(' ')[1]);
                            RoomManager.discoBallStatus = msg.Split(' ')[1];
                            break;

                        case "spotlight":
                            client.userObject.toRoom("SPOTTY" + msg.Split(' ')[1] + " " + msg.Substring(11 + msg.Split(' ')[1].Length + 1));
                            if (msg.Split(' ')[1] == "1")
                            {
                                if (msg.Split(' ')[2] == "follow") RoomManager.spotLight1Follow = msg.Split(' ')[3];
                                if (msg.Split(' ')[2] == "move") RoomManager.spotLight1Moveto = msg.Split(' ')[3];
                                if (msg.Split(' ')[2] == "on") RoomManager.spotLight1Status = "on";
                                if (msg.Split(' ')[2] == "off") RoomManager.spotLight1Status = "off";
                            }
                            else
                            {
                                if (msg.Split(' ')[2] == "follow") RoomManager.spotLight2Follow = msg.Split(' ')[3];
                                if (msg.Split(' ')[2] == "move") RoomManager.spotLight2Moveto = msg.Split(' ')[3];
                                if (msg.Split(' ')[2] == "on") RoomManager.spotLight2Status = "on";
                                if (msg.Split(' ')[2] == "off") RoomManager.spotLight2Status = "off";
                            }
                            break;

                        case "roomcol":
                            RoomManager.roomCol = msg.Split(' ')[1];
                            client.userObject.toRoom("ROOMCOL " + msg.Split(' ')[1]);
                            break;

                        case "discotest":
                            client.userObject.toRoom("DISCOTEST");
                            break;

                        case "drink":
                            client.userObject.addToStatus("carryd " + msg.Substring(6));
                            client.userObject.sendStatus();
                            break;

                        case "lookto":
                            client.userObject.lookTo(Convert.ToInt32(msg.Split(' ')[1]), Convert.ToInt32(msg.Split(' ')[2]));
                            break;

                        case "alert":
                            if (client.userObject.clientRank >= 4)
                            {
                                Handler.toAll("ERROR " + msg.Substring(6));
                            }
                            else
                            {
                                client.sendData("ERROR " + "You cannot do that!");
                            }
                            break;

                        case "alertuser":
                            string user = msg.Split(' ')[1];
                            Handler.toUser("ERROR " + msg.Substring(11 + user.Length), user);
                            break;

                        case "alertroom":
                            client.userObject.toRoom("ERROR " + msg.Substring(11));
                            break;

                        case "kickroom":
                            client.userObject.toRoom("ERROR You have been kicked from the Mobiles client!", true);
                            break;

                        case "kickall":
                            if (client.userObject.clientRank >= 4)
                            {
                                Handler.toAll("ERROR You have been kicked from the pMobiles client!", true);
                            }
                            else
                            {
                                client.sendData("ERROR You cannot do that!");
                            }
                            break;

                        case "goto":
                            RoomManager.toRoom(System.Convert.ToInt32(msg.Split(' ')[1]), client);
                            break;

                        case "kick":
                            string kickUser = msg.Split(' ')[1];
                            Handler.toUser("ERROR You have been kicked from the pMobiles client!", kickUser, true);
                            break;

                        case "warp":
                            client.userObject.x = Convert.ToInt32(msg.Split(' ')[1]);
                            client.userObject.y = Convert.ToInt32(msg.Split(' ')[2]);
                            client.userObject.z = 0;
                            client.userObject.resetStatus();
                            client.userObject.toRoom("STATUS " + client.userObject.ToStatus());
                            break;
                    }

                    return false;
                }
            }
            return true;
        }

        public static DateTime ConvertFromUnixTimestamp(double timestamp)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(timestamp);
        }


        public static double ConvertToUnixTimestamp(DateTime date)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan diff = date - origin;
            return Math.Floor(diff.TotalSeconds);
        }
    }
}
