package net.cobem.grizzly.habbohotel.rooms.items.interactions.wired.effects;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.composers.rooms.UpdateFloorItemComposer;
import net.cobem.grizzly.communication.composers.wired.SendWiredEffectDialogExtraComposer;
import net.cobem.grizzly.habbohotel.pathfinding.Position;
import net.cobem.grizzly.habbohotel.rooms.Room;
import net.cobem.grizzly.habbohotel.rooms.items.FloorItem;
import net.cobem.grizzly.habbohotel.rooms.items.interactions.Interaction;
import net.cobem.grizzly.habbohotel.rooms.models.SquareState;
import net.cobem.grizzly.habbohotel.sessions.Session;

import java.util.ArrayList;
import java.util.List;

public class WiredMoveAndRotateFurniInteraction implements Interaction {
    @Override
    public void onPlace(Session Session, Object Item) {
    }

    @Override
    public void onRemove(Session Session, Object Item) {
    }

    @Override
    public void onTrigger(Session Session, Object Item, int Request) {
        if (Request == 2) {

            Room CurrentRoom = Session.getActor().CurrentRoom;
            FloorItem RoomItem = (FloorItem)Item;

            if (!RoomItem.ExtraData.equals("") || RoomItem.ExtraData.length() >= 2) {

                String[] Split = RoomItem.ExtraData.split("_");

                if (Split.length < 2) {
                    return;
                }

                String[] Movements = Split[0].split(";");
                String[] Furnis = Split[1].split(";");

                for(String ItemID : Furnis) {
                    FloorItem item = CurrentRoom.getItemByID(new Integer(ItemID));

                    int ID = item.ID;

                    Position NewPosition = CurrentRoom.getWiredUtility().calculateMovement(new Integer(Movements[0]), new Position(item.X, item.Y, item.Height));
                    int NewRotation = CurrentRoom.getWiredUtility().calculateRotation(new Integer(Movements[1]), item.Rotation);

                    if (CurrentRoom.getModel().Squares[NewPosition.X][NewPosition.Y] == SquareState.CLOSED) {
                        continue;
                    }

                    item.X = NewPosition.X;
                    item.Y = NewPosition.Y;
                    item.Rotation = NewRotation;

                    Float Z = (float) CurrentRoom.getModel().getTileHeight(NewPosition.X, NewPosition.Y);

                    for (FloorItem fItem : CurrentRoom.getItemsByPosition(new Position(NewPosition.X, NewPosition.Y, 0)).values()) {
                        if (fItem.ID != item.ID) {
                            if (!fItem.getBase().Stackable) {
                                return;
                            }

                            if (fItem.Height != 0){
                                if (fItem.Height > Z){
                                    Z = (float) fItem.getBase().StackHeight + fItem.Height;
                                }
                            }
                            else {
                                Z = fItem.getBase().StackHeight;
                            }
                        }
                    }

                    item.Height = Z;

                    for (Position mPosition : CurrentRoom.getPositions().values()) {
                        if (mPosition.X == NewPosition.X && mPosition.Y == NewPosition.Y) {
                            continue;
                        }
                    }

                    Grizzly.getStorage().execute("UPDATE server_items SET x = '" + item.X + "', y = '" + item.Y + "', rotation = '" + item.Rotation + "', height = '" + item.Height + "' WHERE id = '" + ID + "'");

                    CurrentRoom.getFloors().remove(ID);
                    CurrentRoom.getFloors().put(ID, new FloorItem(ID));

                    CurrentRoom.getMap().ReGenerateCollisionMap();

                    CurrentRoom.sendMessage(UpdateFloorItemComposer.compose(ID, item, CurrentRoom.Owner));
                }
            }

        } else {
            FloorItem RoomItem = (FloorItem) Item;

            List<FloorItem> Furnis = new ArrayList<>();

            String[] Split = RoomItem.ExtraData.split("_");

            if (Split.length >= 2) {
                for(String Furni : Split[1].split(";")) {
                    Furnis.add(Session.getActor().CurrentRoom.getItemByID(new Integer(Furni)));
                }
            }

            Session.sendResponse(SendWiredEffectDialogExtraComposer.compose(5, RoomItem, Furnis, RoomItem.ExtraData, 2, 0, 0, 0, 4));
        }
    }

    @Override
    public void onCycle(Object Item) {
    }
}
