package client.renderer.texture;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import client.util.FileUtils;
import common.color.DyeColor;
import common.log.Log;

public class LayeredColorMaskTexture extends Texture
{
    private final String texture;
    private final List<String> layers;
    private final List<DyeColor> colors;
    
    private static int applyTint(int color, int tint) {
        int i = (color & 16711680) >> 16;
        int j = (tint & 16711680) >> 16;
        int k = (color & 65280) >> 8;
        int l = (tint & 65280) >> 8;
        int i1 = (color & 255) >> 0;
        int j1 = (tint & 255) >> 0;
        int k1 = (int)((float)i * (float)j / 255.0F);
        int l1 = (int)((float)k * (float)l / 255.0F);
        int i2 = (int)((float)i1 * (float)j1 / 255.0F);
        return color & -16777216 | k1 << 16 | l1 << 8 | i2;
    }

    public LayeredColorMaskTexture(String texture, List<String> layers, List<DyeColor> colors)
    {
        this.texture = texture;
        this.layers = layers;
        this.colors = colors;
    }

    public void loadTexture() throws IOException
    {
        this.deleteGlTexture();
        BufferedImage bufferedimage;

        try
        {
            BufferedImage bufferedimage1 = TextureUtil.readImage(FileUtils.getResource(this.texture));
            int i = bufferedimage1.getType();

            if (i == 0)
            {
                i = 6;
            }

            bufferedimage = new BufferedImage(bufferedimage1.getWidth(), bufferedimage1.getHeight(), i);
            Graphics graphics = bufferedimage.getGraphics();
            graphics.drawImage(bufferedimage1, 0, 0, (ImageObserver)null);

            for (int j = 0; j < 17 && j < this.layers.size() && j < this.colors.size(); ++j)
            {
                String s = this.layers.get(j);
                int color = this.colors.get(j).getColor();

                if (s != null)
                {
                    InputStream inputstream = FileUtils.getResource(s);
                    BufferedImage bufferedimage2 = TextureUtil.readImage(inputstream);

                    if (bufferedimage2.getWidth() == bufferedimage.getWidth() && bufferedimage2.getHeight() == bufferedimage.getHeight() && bufferedimage2.getType() == 6)
                    {
                        for (int k = 0; k < bufferedimage2.getHeight(); ++k)
                        {
                            for (int l = 0; l < bufferedimage2.getWidth(); ++l)
                            {
                                int i1 = bufferedimage2.getRGB(l, k);

                                if ((i1 & -16777216) != 0)
                                {
                                    int j1 = (i1 & 16711680) << 8 & -16777216;
                                    int k1 = bufferedimage1.getRGB(l, k);
                                    int l1 = applyTint(k1, color) & 16777215;
                                    bufferedimage2.setRGB(l, k, j1 | l1);
                                }
                            }
                        }

                        bufferedimage.getGraphics().drawImage(bufferedimage2, 0, 0, (ImageObserver)null);
                    }
                }
            }
        }
        catch (IOException ioexception)
        {
        	Log.IO.error((Throwable)ioexception, (String)"Konnte Bild mit mehreren Schichten nicht laden");
            return;
        }

        TextureUtil.uploadImage(this.getGlTextureId(), bufferedimage);
    }
}
