package net.cobem.grizzly.communication.events.messenger;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventRequest;
import net.cobem.grizzly.communication.GameEvent;
import net.cobem.grizzly.communication.composers.messenger.SendInstantMessageComposer;
import net.cobem.grizzly.communication.composers.misc.SendNotificationComposer;
import net.cobem.grizzly.habbohotel.rooms.Room;
import net.cobem.grizzly.habbohotel.rooms.robots.Robot;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.utils.UserInputFilter;

import java.util.Random;

public class SendInstantMessageEvent implements GameEvent {
    @Override
    public void compose(Session Session, EventRequest Request) {
        int Reciever = Request.readInt();

        String Message = UserInputFilter.filterString(Request.readString(), true);

        if (Reciever == new Integer(Grizzly.getConfig().get("habbo.messenger.staffbot.id"))) //is_staff_bot
        {
            if (Message.split(" ").length >= 2) // [0] => command, [1] => param1, [2] => param2
            {
                String Command = Message.split(" ")[0];

                boolean FoundCommand = true;
                boolean InvalidParameters = false;

                switch (Command) {
                    case "alert":

                        break;

                    case "roomalert":
                    case "ra":
                        if (Message.split(" ").length < 3) {
                            InvalidParameters = true;
                        } else {
                            if (Integer.parseInt(Message.split(" ")[1]) != 0) {
                                Room mRoom = Grizzly.getHabboHotel().getRooms().getByID(Integer.parseInt(Message.split(" ")[1]));

                                mRoom.sendMessage(SendNotificationComposer.compose(Message.replace(Message.split(" ")[0], "").replace(Message.split(" ")[1], "")));

                                Session.sendResponse(SendInstantMessageComposer.compose(new Integer(Grizzly.getConfig().get("habbo.messenger.staffbot.id")), "Sending " + Message.replace(Message.split(" ")[0], "").replace(Message.split(" ")[1], "") + " to the room " + Message.split(" ")[1] + "!"));
                            }
                        }
                        break;

                    case "hotelalert":
                    case "ha":

                        if (Message.split(" ").length < 2) {
                            InvalidParameters = true;
                        } else {
                            String Str = "";

                            if (Message.split(" ").length > 2 && !Message.split(" ")[2].startsWith("www.")) {
                                Grizzly.getHabboHotel().sendHotelAlert(Message.replace(Message.split(" ")[0], ""));
                                Session.sendResponse(SendInstantMessageComposer.compose(new Integer(Grizzly.getConfig().get("habbo.messenger.staffbot.id")), "Sending " + Message.replace(Message.split(" ")[0], "") + " to the hotel!"));
                            } else {
                                Grizzly.getHabboHotel().sendHotelAlert(Message.split(" ")[1]);
                                Session.sendResponse(SendInstantMessageComposer.compose(new Integer(Grizzly.getConfig().get("habbo.messenger.staffbot.id")), "Sending " + Message.split(" ")[1] + " to the hotel!"));
                            }

                        }
                        break;

                    case "follow":
                        if (Message.split(" ").length < 2) {
                            InvalidParameters = true;
                        } else {
                            if (Message.split(" ")[1].equals("me")) {
                                Robot Bot = new Robot(Session,
                                        (new Random().nextInt(9999 - 9000) + 9000),
                                        Grizzly.getConfig().get("habbo.messenger.staffbot.name"),
                                        Grizzly.getConfig().get("habbo.messenger.staffbot.motto"),
                                        Grizzly.getConfig().get("habbo.messenger.staffbot.look"), Session.getActor().CurrentRoom);

                                Bot.X = Session.getActor().CurrentRoom.getModel().DoorX;
                                Bot.Y = Session.getActor().CurrentRoom.getModel().DoorY;
                                Bot.Rotation = Session.getActor().CurrentRoom.getModel().DoorRot;

                                Session.getActor().CurrentRoom.generateRobot(Bot);
                            } else {
                                if (Integer.parseInt(Message.split(" ")[1]) != 0) {
                                    int UserID = new Integer(Message.split(" ")[1]);

                                    if (Grizzly.getHabboHotel().getSessions().getByID(UserID) != null) {
                                        Robot Bot = new Robot(Session,
                                                (new Random().nextInt(9999 - 9000) + 9000),
                                                Grizzly.getConfig().get("habbo.messenger.staffbot.name"),
                                                Grizzly.getConfig().get("habbo.messenger.staffbot.motto"),
                                                Grizzly.getConfig().get("habbo.messenger.staffbot.look"), Grizzly.getHabboHotel().getSessions().getByID(UserID).getActor().CurrentRoom);

                                        Bot.X = Grizzly.getHabboHotel().getSessions().getByID(UserID).getActor().CurrentRoom.getModel().DoorX;
                                        Bot.Y = Grizzly.getHabboHotel().getSessions().getByID(UserID).getActor().CurrentRoom.getModel().DoorY;
                                        Bot.Rotation = Grizzly.getHabboHotel().getSessions().getByID(UserID).getActor().CurrentRoom.getModel().DoorRot;

                                        Grizzly.getHabboHotel().getSessions().getByID(UserID).getActor().CurrentRoom.generateRobot(Bot);
                                    }
                                } else {
                                    String Username = Message.split(" ")[1];

                                    if (Grizzly.getHabboHotel().getSessions().getByName(Username) != null) {
                                        Robot Bot = new Robot(Session,
                                                (new Random().nextInt(9999 - 9000) + 9000),
                                                Grizzly.getConfig().get("habbo.messenger.staffbot.name"),
                                                Grizzly.getConfig().get("habbo.messenger.staffbot.motto"),
                                                Grizzly.getConfig().get("habbo.messenger.staffbot.look"), Grizzly.getHabboHotel().getSessions().getByName(Username).getActor().CurrentRoom);

                                        Bot.X = Grizzly.getHabboHotel().getSessions().getByName(Username).getActor().CurrentRoom.getModel().DoorX;
                                        Bot.Y = Grizzly.getHabboHotel().getSessions().getByName(Username).getActor().CurrentRoom.getModel().DoorY;
                                        Bot.Rotation = Grizzly.getHabboHotel().getSessions().getByName(Username).getActor().CurrentRoom.getModel().DoorRot;

                                        Grizzly.getHabboHotel().getSessions().getByName(Username).getActor().CurrentRoom.generateRobot(Bot);

                                    }
                                }
                            }
                        }
                        break;

                    default:
                        FoundCommand = false;
                        break;
                }

                if (!FoundCommand) {
                    Session.sendResponse(SendInstantMessageComposer.compose(new Integer(Grizzly.getConfig().get("habbo.messenger.staffbot.id")), "I can't find that command in my manual.."));
                }

                if (InvalidParameters) {
                    Session.sendResponse(SendInstantMessageComposer.compose(new Integer(Grizzly.getConfig().get("habbo.messenger.staffbot.id")), "You havent given me enough parameters for that command.. It says it right here in my manual!"));
                }
            } else {
                Session.sendResponse(SendInstantMessageComposer.compose(new Integer(Grizzly.getConfig().get("habbo.messenger.staffbot.id")), "I only run commands bro."));
            }
        } else {
            if ((Grizzly.getHabboHotel().getSessions().getByID(Reciever)) != null) {
                Grizzly.getHabboHotel().getSessions().getByID(Reciever).sendResponse(SendInstantMessageComposer.compose(Session.getHabbo().getID(), Message));
            } else {
                Session.sendResponse(SendInstantMessageComposer.compose(Reciever, "Your friend is offline!"));
            }
        }
    }
}
