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

import gnu.trove.map.hash.THashMap;
import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.composers.rooms.*;
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.Map.Entry;

public class Actor {
    private Session Session;

    public Room CurrentRoom = null;

    public Position CurrentPosition = null;
    public Position GoalPosition = null;
    public Position OverridePosition = null;

    public int Rotation = 0;
    public int SignTimer = 0;
    public int Drink = 0;
    public int DrinkTimer = 0;
    public int GoalRoom = 0;
    public int RollerCycle = 0;
    public int IdleTime = 0;

    public float SitHeight = (float) 0.0;

    public boolean CycleInteraction;
    public boolean IsMoving;
    public boolean IsRolling;
    public boolean CanWalk = true;
    public boolean Ghost;
    public boolean IsIdle;

    public FloorItem InteractionItem;

    private THashMap<String, Object> Status = new THashMap<String, Object>();

    public Actor(Session mSession) {
        this.Session = mSession;
    }

    public void updateStatus() {
        String RequireStatus = "";

        for (Entry<String, Object> Stat : Status.entrySet()) {
            RequireStatus += Stat.getKey() + " " + Stat.getValue() + "/";
        }

        if (CurrentRoom != null) {
            CurrentRoom.sendMessage(SendRoomUserStatusComposer.compose(Session.getHabbo().getID(), CurrentPosition, Rotation, RequireStatus + "//"));
        }
    }

    public EventResponse getStatus() {
        String RequireStatus = "";

        for (Entry<String, Object> Stat : Status.entrySet()) {
            RequireStatus += Stat.getKey() + " " + Stat.getValue() + "/";
        }

        return SendRoomUserStatusComposer.compose(Session.getHabbo().getID(), CurrentPosition, Rotation, RequireStatus + "//");
    }

    public boolean addStatus(String Key, Object Value, boolean UpdateStatus) {
        if (Status.containsKey(Key)) {
            Status.remove(Key);
        }

        Status.put(Key, Value);

        if (UpdateStatus) {
            updateStatus();
        }
        return true;
    }

    public boolean removeStatus(String Key, Boolean UpdateStatus) {
        Status.remove(Key);

        if (UpdateStatus) {
            updateStatus();
        }
        return true;
    }

    public boolean hasStatus(String Key) {
        return Status.containsKey(Key);
    }

    public boolean clearStatus() {
        Status = new THashMap<>();
        return true;
    }

    public Boolean inRoom() {
        if (this.CurrentRoom == null) {
            return false;
        }

        if (this.CurrentRoom.ID == 0) {
            return false;
        }

        return true;
    }

    public void say(String STR, boolean Shout) {
        if (!Shout) {
            CurrentRoom.sendMessage(SendRoomChatComposer.compose(Session.getHabbo().getID(), STR, Grizzly.getHabboHotel().parseSmile(STR), 0));
        } else {
            CurrentRoom.sendMessage(SendRoomShoutComposer.compose(Session.getHabbo().getID(), STR, Grizzly.getHabboHotel().parseSmile(STR), 0));
        }
    }

    public void whisper(String Message, Session Receiver){
        Session.sendResponse(SendRoomWhisperComposer.compose(Session.getHabbo().getID(), Message, 0));
        Receiver.sendResponse(SendRoomWhisperComposer.compose(Session.getHabbo().getID(), Message, 0));
    }

    public void carry(int DrinkID) {
        Drink = DrinkID;

        if (Drink != 0) {
            DrinkTimer = 120;
        }

        CurrentRoom.sendMessage(SendDrinkStatusComposer.compose(Session.getHabbo().getID(), DrinkID));
    }

    public void stopMoving(boolean Freeze) {
        removeStatus("mv", true);
        Ghost = false;
        GoalPosition = null;

        if (Freeze) {
            CanWalk = false;
        }
    }

    public void move(Position NewPosition, int Rotation) {

        CanWalk = false;
        IsMoving = false;

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

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

        CurrentPosition = NewPosition;
        this.Rotation = Rotation;

        this.updateStatus();

        CanWalk = true;
    }

    public void unidle() {
        if (!IsIdle) {
            return;
        }

        this.IdleTime = 0;
        this.IsIdle = false;

        CurrentRoom.sendMessage(SendIdleStatusComposer.compose(Session.getHabbo().getID(), IsIdle));
    }

    public void idle() {
        if (IsIdle) {
            return;
        }
        this.IsIdle = true;

        CurrentRoom.sendMessage(SendIdleStatusComposer.compose(Session.getHabbo().getID(), IsIdle));
    }
}
