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.Users;
using MySql.Data.MySqlClient;

namespace Mobiles.Rooms
{
    class Bot
    {
        private Thread movementThread;
        private int moveStage = 0, talkStage = 0;
        private bool givingDrink;
        public int moveTimeout = 25000, startX, startY;
        public UserObject userObject;
        public string[] botShouts;

        public Bot(string username)
        {
            Console.WriteLine("[pMobiles] Initalizing bot: " + username);
            //Create user object
            userObject = newObject(username);

            //Give ze bot ze default valuez
            MySqlDataReader mysqlRead = DBManager.database.getCommand("SELECT * FROM `bots` WHERE `name` = '" + username + "' LIMIT 1").ExecuteReader();
            mysqlRead.Read();

            string shouts = mysqlRead["shouts"].ToString();
            botShouts = shouts.Split((char)10);

            int roomID = Convert.ToInt32(mysqlRead["room"]);
            //Add bot to client objects
            Handler.clientObjects.Add(userObject);
            startX = Convert.ToInt32(mysqlRead["startx"]);
            startY = Convert.ToInt32(mysqlRead["starty"]);
            userObject.toRoom(roomID, startX, startY, Convert.ToInt32(RoomManager.roomData[roomID][5]));

            //Setup the bot's thread
            movementThread = new Thread(new ThreadStart(moveandChat));
            movementThread.Start();

            Handler.botCount++;
        }

        private void moveandChat()
        {
            while (true)
            {
                //Move the bot
                if (!givingDrink) moveStage++;

                Pathfinding.Tile newTile = getNextTile();
                userObject.moveTo(newTile.x, newTile.y);

                if (givingDrink)
                {
                    userObject.removeFromStatus("gived");
                    userObject.sendStatus();
                    givingDrink = false;
                }

                //Talk
                if (talkStage >= botShouts.Length) talkStage = 0;
                userObject.toRoom("CHAT" + (char)13 + userObject.username + " " + botShouts[talkStage]);

                talkStage++;

                Thread.Sleep(moveTimeout);
            }
        }

        public void parseCommands(string msg, TcpClient client)
        {
            msg = msg.ToLower();
            //client.userObject.removeFromStatus("gived");
           // client.userObject.addToStatus("gived " + msg.Substring(6));

            if (msg.Contains("beer"))
            {
                userObject.addToStatus("gived Beer");
                client.userObject.addToStatus("carryd Beer");
                givingDrink = true;
            }

            if (msg.Contains("coffee"))
            {
                userObject.addToStatus("gived Coffee");
                client.userObject.addToStatus("carryd Coffee");
                givingDrink = true;
            }

            if (msg.Contains("whisky"))
            {
                userObject.addToStatus("gived Whisky");
                client.userObject.addToStatus("carryd Whisky");
                givingDrink = true;
            }

            if(msg.Split(' ').Length > 2 && msg.Split(' ')[0] == "koska" && msg.Split(' ')[2] == "viimeksi")
            {
                string username = msg.Split(' ')[1];
                client.sendData("CHAT" + (char)13 + userObject.username + " " + username + " last visited on " + DBManager.database.getString("SELECT `lastvisit` FROM `users` WHERE `name` = '" + username + "';"));
            }

            userObject.sendStatus();
            client.userObject.sendStatus();
        }

        public Pathfinding.Tile getNextTile()
        {
            int xTile;
            int yTile;

            switch (moveStage)
            {
                case 1:
                    xTile = 9;
                    yTile = 2;
                    break;

                case 2:
                    xTile = startX + 3;
                    yTile = startY;
                    break;

                default:
                    xTile = startX;
                    yTile = startY;
                    moveStage = 0;
                break;
            }

            return new Pathfinding.Tile(xTile, yTile);
        }

        public UserObject newObject(string username)
        {
            MySqlDataReader mysqlRead = DBManager.database.getCommand("SELECT * FROM `users` WHERE `name` = '" + username + "' LIMIT 1").ExecuteReader();
            mysqlRead.Read();

            return new UserObject(mysqlRead["name"].ToString(), mysqlRead["password"].ToString(), mysqlRead["mission"].ToString(), mysqlRead["email"].ToString(), mysqlRead["sex"].ToString(), Convert.ToInt32(mysqlRead["pants"]), Convert.ToInt32(mysqlRead["shirt"]), Convert.ToInt32(mysqlRead["head"]), Convert.ToInt32(mysqlRead["rank"]), Convert.ToInt32(mysqlRead["age"]), null);
        }
    }
}
