package net.cobem.grizzly.habbohotel.roleplay;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.composers.rooms.SendRoomShoutComposer;
import net.cobem.grizzly.communication.composers.rooms.UpdateRoomUserInformationComposer;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.storage.DatabaseResult;

import java.security.SecureRandom;
import java.sql.ResultSet;

public class RoleplayObject {
    private Session Session;

    private int TaxiDestination = 0;

    private TaxiMonitor TaxiMonitor;
    private LeavingMonitor LeavingMonitor;
    private WorkoutMonitor WorkoutMonitor;

    private int Strength = 0;
    private int Health = 0;

    private String Head = "";
    private String Look = "";

    private boolean Leaving;
    private boolean WorkingOut;

    public RoleplayObject(Session mSession) {
        Session = mSession;

        this.Strength = 0;
        this.Health = 0;

        TaxiMonitor = new TaxiMonitor(this);
        LeavingMonitor = new LeavingMonitor(Session.getHabbo().getID());
        WorkoutMonitor = new WorkoutMonitor(Session);

        fillClass();

        getHead();
    }

    public void callTaxi(int ID) {
        TaxiDestination = ID;

        if (Session.getHabbo().getRank() >= 5) //No waiting time.. 1 second just to be safe..
        {
            TaxiMonitor.setWaitingTime(1);
        } else if (Session.getHabbo().getRank() >= 2) //A waiting time of less than 10 seconds..
        {
            TaxiMonitor.setWaitingTime((new SecureRandom()).nextInt(10));
        } else //Regular User; Anywhere from 1 second to 30 seconds
        {
            TaxiMonitor.setWaitingTime((new SecureRandom()).nextInt(30));
        }
    }

    public void runTaxi() {
        Session.travel(TaxiDestination);
    }

    private boolean fillClass() {
        DatabaseResult GetRoleplay = Grizzly.getStorage().query("SELECT * FROM server_users_roleplay WHERE id = '" + Session.getHabbo().getID() + "'");

        if (GetRoleplay.rowCount() == 0) {
            Grizzly.getStorage().execute("INSERT INTO server_users_roleplay (id) VALUES ('" + Session.getHabbo().getID() + "')");

            this.Strength = 5;
            this.Health = 100;
        } else {
            ResultSet Values = Grizzly.getStorage().query("SELECT * FROM server_users_roleplay WHERE id = '" + Session.getHabbo().getID() + "'").getRow();

            try {
                this.setStrength(Values.getInt("strength"));
                this.setHealth(Values.getInt("health"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    private void getHead() {
        this.Head = "";

        for (String Node : Session.getHabbo().getLook().split("\\.")) {
            if (Node.contains("hr-") || Node.contains("hd-")) {
                if (this.Head.equals("")) {
                    this.Head = Node;
                }
                this.Head += ("." + Node + ".");
            }
        }

        //Grizzly.write(this.Head);
    }

    public void setBody(String Uniform) {
        Look = this.Head + Uniform;

        updateInformation();
    }

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

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

    public int getStrength() {
        return this.Strength;
    }

    public void setStrength(int Strength) {
        this.Strength = Strength;
    }

    public void upgradeStrength(int Interval) {
        this.Strength += Interval;
    }

    public int getHealth() {
        return this.Health;
    }

    public void setHealth(int Health) {
        this.Health = Health;
    }

    public void decreaseHealth(int Interval) {
        this.Health -= Interval;
    }

    public boolean isLeaving() {
        return this.Leaving;
    }

    public void setLeaving(boolean Leaving) {
        this.Leaving = Leaving;
    }

    public boolean isWorkingOut() {
        return this.WorkingOut;
    }

    public void setWorkingOut(boolean WorkingOut) {
        this.WorkingOut = WorkingOut;
    }

    public void startLeaveHotel() {
        Session.getActor().CurrentRoom.sendMessage(SendRoomShoutComposer.compose(Session.getHabbo().getID(), "--- Signing off in 15 seconds. --", 0, 0));
        setLeaving(true);

        this.LeavingMonitor.startLeave(Session.getActor().CurrentRoom);
    }

    public void startWorkingOut() {
        Session.getActor().CurrentRoom.sendMessage(SendRoomShoutComposer.compose(Session.getHabbo().getID(), "Starts to workout", 0, 11));
        setWorkingOut(true);

        this.WorkoutMonitor.start();
    }

    public void stopWorkingOut() {
        Session.getActor().CurrentRoom.sendMessage(SendRoomShoutComposer.compose(Session.getHabbo().getID(), "Stops working out", 0, 11));
        setWorkingOut(false);

        this.WorkoutMonitor.stop();
    }

    public WorkoutMonitor getWorkoutMonitor() {
        return this.WorkoutMonitor;
    }
}
