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

import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.HeaderLibrary;
import net.cobem.grizzly.habbohotel.pathfinding.Position;
import net.cobem.grizzly.habbohotel.rooms.items.FloorItem;
import net.cobem.grizzly.habbohotel.rooms.models.SquareState;
import net.cobem.grizzly.habbohotel.sessions.Session;

public class RollerInteraction 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) {
        Position NextPosition = Session.getActor().CurrentPosition.GetPositionInFront(((FloorItem) Item).Rotation);

        if ((Session.getActor().CurrentRoom.getModel().Squares[NextPosition.X][NextPosition.Y] == SquareState.CLOSED)) {
            Session.getActor().IsRolling = false;
            Session.getActor().RollerCycle = 0;
            return;
        }

        EventResponse Message = new EventResponse();

        Message.Initialize(HeaderLibrary.UpdateItemOnRollerEvent);
        Message.addInt(Session.getActor().CurrentPosition.X);
        Message.addInt(Session.getActor().CurrentPosition.Y);
        Message.addInt(NextPosition.X);
        Message.addInt(NextPosition.Y);
        Message.addInt(0);
        Message.addInt(((FloorItem) Item).ID);
        Message.addInt(2);
        Message.addInt(Session.getHabbo().getID());
        Message.addString(Session.getActor().CurrentPosition.Z);

        Session.getActor().CurrentPosition = Session.getActor().CurrentPosition.GetPositionInFront(((FloorItem) Item).Rotation);

        if (Session.getActor().CurrentRoom.getItemByPosition(NextPosition) == null){
            Message.addString(Session.getActor().CurrentRoom.getModel().getTileHeight(NextPosition.X, NextPosition.Y));

            Session.getActor().CurrentPosition.Z = Session.getActor().CurrentRoom.getModel().getTileHeight(NextPosition.X, NextPosition.Y);
        }
        else{
            Message.addString(Session.getActor().CurrentRoom.getItemByPosition(NextPosition).Height + 0.55);
            Session.getActor().CurrentPosition.Z = Session.getActor().CurrentRoom.getItemByPosition(NextPosition).Height + 0.55;
        }

        Session.getActor().CurrentRoom.sendMessage(Message);

        Session.getActor().CurrentRoom.getPositions().remove(Session.getHabbo().getID());

        Session.getActor().CurrentRoom.getMap().UserCollisionMap[Session.getActor().CurrentPosition.X][Session.getActor().CurrentPosition.Y] = false;

        //Session.getActor().CurrentRoom.getMap().ReGenerateCollisionMap();


    }

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