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

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.habbohotel.games.soccer.BallThread;
import net.cobem.grizzly.habbohotel.pathfinding.Position;
import net.cobem.grizzly.habbohotel.rooms.actors.Actor;
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 BallInteraction 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) {
        Actor Actor = Session.getActor();
        FloorItem RoomItem = (FloorItem) Item;

        Position ItemPosition = new Position(RoomItem.X, RoomItem.Y, RoomItem.Height.intValue());

        int Cycles = Grizzly.rand(2, 6);

        List<Position> Path = new ArrayList<>();

        Position NextPosition = ItemPosition.GetPositionInFront(Actor.Rotation);

        boolean GoBackwards = false;

        //Grizzly.write("Soccer Ball Cycles: " + Cycles);

        for (int i = 0; i < Cycles; i++) {
            if (Actor.CurrentRoom.getModel().Squares[NextPosition.X][NextPosition.Y] == SquareState.CLOSED || !Actor.CurrentRoom.getMap().realTileOpen(NextPosition)) {
                GoBackwards = true;

                NextPosition = NextPosition.GetPositionBehind(Actor.Rotation);
                continue;
            }

            Path.add(NextPosition);

            if (GoBackwards) {
                NextPosition = NextPosition.GetPositionBehind(Actor.Rotation);
            } else {
                NextPosition = NextPosition.GetPositionInFront(Actor.Rotation);
            }
        }

        new BallThread(RoomItem, Path, Session);
    }

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