package sl.steeldood.bit.client.utils.module;

import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import sl.steeldood.bit.client.utils.general.ReflectionUtil;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author steeldood Created: 29. 06. 2017 at 22:18
 */
public class ItemUtil {

    public static Set<Integer> HOTBAR_USEFUL_ITEMS = new HashSet<>(Arrays.asList(261, 375, 353, 377, 288, 265, 326, 483, 373,
            283, 276, 368, 320, 366, 364, 393, 396));

    public static Set<Integer> HCF_USEFUL_ITEMS = new HashSet<>(Arrays.asList(
            89, 368, 396, 483, 384, 382, 379, 379, 378, 377, 376, 375, 373, 372, 371, 369, 363,
            370, 353, 340, 339, 338, 322, 320, 317, 316, 315, 314, 313, 312, 311, 310, 305,
            304, 303, 302, 278, 277, 276, 264, 265, 266, 261, 122, 120, 119, 118, 117, 116,
            115, 57, 41, 22, 42, 152, 133, 130));

    public static Set<Integer> SG_USEFUL_ITEMS = new HashSet<>(Arrays.asList(
            258, 259, 260, 261, 262, 264, 266, 265, 267, 268, 271, 272, 275, 279, 286, 276, 283,
            289, 287, 288, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311,
            312, 313, 314, 315, 316, 317, 318, 319, 320, 322, 326, 327, 332, 350, 346, 344, 345, 354,
            357, 360, 368, 366, 364, 363, 438, 400, 396, 393, 392, 391, 385, 333, 280));

    public static Set<Integer> UHC_USEFUL_ITEMS = new HashSet<>(Arrays.asList(
            41, 42, 46, 49, 47, 57, 145, 261, 262, 259, 264, 265, 266, 267, 276, 276, 279, 277, 256, 287, 288,
            289, 206, 207, 209, 308, 310, 311, 312, 313, 320, 318, 322, 325, 326, 327, 332, 329, 346, 350, 339,
            340, 338, 334, 354, 368, 371, 373, 384, 385, 393, 392, 391, 396, 395, 417, 418, 419, 420, 421, 438));

    public static boolean isUseless(ItemStack item, String mode) {
        int itemId = getIdFromItem(item);

        switch (mode.toLowerCase()) {
            case "hotbar":
                return !HOTBAR_USEFUL_ITEMS.contains(itemId);
            case "hcf":
                return !HCF_USEFUL_ITEMS.contains(itemId);
            case "sg":
                return !SG_USEFUL_ITEMS.contains(itemId);
            case "uhc":
                return !UHC_USEFUL_ITEMS.contains(itemId);
            case "general":
                return !UHC_USEFUL_ITEMS.contains(itemId) && !SG_USEFUL_ITEMS.contains(itemId);
        }

        return false;
    }

    public static int[][] generate2DInventoryArray() {
        int[][] array = new int[3][9];
        /**
         *  {
         *   {9, 10, 11, 12, 13, 14, 15, 16, 17},
         *   {18, 19, 20, 21, 22, 23, 24, 25, 26},
         *   {27, 28, 29, 30, 31, 32, 33, 34, 35}
         *  }
         *
         */
        int row = 0;
        int i = 0;
        for (int slot = 9; slot < Wrapper.player_inventoryItems().length; slot++) {
            array[row][i] = slot;
            if (i % 8 == 0 && i >= 8) {
                i = 0;
                row++;
                continue;
            }
            i++;
        }
        return array;
    }

    public static int getIdFromItem(Item item) {
        return Item.func_150891_b(item);
    }

    public static int getIdFromItem(ItemStack item) {
        return Item.func_150891_b(ItemUtil.getItemFromStack(item));
    }


    public static Item getItemFromStack(ItemStack i) {
        return i.func_77973_b();
    }

    public static int player_itemSlotHotbar(Item compare) {
        for (int i = 0; i < 9; i++) {
            if (Wrapper.player_inventoryItems() != null) {
                try {
                    Item item = getItemFromStack(Wrapper.player_inventoryItems()[i]);
                    if (item != null && item.equals(compare))
                        return i;
                } catch (Exception e) {
                    continue;
                }
            }
        }
        return -1;
    }

