package me.km.blocks.cookingpot;

import java.awt.Color;
import java.util.LinkedList;
import net.minecraft.item.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class CookingPotColorMixer {
    public final static Color DEFAULT = new Color(47, 67, 244);

    private final static Color POISON = new Color(0, 128, 43);

    private final static Color PUMPKIN = new Color(197, 120, 23, 255);
    private final static Color APPLE = new Color(195, 30, 38);
    private final static Color APPLE_GOLDEN = new Color(164, 149, 56);
    private final static Color BEEF_RAW = new Color(155, 48, 42);
    private final static Color BEETROOT = new Color(144, 86, 69);
    private final static Color BEETROOT_SEEDS = new Color(229, 221, 210);
    //private final static Color BLAZE_POWDER = new Color(183, 100, 15); 
    private final static Color CARROT = new Color(144, 121, 29);
    private final static Color CARROT_GOLDEN = new Color(129, 104, 26);
    private final static Color CHICKEN_RAW = new Color(191, 148, 135);
    private final static Color FISH_CLOWNFISH_RAW = new Color(216, 170, 143, 255);
    private final static Color FISH_COD_RAW = new Color(163, 180, 180);
    private final static Color FISH_SALMON_RAW = new Color(207, 188, 186, 255);
    //private final static Color MAGMA_CREAM = new Color(168, 118, 55);    
    private final static Color MELON = new Color(155, 90, 46);
    private final static Color MELON_SPECKLED = new Color(173, 114, 62);
    private final static Color MUTTON_RAW = new Color(175, 60, 53);
    private final static Color NETHER_WART = new Color(115, 27, 34, 255);
    private final static Color PORKCHOP_RAW = new Color(202, 114, 114);
    private final static Color POTATO = new Color(206, 169, 91);
    private final static Color RABBIT_FOOT = new Color(169, 130, 86);
    private final static Color RABBIT_RAW = new Color(213, 170, 155);
    private final static Color SEEDS_MELON = new Color(57, 48, 23);
    private final static Color SEEDS_PUMPKIN = new Color(201, 199, 146);
    private final static Color SEEDS_WHEAT = new Color(61, 134, 27);
    private final static Color SPIDER_EYE_FERMENTED = new Color(117, 44, 54);
    private final static Color SUGAR = new Color(185, 185, 195);
    //private final static Color WHEAT = new Color(122, 125, 36);    

    private final LinkedList<ItemStack> items;
    private final LinkedList<Color> colors;
    private Color cachedColor;

    public CookingPotColorMixer() {
        items = new LinkedList<>();
        colors = new LinkedList<>();
        reset();
    }

    public final void reset(Color c) {
        cachedColor = c;
        colors.clear();
        items.clear();
    }

    public final void reset() {
        reset(DEFAULT);
    }

    public LinkedList<ItemStack> getItems() {
        return items;
    }

    public Color getItemColor(ItemStack stack) {
        Item item = stack.getItem();
        if(item == Items.PUMPKIN) {
            return PUMPKIN;
        } else if(item == Items.APPLE) {
            return APPLE;
        } else if(item == Items.GOLDEN_APPLE) {
            return APPLE_GOLDEN;
        } else if(item == Items.BEETROOT_SEEDS) {
            return BEETROOT_SEEDS;
        } else if(item == Items.MELON_SEEDS) {
            return SEEDS_MELON;
        } else if(item == Items.PUMPKIN_SEEDS) {
            return SEEDS_PUMPKIN;
        } else if(item == Items.WHEAT_SEEDS) {
            return SEEDS_WHEAT;
        } else if(item == Items.PORKCHOP) {
            return PORKCHOP_RAW;
        } else if(item == Items.BEEF) {
            return BEEF_RAW;
        } else if(item == Items.TROPICAL_FISH) {
            return FISH_CLOWNFISH_RAW;
        } else if(item == Items.COD) {
            return FISH_COD_RAW;
        } else if(item == Items.PUFFERFISH) {
            return POISON;
        } else if(item == Items.SALMON) {
            return FISH_SALMON_RAW;
        } else if(item == Items.SUGAR) {
            return SUGAR;
        } else if(item == Items.MELON_SLICE) {
            return MELON;
        } else if(item == Items.GLISTERING_MELON_SLICE) {
            return MELON_SPECKLED;
        } else if(item == Items.CHICKEN) {
            return CHICKEN_RAW;
        } else if(item == Items.SPIDER_EYE) {
            return POISON;
        } else if(item == Items.POISONOUS_POTATO) {
            return POISON;
        } else if(item == Items.RABBIT_FOOT) {
            return RABBIT_FOOT;
        } else if(item == Items.NETHER_WART) {
            return NETHER_WART;
        } else if(item == Items.FERMENTED_SPIDER_EYE) {
            return SPIDER_EYE_FERMENTED;
        } else if(item == Items.GOLDEN_CARROT) {
            return CARROT_GOLDEN;
        } else if(item == Items.CARROT) {
            return CARROT;
        } else if(item == Items.RABBIT) {
            return RABBIT_RAW;
        } else if(item == Items.POTATO) {
            return POTATO;
        } else if(item == Items.MUTTON) {
            return MUTTON_RAW;
        } else if(item == Items.BEETROOT) {
            return BEETROOT;
        }
        return null;
    }

    public void addColor(ItemStack stack, Color c) {
        colors.add(c);
        ItemStack copy = stack.copy();
        copy.setCount(1);
        items.add(copy);
        cachedColor = mixColors();
    }

    public void cache() {
        if(items.isEmpty()) {
            colors.clear();
            cachedColor = DEFAULT;
            return;
        }
        if(colors.size() != items.size()) {
            colors.clear();
            Color c;
            for(ItemStack stack : items) {
                c = getItemColor(stack);
                if(c != null) {
                    colors.add(c);
                }
            }
        }
        cachedColor = mixColors();
    }

    public Color getMixedColor() {
        return cachedColor;
    }

    private Color mixColors() {
        int alpha = 0;
        int red = 0;
        int green = 0;
        int blue = 0;
        for(Color c : colors) {
            alpha += c.getAlpha();
            red += c.getRed();
            green += c.getGreen();
            blue += c.getBlue();
        }
        if(!colors.isEmpty()) {
            int size = colors.size();
            alpha /= size;
            red /= size;
            green /= size;
            blue /= size;
        }
        return new Color(red, green, blue, alpha);
    }
}
