using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mobiles.Pathfinding;
using Mobiles.Rooms;
using ParticleFramework.Communication;

namespace Mobiles.Users
{
    class UserObject
    {
        #region Mobiles user field
            public string username, password, email, sex, mission;
            public int clientRank, age;
            public int x, y, z, roomID;
            public int destX = -1, destY = -1;
            public int pants, shirt, head, rotationHead = 1, direction = 1;
            public TcpClient tcpClient;
            public bool dancing, sitting;
            private System.Collections.Hashtable extraStatus = new System.Collections.Hashtable();
            private int statusID = 0;
            private PathFinder tPathfinder;
        #endregion

        public UserObject(string _username, string _pass, string _mission, string _email, string _sex, int _pants, int _shirt, int _head, int _clientRank, int _age, TcpClient _tcpClient)
        {
            username = _username;
            password = _pass;
            mission = _mission;
            email = _email;
            sex = _sex;
            pants = _pants;
            shirt = _shirt;
            head = _head;
            clientRank = _clientRank;
            age = _age;
            tcpClient = _tcpClient;
        }

        public void remove()
        {
            leaveRoom();
            Handler.clientObjects.Remove(this);
            tcpClient = null;
            clientRank = 0;
            username = "";
        }

        public void leaveRoom()
        {
            x = destX;
            y = destY;
            toRoom("STATUS" + (char)13 + username + " 999,999,0,1,1/");
            resetStatus();
            statusID = 0;
            roomID = 0;
            tPathfinder.setCell(x, y, 1);
        }

        public void toRoom(string Data)
        {
            foreach (UserObject user in Handler.clientObjects)
            {
                if (user.roomID == roomID && user.tcpClient != null)
                {
                    user.tcpClient.sendData(Data);
                }
            }
        }

        public void toRoom(string Data, Boolean disconnect)
        {
            foreach (UserObject user in Handler.clientObjects)
            {
                if (user.roomID == roomID && user.tcpClient != null)
                {
                    user.tcpClient.sendData(Data);
                    if (disconnect) user.tcpClient.Disconnect();
                }
            }
        }

        public void toRoom(int _rID, int startX, int startY, int startZ)
        {
            destX = x = startX;
            destY = y = startY;
            z = startZ;
            roomID = _rID;
            direction = 1;
            dancing = false;

            //Setup pathfinder
            tPathfinder = (RoomManager.mPathFinder[roomID] as PathFinder);

            toRoom("USERS" + (char)13 + ToString());
        }

        public void updatePosition(int _x, int _y, int _z, int _dir)
        {
            x = _x;
            y = _y;
            z = _z;
            direction = _dir;
        }

        public void moveTo(int _x, int _y)
        {
           //// if ((Handler.roomMaps[roomID] as string[,])[_x, _y] != "-1")
            // {
                destX = _x;
                destY = _y;
          //  }
        }

        public void moveSelfOnThread()
        {
           // while (true)
           // {
                if (tPathfinder != null)
                {
                    if (destX != x || destY != y)
                    {
                        removeFromStatus("sit");
                        sitting = false;

                        removeFromStatus("mv");

                        List<PathFinderNode> pList = tPathfinder.FindPath(new System.Drawing.Point(x, y), new System.Drawing.Point(destX, destY));
                        if (pList == null || pList.Count <= 1)
                        {
                            destX = x;
                            destY = y;
                        }
                        else
                        {
                            Tile nextTile = new Tile(pList[1].X, pList[1].Y);

                            string[,] mapArray = (RoomManager.roomMaps[roomID] as string[,]);
                            string nextZ = mapArray[nextTile.x, nextTile.y];

                            if (nextTile.x != -1 && nextTile.y != -1)
                            {
                                direction = Directions.calculateDirection(x, y, nextTile.x, nextTile.y);
                                rotationHead = direction;

                                addToStatus("mv " + nextTile.x + "," + nextTile.y + "," + nextZ);
                                sendStatus();
                                tPathfinder.setCell(x, y, 1);
                                x = nextTile.x;
                                y = nextTile.y;
                                tPathfinder.setCell(x, y, 0);

                                if (nextZ != "")
                                {
                                    z = Convert.ToInt32(nextZ);
                                }
                                else
                                {
                                    z = 0;
                                }

                                //Are we going to the disco room?
                                if (x == 16 && y == 18 && roomID == 1)
                                {
                                    RoomManager.toRoom(2, tcpClient);
                                    destX = x;
                                    destY = y;
                                    extraStatus.Clear();
                                }
                            }
                        }
                    }
                    else if (RoomManager.seatMaps[roomID] != null && RoomManager.seatMaps[roomID][x, y] > -1)
                    {
                        if (RoomManager.seatMaps[roomID][x, y] != direction && sitting)
                        {
                            rotationHead = direction = RoomManager.seatMaps[roomID][x, y];
                            removeFromStatus("sit");
                            addToStatus("sit " + direction);
                            sendStatus();
                        }
                        else
                        {
                            rotationHead = direction = RoomManager.seatMaps[roomID][x, y];

                            if (sitting == false)
                            {
                                if (dancing)
                                {
                                    removeFromStatus("dance");
                                    dancing = false;
                                }

                                //Sit down
                                addToStatus("sit " + direction);
                                sendStatus();

                                sitting = true;
                            }
                        }
                    }
                }
           // }
        }

        public void stopAction(string action)
        {
            if (action == "Dance")
            {
                dancing = false;
                removeFromStatus("dance");
            }
            else
            {
                removeFromStatus(action);
            }
        }

        public void removeFromStatus(string header)
        {
            for (int i = 0; i < statusID; i++)
            {
                if (extraStatus[i] != null)
                {
                    if (extraStatus[i].ToString().Split(' ') != null)
                    {
                        if (extraStatus[i].ToString().Substring(0, header.Length) == header)
                        {
                            extraStatus.Remove(i);
                            return;
                        }
                    }
                }
            }

        }

        public bool statusContains(string header)
        {
            for (int i = 0; i < statusID; i++)
            {
                if (extraStatus[i] != null)
                {
                    if (extraStatus[i].ToString().Split(' ') != null)
                    {
                        if (extraStatus[i].ToString().Substring(0, header.Length) == header)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;

        }

        public void sendStatus()
        {
            toRoom("STATUS " + ToStatus());
        }

        public void lookTo(int _x, int _y)
        {
            rotationHead = Directions.calculateDirection(x, y, _x, _y);
            sendStatus();
        }

        public void addToStatus(string data)
        {
            removeFromStatus(data.Split(' ')[0]);
            extraStatus.Add(statusID, data + "/");
            statusID++;
        }

        public void resetStatus()
        {
            extraStatus.Clear();
        }

        public string ToStatus()
        {
            string newStat = (char)13 + username + " " + x + "," + y + "," + z + "," + rotationHead + "," + direction + "/";
            newStat += getExtraStatus();
            return newStat;
        }

        private string getExtraStatus()
        {
            string newStat = "";
            for (int i = 0; i < statusID; i++)
            {
                if (extraStatus[i] != null)
                {
                    newStat = newStat + extraStatus[i] + "/";
                }
                else
                {
                    extraStatus.Remove(i);
                }
            }
            return newStat;
        }

        public override string ToString()
        {
            return username + " " + pants + "," + shirt + "," + head + " " + x + " " + y + " " + z + " " + mission;
        }

        public string ToPrivate()
        {
            return username + " " + password + " " + email + " " + pants + "," + shirt + "," + head + " " + "noidea x " + (sex.ToString().Equals("M") ? "Male" : "Female") + " yes " + age + " " + mission;
        }

    }
}
