package net.cobem.grizzly.habbohotel.rooms.actors;

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.items.FloorItem;
import net.cobem.grizzly.habbohotel.sessions.Session;

import java.util.Collection;

public class ActorHandler {
    private Room Room;

    public ActorHandler(Room mRoom) {
        this.Room = mRoom;
    }

    public void cycle() {
        try {
            for (Session Session : this.Room.getParty().values()) {
                /*
				 * Explanation for the IF statements.
				 * We need a .5 second time out between the user getting to the tile and the interaction occuring.
				 * So we wait .5 seconds after they're at the tile to start whatever interaction needed.
				 */
                if (Session.getActor().hasStatus("mv") && !Session.getActor().IsMoving) {
                    Session.getActor().removeStatus("mv", true);
                }

                if (Session.getActor().hasStatus("mv") && Room.getItemByPosition(Session.getActor().CurrentPosition) != null && Room.getItemByPosition(Session.getActor().CurrentPosition).getBase().Sitable) {
                    Session.getActor().removeStatus("mv", true);
                    Session.getActor().addStatus("sit", Session.getActor().SitHeight, true);
                    Session.getActor().SitHeight = (float) 0.0;
                }

                if (Session.getActor().CycleInteraction) {
                    if (Session.getActor().InteractionItem.getBase().Interaction.equals("roller")) //Give it another 1/2 of a second.
                    {
                        Session.getActor().IsRolling = true;

                        if (Session.getActor().RollerCycle > 0) {
                            Session.getActor().RollerCycle--;
                            continue;
                        }
                    }

                    Grizzly.getHabboHotel().getInteractions().onTrigger(Session.getActor().InteractionItem.getBase().Interaction, Session, Session.getActor().InteractionItem, 0);
                    Session.getActor().CycleInteraction = false;
                }

                if (Session.getActor().IsMoving) {

                    Session.getActor().IdleTime = 0;

                    Collection<byte[]> Path;

                    if (Session.getActor().Ghost) {
                        Path = Session.getActor().CurrentRoom.getGhostPathfinder().Path(
                                (byte) Session.getActor().CurrentPosition.X,
                                (byte) Session.getActor().CurrentPosition.Y,
                                (byte) Session.getActor().GoalPosition.X,
                                (byte) Session.getActor().GoalPosition.Y,
                                Grizzly.getHabboHotel().getRooms().MaxDrop,
                                Grizzly.getHabboHotel().getRooms().MaxJump);
                    } else {
                        Path = Session.getActor().CurrentRoom.getPathfinder().Path(
                                (byte) Session.getActor().CurrentPosition.X,
                                (byte) Session.getActor().CurrentPosition.Y,
                                (byte) Session.getActor().GoalPosition.X,
                                (byte) Session.getActor().GoalPosition.Y,
                                Grizzly.getHabboHotel().getRooms().MaxDrop,
                                Grizzly.getHabboHotel().getRooms().MaxJump);
                    }


                    if (Path.size() == 0) {
                        Session.getActor().IsMoving = false;
                    }

                    byte[] NextStep = null;
                    byte[] NextNextStep = null;
                    int Key = 0;

                    for (byte[] Next : Path) {
                        if (Key == 0) {
                            NextStep = Next;
                            Key++;
                            continue;
                        }

                        if (Key == 1) {
                            NextNextStep = Next;
                            break;
                        }
                    }

                    if (NextStep == null) {
                        continue;
                    }

                    FloorItem NextItem;

                    try {
                        NextItem = this.Room.getMap().GetItemAtPosition(new Position(NextStep[0], NextStep[1], 0));
                    } catch (Exception Ex) {
                        NextItem = null;
                    }

                    Session.getActor().Rotation = Session.getActor().CurrentPosition.Calculate(NextStep[0], NextStep[1]);

                    if (NextItem != null) {
                        if (NextItem.getBase().Sitable) {
                            Session.getActor().CurrentPosition.Z = NextItem.Height;
                        } else {
                            Session.getActor().CurrentPosition.Z = (NextItem.Height + NextItem.getBase().StackHeight);
                        }
                    } else {
                        Session.getActor().CurrentPosition.Z = this.Room.getModel().getTileHeight(NextStep[0], NextStep[1]);
                    }

                    Room.getPositions().remove(Session.getHabbo().getID());

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

                    if (Session.CurrentEffect != 0 && !Session.GlobalEffect) {
                        NextItem = Session.getActor().CurrentRoom.getItemForInteraction(Session.getActor().CurrentPosition);

                        if (NextItem == null || NextItem.getBase().Sitable) {
                            Session.effect(0);
                        }
                    }

                    FloorItem CurrentItem = null;

                    if (Session.getActor().CurrentRoom.getItemByPosition(Session.getActor().CurrentPosition) != null) {
                        CurrentItem =  Session.getActor().CurrentRoom.getItemByPosition(Session.getActor().CurrentPosition);
                    }

                    Session.getActor().removeStatus("sit", false);
                    Session.getActor().removeStatus("lay", false);
                    Session.getActor().addStatus("mv", (+NextStep[0] + "," + NextStep[1] + "," + Double.toString(Session.getActor().CurrentPosition.Z)), true);

                    Session.getActor().CurrentPosition.X = NextStep[0];
                    Session.getActor().CurrentPosition.Y = NextStep[1];

                    Room.getPositions().put(Session.getHabbo().getID(), Session.getActor().CurrentPosition);

                    if (Grizzly.roleplayEnabled() && Session.getRoleplay().isWorkingOut()) {
                        Session.getRoleplay().stopWorkingOut();
                    }

                    if (CurrentItem != null) {
                        Room.getWiredUtility().onUserWalkOffFurni(Session, CurrentItem);
                    }

                    if (NextNextStep == null) {
                        NextItem = Session.getActor().CurrentRoom.getItemForInteraction(Session.getActor().CurrentPosition);

                        if (NextItem != null) {
                            if (NextItem.getBase().Sitable) {
                                Session.getActor().Rotation = NextItem.Rotation;

                                if (NextItem.Height != 0){
                                    Session.getActor().SitHeight = NextItem.Height + NextItem.getBase().StackHeight;
                                }
                                else{
                                    Session.getActor().SitHeight = NextItem.getBase().StackHeight;
                                }
                            } else {
                                Session.getActor().CycleInteraction = true;
                                Session.getActor().InteractionItem = NextItem;

                                if (NextItem.getBase().Interaction.equals("roller")) {
                                    Session.getActor().RollerCycle = 1;
                                }
                            }

                        Session.getActor().CurrentRoom.getWiredUtility().onUserWalkOnFurni(Session, NextItem);
                        } else {
							/*FloorItem ItemInFront = Session.getActor().CurrentRoom.getItemByPosition(Session.getActor().CurrentPosition.GetPositionInFront(Session.getActor().Rotation));

                            if (ItemInFront.getBase().Interaction.equals("ball"))
                            {
                                Session.getActor().CycleInteraction = true;
                                Session.getActor().InteractionItem = ItemInFront;
                            } */

                            Session.getActor().IsMoving = false;

                            if (Path.size() > 1) {
                                Session.getActor().removeStatus("mv", true);
                            }
                        }
                    }
                    /* else
                    {
                        NextItem = Session.getActor().CurrentRoom.getItemForInteraction(Session.getActor().CurrentPosition);

                        if (NextItem != null)
                        {
                            if (!NextItem.getBase().Sitable)
                            {
                                Session.getActor().CycleInteraction = true;
                                Session.getActor().InteractionItem = NextItem;
                            }
                        }
                    } */
                } else if (Session.getActor().IsRolling) {
                    if (Session.getActor().RollerCycle > 0) {
                        continue;
                    }

                    FloorItem NextItem = Session.getActor().CurrentRoom.getItemForInteraction(Session.getActor().CurrentPosition);

                    if (NextItem != null) {
                        if (NextItem.getBase().Sitable) {
                            Session.getActor().Rotation = NextItem.Rotation;

                            if (NextItem.Height != 0){
                                Session.getActor().SitHeight = NextItem.Height + NextItem.getBase().StackHeight;
                            }
                            else{
                                Session.getActor().SitHeight = NextItem.getBase().StackHeight;
                            }
                        } else {
                            Session.getActor().CycleInteraction = true;
                            Session.getActor().InteractionItem = NextItem;

                            if (NextItem.getBase().Interaction.equals("roller")) {
                                Session.getActor().RollerCycle = 1;
                            }

                            //Session.getActor().CurrentPosition.Z = (NextItem.Height + NextItem.getBase().StackHeight);
                            //Session.getActor().updateStatus();
                        }
                    } else {
                        FloorItem ItemInFront = Session.getActor().CurrentRoom.getItemByPosition(Session.getActor().CurrentPosition.GetPositionInFront(Session.getActor().Rotation));

                        if (ItemInFront.getBase().Interaction.equals("ball")) {
                            Session.getActor().CycleInteraction = true;
                            Session.getActor().InteractionItem = ItemInFront;
                        }

                        Session.getActor().IsRolling = false;
                    }
                }

                //SIGNS
                if (Session.getActor().SignTimer > 0) {
                    if (Session.getActor().SignTimer == 1) {
                        Session.getActor().removeStatus("sign", true);
                    }

                    Session.getActor().SignTimer--;
                }

                //DRINKS
                if (Session.getActor().DrinkTimer > 0) {
                    if (Session.getActor().DrinkTimer == 1) {
                        Session.getActor().Drink = 0;
                        Session.getActor().carry(0);
                    }

                    Session.getActor().DrinkTimer--;
                }

                Session.getActor().IdleTime++;

                if (Session.getActor().IdleTime > new Integer(Grizzly.getConfig().get("habbo.user.idle.time"))) {
                    Session.getActor().idle();
                }

            }
        } catch (Exception Ex) {
        }
    }
}
