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

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventRequest;
import net.cobem.grizzly.communication.GameEvent;
import net.cobem.grizzly.communication.composers.inventory.DisposeItemFromInventoryComposer;
import net.cobem.grizzly.communication.composers.rooms.SendFloorItemToRoomComposer;
import net.cobem.grizzly.communication.composers.rooms.SendWallItemToRoomComposer;
import net.cobem.grizzly.communication.events.user.InitializeInventoryEvent;
import net.cobem.grizzly.habbohotel.pathfinding.Position;
import net.cobem.grizzly.habbohotel.rooms.items.FloorItem;
import net.cobem.grizzly.habbohotel.rooms.items.WallItem;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.habbohotel.users.items.InventoryItem;

public class PlaceItemEvent implements GameEvent {

    @Override
    public void compose(Session Session, EventRequest Request) {
        if (Session.getActor().CurrentRoom.Owner != Session.getHabbo().getID()) {
            return;
        }

        String PlaceData = Request.readString();
        String[] Bits = PlaceData.split(" ");
        Integer ID = Integer.parseInt(Bits[0]);

        if (Bits[1].startsWith(":")) //It's a wall item!
        {
            String Position = Bits[1] + " " + Bits[2] + " " + Bits[3];

            InventoryItem Item = Session.getHabbo().getItems().getWalls().get(ID);

            try {
                //Grizzly.getStorage().execute("INSERT INTO server_room_items (room, base, position, extra, floor) VALUES ('" + Session.getActor().CurrentRoom.ID + "', '" + Item.getBase().ID + "', '" + Position + "', '0', '0')");
                Grizzly.getStorage().execute("UPDATE items SET room_id = '" + Session.getActor().CurrentRoom.ID + "', base_item = '" + Item.getBase().ID + "', wall_pos = '" + Position + "', extra_data = '0' WHERE id = '" + ID + "'");
            } catch (Exception ignored) {
            }

            int FurniID = Grizzly.getStorage().getLastEntry("items", "room_id = '" + Session.getActor().CurrentRoom.ID + "'");

            Session.getActor().CurrentRoom.sendMessage(SendWallItemToRoomComposer.compose(FurniID, Item.getBase().Sprite, Position, Session.getActor().CurrentRoom));

            Session.getHabbo().getItems().remove(ID, false);

            Session.sendResponse(DisposeItemFromInventoryComposer.compose(ID));

            (new InitializeInventoryEvent()).compose(Session, Request);

            Session.getActor().CurrentRoom.generateItem(ID);

            WallItem mItem = null;

            for (WallItem Furni : Session.getActor().CurrentRoom.getWalls().values()) {
                if (Furni.Position.equals(Position)) {
                    mItem = Furni;
                    break;
                }
            }

            if (mItem != null) {
                Grizzly.getHabboHotel().getInteractions().onPlace(mItem.getBase().Interaction, Session, mItem);
            }

        } else {
            Integer X = Integer.parseInt(Bits[1]);
            Integer Y = Integer.parseInt(Bits[2]);
            Integer Rotation = Integer.parseInt(Bits[3]);
            Float Height = (float) Session.getActor().CurrentRoom.getModel().getTileHeight(X, Y);

            if (Session.getActor().CurrentRoom.nonStackableTile(new Position(X, Y, 0))) {
                return;
            }

            Height += (float) Session.getActor().CurrentRoom.getTopHeight(new Position(X, Y, 0), null);

            try {
                InventoryItem Item = Session.getHabbo().getItems().getFloors().get(ID);

                Grizzly.getStorage().execute("UPDATE items SET room_id = '" + Session.getActor().CurrentRoom.ID + "', base_item = '" + Item.getBase().ID + "', x = '" + X + "', y = '" + Y + "', extra_data = ' ', rot = '" + Rotation + "', wall_pos = '', z = '" + Height + "' WHERE id = '" + ID + "'");

                Session.getActor().CurrentRoom.sendMessage(SendFloorItemToRoomComposer.compose(ID, Item.getBase().Sprite, X, Y, Rotation, Float.toString(Height), Session.getActor().CurrentRoom));

                Session.getHabbo().getItems().remove(ID, false);

                Session.sendResponse(DisposeItemFromInventoryComposer.compose(ID));

                (new InitializeInventoryEvent()).compose(Session, Request);

                Session.getActor().CurrentRoom.generateItem(ID); //Not sure what the hell is going on..

                if (Session.getActor().CurrentRoom.getItemsByPosition(new Position(X, Y, Height.intValue())) != null) {
                    FloorItem mItem = null;

                    for (FloorItem Furni : Session.getActor().CurrentRoom.getItemsByPosition(new Position(X, Y, 0)).values()) {
                        if (Furni.Height.equals(Height)) {
                            mItem = Furni;
                            break;
                        }
                    }

                    if (mItem != null) {
                        Grizzly.getHabboHotel().getInteractions().onPlace(mItem.getBase().Interaction, Session, mItem);
                    }
                }


            } catch (Exception ignored) {
            }
            //Session.GrabActor().CurrentRoom.RegenerateCollisionMap();
        }
    }

}
