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

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.habbohotel.pathfinding.Position;
import net.cobem.grizzly.habbohotel.rooms.Room;
import net.cobem.grizzly.habbohotel.rooms.actors.Actor;
import net.cobem.grizzly.habbohotel.rooms.items.FloorItem;
import net.cobem.grizzly.habbohotel.sessions.Session;

public class TeleportInteraction 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; //The day they make wall furniture that you can tele with... is the day this code doesn't work!

        if (RoomItem.Interactor != null) {
            return;
        }

        Position ItemPosition;

        try {
            ItemPosition = new Position(RoomItem.X, RoomItem.Y, RoomItem.Height.intValue());
        } catch (Exception ex) {
            return;
        }

        if (!Actor.CurrentPosition.TilesTouching(ItemPosition)) {
            //Actor.GoalPosition = ItemPosition.GetPositionInFront(Actor.Rotation);
            //Actor.IsMoving = true;
        } else {
            RoomItem.Interactor = Session;

            RoomItem.UpdateNeeded = true;
            RoomItem.UpdateCounter = 3;
        }
    }

    @Override
    public void onCycle(Object Item) {
        FloorItem RoomItem = (FloorItem) Item;
        Session Interactor = RoomItem.Interactor;
        Position Position = new Position(RoomItem.X, RoomItem.Y, RoomItem.Height.intValue());
        Room CurrentRoom = Grizzly.getHabboHotel().getRooms().getByID(RoomItem.Room);

        if (RoomItem.UpdateCounter == 4) {
            RoomItem.ExtraData = "0";

            RoomItem.serializeUpdate(CurrentRoom);

            RoomItem.UpdateCounter = 0;
        }

        if (RoomItem.UpdateCounter == 1) {
            RoomItem.ExtraData = "0";

            RoomItem.serializeUpdate(CurrentRoom);

            Interactor.getActor().CanWalk = true;
            Interactor.getActor().GoalPosition = Position.GetPositionInFront(RoomItem.Rotation);
            Interactor.getActor().IsMoving = true;

            RoomItem.Interactor = null;
            return;
        }

        if (RoomItem.UpdateCounter == 2) {
            RoomItem.ExtraData = "2";

            RoomItem.serializeUpdate(CurrentRoom);
            return;
        }

        int Partner = Grizzly.getHabboHotel().getFurniture().getTeleporters().getPair(RoomItem.ID);

        if (Partner == 0) {
            RoomItem.UpdateCounter = 1;
            return;
        }

        if (CurrentRoom.getItemByID(Partner) != null) //Easy way!
        {
            FloorItem Pair = CurrentRoom.getItemByID(Partner);

            if (Pair == null) {
                RoomItem.UpdateCounter = 1;
                return;
            }

            if (Pair.Interactor != null) {
                return;
            }

            Interactor.getActor().stopMoving(true); //Stop them in their tracks, we need to move them safely.

            Interactor.getActor().move(new Position(Pair.X, Pair.Y, Pair.Height.intValue()), Pair.Rotation);

            RoomItem.ExtraData = "2";

            RoomItem.serializeUpdate(CurrentRoom);

            Pair.Interactor = Interactor;
            Pair.UpdateCounter = 2; //We're getting a user so we need to open the doors.
            Pair.UpdateNeeded = true;

            RoomItem.UpdateCounter = 4;
            RoomItem.Interactor = null;
        } else {
            FloorItem Pair = new FloorItem(Partner);

            if (Pair.ID == 0) {
                RoomItem.UpdateCounter = 1;
                return;
            }

            if (Pair.Interactor != null) {
                return;
            }

            Room PairRoom;

            if (Grizzly.getHabboHotel().getRooms().getByID(Pair.Room) == null) {
                try {
                    Grizzly.getHabboHotel().getRooms().getRooms().put(Pair.Room, new Room(Pair.Room));
                } catch (Exception Ex) {
                    //Room doesn't exist.
                    RoomItem.Interactor = null;
                    RoomItem.UpdateCounter = 0;
                    return;
                }
            }

            PairRoom = Grizzly.getHabboHotel().getRooms().getByID(Pair.Room);

            Pair = PairRoom.getItemByID(Partner);

            RoomItem.ExtraData = "2";
            RoomItem.serializeUpdate(CurrentRoom);

            Interactor.getActor().OverridePosition = new Position(Pair.X, Pair.Y, Pair.Height.intValue()); //send them away
            Interactor.travel(PairRoom.ID);

            Pair.Interactor = Interactor;
            Pair.UpdateCounter = 2; //We're getting a user so we need to open the doors.
            Pair.UpdateNeeded = true;

            RoomItem.UpdateCounter = 4;
            RoomItem.Interactor = null;
        }
    }
}
