package info.sleeplessacorn.morechisels.util;

/*
 *  Copyright 2017 Sleepless Acorn
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import info.sleeplessacorn.morechisels.MoreChisels;
import info.sleeplessacorn.morechisels.chisel.ItemChiselOreDict;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.color.ItemColors;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.OreDictionary;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.HashMap;
import java.util.Map;

@SideOnly(Side.CLIENT)
public class ColorHandler {

    public static final Map<String, Integer> ORE_COLORS = new HashMap<>();

    public void registerColorHandler() {
        Minecraft mc = Minecraft.getMinecraft();
        ((IReloadableResourceManager) mc.getResourceManager())
                .registerReloadListener(resourceManager -> {
                    for (String ingot : OreDictHelper.getAllFromPrefix("ingot"))
                        ORE_COLORS.put(ingot, getOreColor(ingot));
                    for (String gem : OreDictHelper.getAllFromPrefix("gem"))
                        ORE_COLORS.put(gem, getOreColor(gem));

                });
        for (Item chisel : ChiselRegistrar.CHISELS) {
            if (chisel instanceof ItemChiselOreDict)
                registerChiselColor((ItemChiselOreDict) chisel);
        }
    }

    public void registerChiselColor(ItemChiselOreDict chisel) {
        ItemColors colors = Minecraft.getMinecraft().getItemColors();
        colors.registerItemColorHandler((stack, tintIndex) -> {
            boolean isHead = tintIndex == 1;
            if (ORE_COLORS.containsKey(chisel.getOreDict())) {
                return isHead ? ORE_COLORS.get(chisel.getOreDict()) : -1;
            }
            else {
                // This else statement should never be reached, and if it is, you done fucked up, Kit
                String msg = "Could not find a cached color value for <{}>, generating a new one...";
                MoreChisels.LOGGER.warn(msg, chisel.getOreDict());
                ORE_COLORS.put(chisel.getOreDict(), getOreColor(chisel.getOreDict()));
            }
            return -1;
        }, chisel);
    }

    public int getOreColor(String oredict) {
        if (OreDictionary.getOres(oredict).size() > 0) {
            ItemStack stack = OreDictionary.getOres(oredict).get(0);
            IBakedModel model = Minecraft.getMinecraft().getRenderItem()
                    .getItemModelWithOverrides(stack, null, null);
            TextureAtlasSprite sprite = model.getParticleTexture();
            int[] pixels = sprite.getFrameTextureData(0)[0];
            int r = 0, g = 0, b = 0, count = 0;
            for (int argb : pixels) {
                int ca = argb >> 24 & 0xFF;
                int cr = argb >> 16 & 0xFF;
                int cg = argb >> 8 & 0xFF;
                int cb = argb & 0xFF;
                if (ca > 0x7F && NumberUtils.max(cr, cg, cb) > 0x1F) {
                    r += cr;
                    g += cg;
                    b += cb;
                    count++;
                }
            }
            if (count > 0) {
                r /= count;
                g /= count;
                b /= count;
            }
            return 0xFF000000 | r << 16 | g << 8 | b;
        } else return 0xFF000000;
    }

}

@@ -2606,28 +2606,16 @@
        %7D
-%0A           
  else %7B%0A
@@ -3207,24 +3207,150 @@
 get(0);%0A    
+        return getStackColor(stack);%0A        %7D else return 0xFF000000;%0A    %7D%0A%0A    public int getStackColor(ItemStack stack) %7B%0A
         IBak
@@ -3422,20 +3422,16 @@
         
-    
 .getItem
@@ -3477,20 +3477,16 @@
         
-  
-  
 TextureA
@@ -3541,20 +3541,16 @@
         
-    
 int%5B%5D pi
@@ -3594,28 +3594,24 @@
 0%5D;%0A        
-  
-  
 int r = 0, g
@@ -3638,28 +3638,24 @@
  0;%0A        
-    
 for (int arg
@@ -3676,28 +3676,24 @@
             
-  
-  
 int ca = arg
@@ -3700,36 +3700,32 @@
 b %3E%3E 24 & 0xFF;%0A
-    
             int 
@@ -3756,28 +3756,24 @@
             
- 
-   
 int cg = arg
@@ -3795,28 +3795,24 @@
             
-    
 int cb = arg
@@ -3829,28 +3829,24 @@
             
- 
-   
 if (ca %3E 0x7
@@ -3904,20 +3904,16 @@
         
-    
 r += cr;
@@ -3929,20 +3929,16 @@
         
- 
-   
 g += cg;
@@ -3938,20 +3938,16 @@
  += cg;%0A
-    
         
@@ -3979,20 +3979,16 @@
         
-    
 count++;
@@ -3988,20 +3988,16 @@
 ount++;%0A
-    
         
@@ -4006,34 +4006,26 @@
   %7D%0A        
-    %7D%0A    
+%7D%0A
         if (
@@ -4049,20 +4049,16 @@
         
-    
 r /= cou
@@ -4053,36 +4053,32 @@
     r /= count;%0A
-    
             g /=
@@ -4097,20 +4097,16 @@
         
-    
 b /= cou
@@ -4113,34 +4113,26 @@
 nt;%0A        
-    %7D%0A    
+%7D%0A
         retu
@@ -4173,42 +4173,8 @@
  b;%0A
-        %7D else return 0xFF000000;%0A
     

Refactor color methods