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

namespace Mobiles.Rooms
{
    class RoomManager
    {
        public static System.Collections.Hashtable roomMaps = new System.Collections.Hashtable();
        public static System.Collections.Hashtable mPathFinder = new System.Collections.Hashtable();
        public static int[,] seatMap;
        public static List<string[]> roomData;
        public static List<int[,]> seatMaps;
        public static List<Bot> roomBots = new List<Bot>();
        public static Thread roomStatusThread;

        //Disco room vars
        public static string spotLight1Follow = "", spotLight2Follow = "";
        public static string spotLight1Moveto = "0", spotLight2Moveto = "0";
        public static string spotLight1Status = "off", spotLight2Status = "off";
        public static string discoBallStatus = "on", roomCol = "";

        public static void toRoom(int roomID, TcpClient client)
        {
            Log.Info("Client " + client.index + "  " + roomID);
            if (client.userObject.roomID != roomID)
            {
                Log.Info("Client " + client.index + " going to public room " + roomID);

                if (DBManager.database.getString("SELECT COUNT(*) FROM `rooms` WHERE `id` = '" + roomID + "';") != "0")
                {
                    DBManager.database.closeClient();

                    //kick plz
                    if (client.userObject.roomID > 0)
                    {
                        client.userObject.leaveRoom();
                    }

                    client.sendData("HEIGHTMAP " + roomMaps[roomID + "_text"].ToString());
                    client.sendData("OBJECTS " + Convert.ToInt32(RoomManager.roomData[roomID][2]) + (char)13 + RoomManager.roomData[roomID][6]);

                    client.userObject.toRoom(roomID, Convert.ToInt32(RoomManager.roomData[roomID][3]), Convert.ToInt32(RoomManager.roomData[roomID][4]), Convert.ToInt32(RoomManager.roomData[roomID][5]));

                    RoomManager.sendUserList(client);
                }
                else
                {
                    DBManager.database.closeClient();
                    client.sendData("ER You have an invalid SSO ticket. Please re-login and then reload.");
                }
            }
        }

        public static void setupThreads()
        {
            roomStatusThread = new Thread(new ThreadStart(roomStatusUpdate));
            roomStatusThread.Start();
        }

        public static string buildDiscoPackets()
        {
            string packet_spotty1 = "SPOTTY1 " + spotLight1Status;

            if (spotLight1Follow != "")
            {
                packet_spotty1 += "###SPOTTY1 follow " + spotLight1Follow;
            }else if (spotLight1Moveto != "0")
            {
                packet_spotty1 += "###SPOTTY1 " + spotLight1Moveto;
            }

            string packet_spotty2 = "###SPOTTY2 " + spotLight2Status;

            if (spotLight1Follow != "")
            {
                packet_spotty2 += "###SPOTTY2 follow " + spotLight1Follow;
            }
            else if (spotLight2Moveto != "0")
            {
                packet_spotty2 += "###SPOTTY2 " + spotLight2Moveto;
            }

            if (roomCol != "") packet_spotty2 += "###ROOMCOL " + roomCol;

            return packet_spotty1 + packet_spotty2 + "###DISCOBALL " + discoBallStatus;
        }

        public static void moveUser(TcpClient client, int _x, int _y)
        {
            client.userObject.moveTo(_x, _y);
        }

        private static void roomStatusUpdate()
        {
            while (true)
            {
                foreach (Mobiles.Users.UserObject user in Mobiles.Users.Handler.clientObjects)
                {
                    user.moveSelfOnThread();
                }
                Thread.Sleep(500);
            }
        }

        public static void sendUserList(TcpClient client)
        {
            string userList = "USERS" + (char)13;
            string statusList = "STATUS ";

            foreach (Mobiles.Users.UserObject user in Mobiles.Users.Handler.clientObjects)
            {
                if (user.roomID == client.userObject.roomID)
                {
                    if (user.username != client.userObject.username)
                    {
                        userList += user.ToString() + (char)13;
                        statusList += user.ToStatus() + (char)13;
                    }
                }
            }

            client.sendData(userList/* + "###" + statusList*/);
        }

        public static void parseHeightmap(int roomID, string heightmap)
        {

            string[] axes = heightmap.Split((char)10);
            int maxX = axes[0].Length - 1;
            int maxY = axes.Length - 1;

            string[,] map = new string[maxX + 1, maxY + 1];
            bool[,] mapUsers = new bool[maxX + 1, maxY + 1];
            byte[,] bytemap = new byte[maxX + 2, maxY + 2];
            seatMap = new int[maxX + 2, maxY + 2];

            for (int j = 0; j <= maxX; j++)
            {
                for (int y = 1; y <= maxY; y++)
                {
                    bytemap[j, y] = 1;
                    seatMap[j, y] = -1;
                }
            }

            for (int j = 0; j < maxX; j++)
            {
                for (int y = 1; y < maxY; y++)
                {
                    string str = axes[y].Substring(j, 1);

                    if (str.ToLower() == "x")
                    {
                        bytemap[j + 1, y - 1] = 0;
                    }
                    else
                    {
                        map[j + 1, y] = str;
                    }
                }
            }

            seatMaps.Add(seatMap);

            roomMaps.Add(roomID, map);
            roomMaps.Add(roomID + "_byte", bytemap);
            roomMaps.Add(roomID + "_text", heightmap);
            mPathFinder.Add(roomID, new PathFinder(bytemap));
        }

    }
}