    public static int player_itemSlot(Item compare) {
        for (int i = 0; i < Wrapper.player_inventoryItems().length; i++) {
            if (Wrapper.player_inventoryItems() != null) {
                try {
                    Item item = getItemFromStack(Wrapper.player_inventoryItems()[i]);
                    if (item != null && item.equals(compare))
                        return i;
                } catch (Exception e) {
                    continue;
                }
            }
        }
        return -1;
    }

    public static int player_itemSlot(Class<? extends Item> compare) {
        for (int i = 0; i < Wrapper.player_inventoryItems().length; i++) {
            if (Wrapper.player_inventoryItems() != null) {
                try {
                    Item item = getItemFromStack(Wrapper.player_inventoryItems()[i]);
                    if (item != null && item.getClass().equals(compare))
                        return i;
                } catch (Exception e) {
                    continue;
                }
            }
        }
        return -1;
    }

    public static int player_itemSlot_noHotbar(Item compare) {
        for (int i = 9; i < Wrapper.player_inventoryItems().length; i++) {
            if (Wrapper.player_inventoryItems() != null) {
                try {
                    Item item = getItemFromStack(Wrapper.player_inventoryItems()[i]);
                    if (item != null && item.equals(compare))
                        return i;
                } catch (Exception e) {
                    continue;
                }
            }
        }
        return -1;
    }

    public static int getPotionSlot(boolean hotbarOnly) {
        try {
            int potSlot = (hotbarOnly ? player_itemSlotHotbar(Items.field_151068_bn) : player_itemSlot(Items.field_151068_bn));
            if (potSlot == -1) return -1;
            ItemStack potStack = Wrapper.player_inventoryItems()[potSlot];
            if (potStack == null) return -1;
            if (isHealthPot(potStack)) return potSlot;
            return -1;
        } catch (Exception e) {
            return -1;
        }
    }

    public static int getSoupSlot(boolean hotbarOnly) {
        try {
            return hotbarOnly ? player_itemSlotHotbar(Items.field_151009_A) : player_itemSlot(Items.field_151009_A);
        } catch (Exception e) {
            return -1;
        }
    }

    public static boolean player_inventoryFull() {
        for (int i = 0; i < Wrapper.player_inventoryItems().length; i++) {
            if (Wrapper.player_inventoryItems()[i] == null) return false;
        }
        return true;
    }

    public static boolean isHealthPot(ItemStack item) {
        try {
            if (item == null) return false;
            Item pot = getItemFromStack(item);
            if (pot == null) return false;
            ItemPotion potion = (ItemPotion) pot;
            if (potion != null && potion.func_77832_l(item) != null) {
                for (Object obj : potion.func_77832_l(item)) {
                    if (obj == null || !(obj instanceof PotionEffect)) continue;
                    PotionEffect effect = (PotionEffect) obj;
                    if ((effect.func_76456_a() == Potion.field_76432_h.func_76396_c()) && ItemPotion.func_77831_g((Integer) ReflectionUtil.getFieldValue("field_77991_e", item)))
                        return true;
                }
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean player_hasPotsInHotbar() {
        return ItemUtil.getPotionSlot(true) >= 0;
    }

    public static boolean player_hasPotsInInventory() {
        return ItemUtil.getPotionSlot(false) >= 0;
    }

    public static boolean player_hasSoupsInInventory() {
        return ItemUtil.getSoupSlot(false) >= 0;
    }

    public static boolean player_hasSoupsInHotbar() {
        return ItemUtil.getPotionSlot(true) >= 0;
    }


    public static boolean isChestEmpty(GuiChest chest) {
        for (int i = 0; i < ((IInventory) ReflectionUtil.getFieldValue("field_147015_w", chest)).func_70302_i_(); i++) {
            ItemStack stack = ((IInventory) ReflectionUtil.getFieldValue("field_147015_w", chest)).func_70301_a(i);
            if (stack != null) return false;
        }
        return true;
    }

    public static boolean isValidChest(GuiChest chest) {
        if (chest == null) return false;
        IInventory inventory = ((IInventory) ReflectionUtil.getFieldValue("field_147015_w", chest));
        int size = inventory.func_70302_i_();
        String title = inventory.func_145825_b();
        return (title.toLowerCase().contains("chest") || title.toLowerCase().contains("tier")) && (size == 27 || size == 54);
    }

    public static boolean isDoubleChest(GuiChest chest) {
        return ((IInventory) ReflectionUtil.getFieldValue("field_147015_w", chest)).func_70302_i_() == 54;
    }

}