package net.cobem.grizzly.habbohotel.sessions;


import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.HeaderLibrary;
import net.cobem.grizzly.communication.composers.rooms.UpdateRoomUserInformationComposer;
import net.cobem.grizzly.communication.composers.user.SendToHomeRoomComposer;
import net.cobem.grizzly.habbohotel.roleplay.RoleplayObject;
import net.cobem.grizzly.habbohotel.rooms.actors.Actor;
import net.cobem.grizzly.habbohotel.users.User;
import org.jboss.netty.channel.Channel;

public class Session {
    private int ID;

    private Channel Channel;
    private User Habbo;
    private Actor Actor;

    private EventResponse Response;

    public boolean GlobalEffect;

    public int OverideID = 0;
    public int CurrentEffect = 0;

    private RoleplayObject RoleplayObject;

    public String MachineID;

    public Session(Channel Channel, int ID) {
        this.Channel = Channel;
        this.ID = ID;
        this.Response = new EventResponse();
    }

    public int id() {
        return ID;
    }

    public Channel getChannel() {
        return Channel;
    }

    public String ip() {
        if (Channel == null) {
            return "NULL";
        } else {
            return Channel.getRemoteAddress().toString().split(":")[0].substring(1);
        }
    }

    public User getHabbo() {
        return Habbo;
    }

    public Actor getActor() {
        return this.Actor;
    }

    public EventResponse getResponse() {
        return Response;
    }

    public void sendResponse(EventResponse Message) {
        if (Grizzly.getConfig().get("net.grizzly.packetlog").equals("1")) {
            Grizzly.write("[" + Message.header() + "] " + Message.body() + " sent by " + this.ip());
            Grizzly.write("[" + Message.header() + "] " + Message.SimpleResponse);
        }

        //if (this.Channel.isWritable())
        {
            this.Channel.write(Message);
        }
    }

    public void sendResponse() {
        if (Grizzly.getConfig().get("net.grizzly.packetlog").equals("1")) {
            Grizzly.write("[" + Response.header() + "] " + Response.body() + " sent by " + this.ip());
            Grizzly.write("[" + Response.header() + "] " + Response.SimpleResponse);
        }

        //if (this.Channel.isWritable())
        {
            this.Channel.write(Response);
        }
    }

    public EventResponse returnResponse() {
        return Response;
    }

    public void leaveRoom(boolean LeavingHotel) {
        if (!getActor().inRoom()) {
            return;
        }

        getActor().CurrentRoom.removeUser(this, LeavingHotel);
    }

    public void travel(int ID) {
        this.OverideID = ID;

        sendResponse(SendToHomeRoomComposer.compose(ID));
    }

    public void effect(int EffectID) {
        CurrentEffect = EffectID;

        EventResponse Message = new EventResponse();

        Message.Initialize(HeaderLibrary.SendRoomEffectEvent);
        Message.addInt(getHabbo().getID());
        Message.addInt(CurrentEffect);
        Message.addInt(0);
        getActor().CurrentRoom.sendMessage(Message);
    }

    public void updateInformation() {
        getActor().CurrentRoom.sendMessage(UpdateRoomUserInformationComposer.compose(getHabbo().getID(), getHabbo().getLook(), getHabbo().getGender().toString(), getHabbo().getMotto()));

        sendResponse(UpdateRoomUserInformationComposer.compose(-1, getHabbo().getLook(), getHabbo().getGender().toString(), getHabbo().getMotto()));
    }

    public RoleplayObject getRoleplay() {
        return this.RoleplayObject;
    }

    public void tryLogin(String Ticket) {
        this.Habbo = new User(Ticket);

        this.Actor = new Actor(this);

        if (Grizzly.roleplayEnabled()) {
            this.RoleplayObject = new RoleplayObject(this);
        } else {
            this.RoleplayObject = null;
        }
    }
}
