package me.km.items;

import java.util.LinkedList;
import java.util.List;
import me.km.blocks.cookingpot.CookingPotColorMixer;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.block.Blocks;
import net.minecraft.item.Items;
import net.minecraft.item.Item;
import net.minecraft.item.Food;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.StringNBT;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.LanguageMap;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

public class ItemColoredSoup extends ItemFood {
    public ItemColoredSoup() {
        super(new Properties().group(ItemGroup.FOOD).food(new Food.Builder().build()).maxStackSize(1));
    }

    @Override
    public ItemStack onItemUseFinish(ItemStack stack, World w, LivingEntity liv) {
        super.onItemUseFinish(stack, w, liv);
        return new ItemStack(Items.BOWL);
    }

    @Override
    public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt) {
        getTagCompound(stack);
        return null;
    }

    @OnlyIn(Dist.CLIENT)
    @Override
    public void addInformation(ItemStack stack, World w, List<ITextComponent> lore, ITooltipFlag flag) {
        String text = getLocalizedIngredientList(stack);
        if(!text.isEmpty()) {
            lore.add(new StringTextComponent(text));
        }
    }

    private static CompoundNBT getTagCompound(ItemStack stack) {
        CompoundNBT com = stack.getTag();
        if(com == null) {
            com = new CompoundNBT();
            com.putInt("color", CookingPotColorMixer.DEFAULT.getRGB());
            com.put("ingredients", new ListNBT());
            com.putFloat("saturation", 0.0f);
            com.putInt("food", 0);
            stack.setTag(com);
        } else {
            if(!com.contains("color")) {
                com.putInt("color", CookingPotColorMixer.DEFAULT.getRGB());
            }
            if(!com.contains("ingredients")) {
                com.put("ingredients", new ListNBT());
            }
            if(!com.contains("saturation")) {
                com.putFloat("saturation", 0);
            }
            if(!com.contains("food")) {
                com.putInt("food", 0);
            }
        }
        return com;
    }

    @SuppressWarnings(value = "")
    public static void addIngredient(ItemStack stew, Iterable<ItemStack> stacks) {
        CompoundNBT com = stew.getTag();
        ListNBT list = com.getList("ingredients", 8);
        for(ItemStack stack : stacks) {
            list.add(StringNBT.valueOf(stack.getTranslationKey()));
        }
    }

    @OnlyIn(Dist.CLIENT)
    @SuppressWarnings(value = "")
    private String getLocalizedIngredientList(ItemStack stack) {
        CompoundNBT com = stack.getTag();
        ListNBT list = com.getList("ingredients", 8);
        switch(list.size()) {
            case 0:
                return "";
            case 1:
                return TextFormatting.GRAY + LanguageMap.getInstance().translateKey(list.getString(0));
            default:
                StringBuilder sb = new StringBuilder(TextFormatting.GRAY.toString());
                sb.append(LanguageMap.getInstance().translateKey(list.getString(0)));
                for(int i = 1; i < list.size(); i++) {
                    sb.append(", ");
                    sb.append(LanguageMap.getInstance().translateKey(list.getString(i)));
                }
                return sb.toString();
        }
    }

    public static void setColor(ItemStack stack, int color) {
        getTagCompound(stack).putInt("color", color);
    }

    public static int getColor(ItemStack stack) {
        return getTagCompound(stack).getInt("color");
    }

    public static void setFoodStats(ItemStack soup, LinkedList<ItemStack> ingredients) {
        float saturation = 0.0f;
        int food = 0;

        for(ItemStack stack : ingredients) {
            Item item = stack.getItem();
            if(item == Item.getItemFromBlock(Blocks.PUMPKIN)) {
                food += 6;
                saturation += 0.3F;
            } else if(item == Items.APPLE) {
                food += 4;
                saturation += 0.3F;
            } else if(item == Items.GOLDEN_APPLE) {
                food += 4;
                saturation += 1.2F;
            } else if(item == Items.BEETROOT_SEEDS || item == Items.MELON_SEEDS || item == Items.PUMPKIN_SEEDS || item == Items.WHEAT_SEEDS) {
                food += 1;
                saturation += 0.1F;
            } else if(item == Items.BEEF || item == Items.PORKCHOP) {
                food += 8;
                saturation += 0.8F;
            } else if(item == Items.TROPICAL_FISH) {
                food += 1;
                saturation += 0.1F;
            } else if(item == Items.COD) {
                food += 5;
                saturation += 0.6F;
            } else if(item == Items.PUFFERFISH) {
                food -= 75;
                saturation -= 0.8F;
            } else if(item == Items.SALMON) {
                food += 6;
                saturation += 0.8F;
            } else if(item == Items.SUGAR) {
                food += 1;
                saturation += 0.1F;
            } else if(item == Items.MELON_SLICE || item == Items.GLISTERING_MELON_SLICE) {
                food += 2;
                saturation += 0.3F;
            } else if(item == Items.CHICKEN) {
                food += 6;
                saturation += 0.6F;
            } else if(item == Items.POISONOUS_POTATO || item == Items.SPIDER_EYE) {
                food += -25;
                saturation -= 0.2F;
            } else if(item == Items.RABBIT_FOOT || item == Items.NETHER_WART || item == Items.FERMENTED_SPIDER_EYE) {
                food += 1;
                saturation += 0.1F;
            } else if(item == Items.GOLDEN_CARROT || item == Items.CARROT) {
                food += 3;
                saturation += 0.6F;
            } else if(item == Items.RABBIT || item == Items.POTATO) {
                food += 5;
                saturation += 0.6F;
            } else if(item == Items.MUTTON) {
                food += 6;
                saturation += 0.8F;
            } else if(item == Items.BEETROOT) {
                food += 1;
                saturation += 0.6F;
            }
        }

        getTagCompound(soup).putFloat("saturation", saturation);
        getTagCompound(soup).putInt("food", food);
    }

    @Override
    public void onFoodEaten(ItemStack stack, World w, LivingEntity liv) {
        int food = getTagCompound(stack).getInt("food");
        if(food < 0 && !w.isRemote) {
            liv.addPotionEffect(new EffectInstance(Effects.POISON, 100, -food / 25));
            food = 0;
        }
        float saturation = getTagCompound(stack).getFloat("saturation");
        if(liv instanceof PlayerEntity && !w.isRemote) {
            ((PlayerEntity) liv).getFoodStats().addStats(food, saturation);
        }
    }
}
