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

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.composers.wired.SendWiredEffectDialogComposer;
import net.cobem.grizzly.habbohotel.pathfinding.Position;
import net.cobem.grizzly.habbohotel.rooms.items.FloorItem;
import net.cobem.grizzly.habbohotel.rooms.items.interactions.Interaction;
import net.cobem.grizzly.habbohotel.sessions.Session;

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

public class WiredTransportUserInteraction 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) {

            FloorItem RoomItem = (FloorItem)Item;

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

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

                if (Furnis.length == 0) {
                    return;
                }

                FloorItem TargetItem = Session.getActor().CurrentRoom.getItemByID(new Integer(Furnis[Grizzly.rand(0, Furnis.length - 1)]));

                if (TargetItem == null) {
                    return;
                }

                Session.getActor().move(new Position(TargetItem.X, TargetItem.Y, TargetItem.Height), Session.getActor().Rotation);
            }

        } else {
            FloorItem RoomItem = (FloorItem) Item;

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

            if (RoomItem.ExtraData.split(";").length > 1) {
                for(String Furni : RoomItem.ExtraData.split(";")) {
                    Furnis.add(Session.getActor().CurrentRoom.getItemByID(new Integer(Furni)));
                }
            }

            Session.sendResponse(SendWiredEffectDialogComposer.compose(5, RoomItem, Furnis, RoomItem.ExtraData, 0, 8, 0, 0, 0));
        }
    }

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