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

namespace Mobiles
{
    class MobilesPackets : ClientMessage
    {
        public Boolean handle(string packet, TcpClient client)
        {
            try
            {
                int length = System.Convert.ToInt32(packet.Substring(0, 4));
                packet = packet.Substring(4);

                string header = packet.Split(' ')[0];
                Boolean outcome = true;

                switch (header)
                {
                    //Client login
                    case "LOGIN":
                        string username = Security.Cleaner.cleanString(packet.Split(' ')[1]);
                        string password = Security.Cleaner.cleanString(packet.Split(' ')[2]);
                        Handler.login(username, password, client, false);
                        break;

                    //Update user
                    case "UPDATE":
                        Handler.updateUser(packet, client);
                        break;

                    //Info retreive
                    case "INFORETRIEVE":
                        string info_username = Security.Cleaner.cleanString(packet.Split(' ')[1]);
                        string info_password = Security.Cleaner.cleanString(packet.Split(' ')[2]);
                        Handler.login(info_username, info_password, client, true);
                        break;

                    case "DISCOSTART":
                        client.sendData(RoomManager.buildDiscoPackets());
                        break;

                    //Register
                    case "REGISTER":
                        Handler.registerUser(packet, client);
                        break;

                    //Move user
                    case "Move":
                        RoomManager.moveUser(client, Convert.ToInt32(packet.Split(' ')[1]), Convert.ToInt32(packet.Split(' ')[2]));
                        break;

                    //Drop drink
                    case "DROPDRINK":
                        client.userObject.removeFromStatus("carryd");
                        client.userObject.sendStatus();
                        break;

                    //Party time!
                    case "Dance":
                        client.userObject.removeFromStatus("dance");
                        client.userObject.addToStatus("dance");
                        client.userObject.dancing = true;
                        client.userObject.sendStatus();
                        break;

                    //Stop action
                    case "STOP":
                        client.userObject.stopAction(packet.Substring(5));
                        client.userObject.sendStatus();
                        break;

                    //Get oot!
                    case "GOAWAY":
                        client.Disconnect();
                        break;

                    //Look to
                    case "LOOKTO":
                        client.userObject.lookTo(Convert.ToInt32(packet.Split(' ')[1]), Convert.ToInt32(packet.Split(' ')[1]));
                        break;

                    //Chat
                    case "CHAT":
                        ChatHandler.chat(client, packet.Substring(5));
                        break;

                    //Shout
                    case "SHOUT":
                        ChatHandler.shout(client, packet.Substring(6));
                        break;

                    //Whisper
                    case "WHISPER":
                        ChatHandler.whisper(client, packet.Substring(8));
                        break;

                    default:
                        outcome = false;
                        break;
                }

                return outcome;
            }
            catch(Exception e)
            {
                ParticleFramework.Log.Error("Packet handler: " + e.ToString());
                return false;
            }
        }
    }
}
