package net.cobem.grizzly.habbohotel.rooms;

import gnu.trove.map.hash.THashMap;
import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.storage.DatabaseResult;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;


public class RoomHandler {
    private THashMap<Integer, Room> Rooms;

    @SuppressWarnings("unused")
    private RoomMonitor Monitor;

    public float MaxJump;
    public float MaxDrop;

    public RoomHandler() {
        Rooms = new THashMap<>();

        this.MaxDrop = new Float(Grizzly.getConfig().get("habbo.pathfinder.maxdrop"));
        this.MaxJump = new Float(Grizzly.getConfig().get("habbo.pathfinder.maxjump"));
/*		
        Grizzly.GrabDatabase().SetQuery("SELECT * FROM server_rooms");
		
		try 
		{
			ResultSet Results = Grizzly.GrabDatabase().GrabTable();
			
			while (Results.next())
			{
				Rooms.put(new Integer(Results.readInt("id")), new Room(Results));
			}
		} 
		catch (SQLException e) 
		{
			//
		}
		
		Grizzly.WriteOut("Loaded and stored " + Rooms.size() + " rooms!");
*/
        Monitor = new RoomMonitor();
    }

    public Room getByID(int ID) {
        if (Rooms.containsKey(new Integer(ID))) {
            return Rooms.get(new Integer(ID));
        } else {
            return null;
        }
    }


    public THashMap<Integer, Room> getByOwner(int User) {
        THashMap<Integer, Room> Result = new THashMap<Integer, Room>();

        for (Room UserRoom : Rooms.values()) {
            if (UserRoom.Owner == User) {
                Result.put(UserRoom.ID, UserRoom);
            }
        }

        return Result;
    }

    public THashMap<Integer, Room> getByOwner(int User, boolean CheckDB) {
        THashMap<Integer, Room> Result = getByOwner(User);

        DatabaseResult Res = Grizzly.getStorage().preparedQuery("SELECT username FROM users WHERE id = ?", Arrays.asList(String.valueOf(User)));

        String Name = Res.getString();

        DatabaseResult GrabRooms = Grizzly.getStorage().query("SELECT * FROM rooms WHERE owner = '" + Name + "'");

        try {
            ResultSet Results = GrabRooms.getTable();

            while (Results.next()) {
                if (!Result.containsKey(new Integer(Results.getInt("id")))) {
                    Result.put(Results.getInt("id"), new Room(Results));
                }
            }
        } catch (SQLException e) {
            //
        }

        return Result;
    }

    public THashMap<Integer, Room> getPopulated() {
        THashMap<Integer, Room> Result = new THashMap<Integer, Room>();

        for (Room UserRoom : Rooms.values()) {
            if (UserRoom.getParty().size() >= 1) {
                Result.put(UserRoom.ID, UserRoom);
            }
        }

        return Result;
    }

    public THashMap<Integer, Room> getRooms() {
        return Rooms;
    }

    public boolean dispose(int Room, boolean Perm) {
        if (Perm) {
            Grizzly.getStorage().execute("DELETE FROM rooms WHERE id = '" + Room + "'");
        }

        //TODO: Remove items from room as well!
        Rooms.remove(Room);
        return true;
    }

    public int create(String Name, String Owner, String Model) {

        Grizzly.getStorage().execute("INSERT INTO rooms " +
                "(caption, owner, description, state, password, model_name, wallpaper, floor, landscape) VALUES " +
                "('" + Name + "', '" + Owner + "', '" + Name + "', '0', ' ', '" + Model + "', '0.0', '0.0', '0.0')");

        DatabaseResult GrabLastID = Grizzly.getStorage().query("SELECT id FROM rooms ORDER BY id DESC LIMIT 1");

        int LastID = GrabLastID.getInt();

        Grizzly.getStorage().execute("INSERT INTO room_rights (room_id, user_id) VALUES ('" + LastID + "', '" + Owner + "')");

        Rooms.put(LastID, new Room(LastID));
        return LastID;
    }
}
