package net.cobem.grizzly.habbohotel.users.items;

import gnu.trove.map.hash.THashMap;
import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.habbohotel.furni.Furniture;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.storage.DatabaseResult;

import java.sql.ResultSet;
import java.sql.SQLException;


public class ItemHandler {
    private int ID;

    private THashMap<Integer, InventoryItem> Inventory;

    public ItemHandler(int ID) throws SQLException {
        this.ID = ID;

        Inventory = new THashMap<>();

        DatabaseResult GrabItems = Grizzly.getStorage().query("SELECT * FROM items WHERE user_id = '" + ID + "' AND room_id = '0'");

        if (GrabItems.getTable() != null) {
            ResultSet Items = GrabItems.getTable();

            while (Items.next()) {
                String Extra = (Items.getString("extra_data").length() > 1 ? Items.getString("extra_data") : null);

                Inventory.put(Items.getInt("id"), new InventoryItem(Items.getInt("id"), Items.getInt("user_id"), Items.getInt("base_item"), Extra));
            }

        }
    }

    public boolean remove(int ID, boolean Perm) {
        if (Perm) {
            Grizzly.getStorage().execute("DELETE FROM items WHERE id = '" + ID + "' AND user_id = '" + this.ID + "'");
        }

        Inventory.remove(ID);
        return true;
    }

    public int add(Furniture Item, boolean Perm, String Extra) {
        if (Perm) {
            Grizzly.getStorage().execute("INSERT INTO items (room_id, user_id, base_item, extra_data, wall_pos) VALUES ('0', '" + this.ID + "', '" + Item.ID + "', '" + Extra + "', '')");

            if (Item.Interaction.equals("teleport")) {
                int One = Grizzly.getStorage().getLastEntry("items", "user_id = '" + this.ID + "'"); //Just to be safe, we're using a where clause.

                Inventory.put(One, new InventoryItem(One, this.ID, Item.ID, Extra));

                Grizzly.getStorage().execute("INSERT INTO items (room_id, user_id, base_item, extra_data, wall_pos) VALUES ('0', '" + this.ID + "', '" + Item.ID + "', '" + Extra + "', '')");

                int Two = Grizzly.getStorage().getLastEntry("items", "user_id = '" + this.ID + "'");

                Inventory.put(Two, new InventoryItem(Two, this.ID, Item.ID, Extra));

                Grizzly.getHabboHotel().getCatalog().createTeleporters(One, Two);

                return One;
            }
        }

        int LastID = Grizzly.getStorage().getLastEntry("items", "user_id = '" + this.ID + "' AND base_item = '" + Item.ID + "'");

        Inventory.put(LastID, new InventoryItem(LastID, this.ID, Item.ID, Extra));

        return LastID;
    }

    public THashMap<Integer, InventoryItem> getInventory() {
        return Inventory;
    }

    public THashMap<Integer, InventoryItem> getFloors() {
        THashMap<Integer, InventoryItem> Items = new THashMap<Integer, InventoryItem>();

        for (InventoryItem Item : this.Inventory.values()) {
            if (Item.getBase().Type.contains("s")) {
                Items.put(Item.ID, Item);
            }
        }
        return Items;
    }

    public THashMap<Integer, InventoryItem> getWalls() {
        THashMap<Integer, InventoryItem> Items = new THashMap<Integer, InventoryItem>();

        for (InventoryItem Item : this.Inventory.values()) {
            if (Item.getBase().Type.contains("i")) {
                Items.put(Item.ID, Item);
            }
        }

        return Items;
    }

    public InventoryItem getItem(int id) {
        return Inventory.get(id);
    }
}
