package net.minecraft.client.renderer.texture;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.ITickable;
import net.minecraft.client.renderer.texture.ITickableTextureObject;
import net.minecraft.client.renderer.texture.SimpleTexture;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.src.Config;
import net.minecraft.src.RandomMobs;
import net.minecraft.util.ReportedException;
import net.minecraft.util.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import shadersmod.client.ShadersTex;

public class TextureManager implements ITickable, IResourceManagerReloadListener {

   private static final Logger field_147646_a = LogManager.getLogger();
   private final Map field_110585_a = Maps.newHashMap();
   private final List field_110583_b = Lists.newArrayList();
   private final Map field_110584_c = Maps.newHashMap();
   private IResourceManager field_110582_d;
   private static final String __OBFID = "CL_00001064";
   private static final String __OBFID = "CL_00001064";


   public TextureManager(IResourceManager p_i1284_1_) {
      this.field_110582_d = p_i1284_1_;
   }

   public void func_110577_a(ResourceLocation resource) {
      if(Config.isRandomMobs()) {
         resource = RandomMobs.getTextureLocation(resource);
      }

      Object var2 = (ITextureObject)this.field_110585_a.get(resource);
      if(var2 == null) {
         var2 = new SimpleTexture(resource);
         this.func_110579_a(resource, (ITextureObject)var2);
      }

      if(Config.isShaders()) {
         ShadersTex.bindTexture((ITextureObject)var2);
      } else {
         TextureUtil.func_94277_a(((ITextureObject)var2).func_110552_b());
      }

   }

   public boolean func_110580_a(ResourceLocation p_110580_1_, ITickableTextureObject p_110580_2_) {
      if(this.func_110579_a(p_110580_1_, p_110580_2_)) {
         this.field_110583_b.add(p_110580_2_);
         return true;
      } else {
         return false;
      }
   }

   public boolean func_110579_a(ResourceLocation p_110579_1_, final ITextureObject p_110579_2_) {
      boolean var3 = true;
      Object p_110579_2_2 = p_110579_2_;

      try {
         p_110579_2_.func_110551_a(this.field_110582_d);
      } catch (IOException var8) {
         field_147646_a.warn("Failed to load texture: " + p_110579_1_, var8);
         p_110579_2_2 = TextureUtil.field_111001_a;
         this.field_110585_a.put(p_110579_1_, p_110579_2_2);
         var3 = false;
      } catch (Throwable var9) {
         CrashReport var5 = CrashReport.func_85055_a(var9, "Registering texture");
         CrashReportCategory var6 = var5.func_85058_a("Resource location being registered");
         var6.func_71507_a("Resource location", p_110579_1_);
         var6.func_71500_a("Texture object class", new Callable() {

            private static final String __OBFID = "CL_00001065";
            private static final String __OBFID = "CL_00001065";

            public String call() {
               return p_110579_2_.getClass().getName();
            }
            // $FF: synthetic method
            // $FF: bridge method
            public Object call() throws Exception {
               return this.call();
            }
         });
         throw new ReportedException(var5);
      }

      this.field_110585_a.put(p_110579_1_, p_110579_2_2);
      return var3;
   }

   public ITextureObject func_110581_b(ResourceLocation p_110581_1_) {
      return (ITextureObject)this.field_110585_a.get(p_110581_1_);
   }

   public ResourceLocation func_110578_a(String p_110578_1_, DynamicTexture p_110578_2_) {
      Integer var3 = (Integer)this.field_110584_c.get(p_110578_1_);
      if(var3 == null) {
         var3 = Integer.valueOf(1);
      } else {
         var3 = Integer.valueOf(var3.intValue() + 1);
      }

      this.field_110584_c.put(p_110578_1_, var3);
      ResourceLocation var4 = new ResourceLocation(String.format("dynamic/%s_%d", new Object[]{p_110578_1_, var3}));
      this.func_110579_a(var4, p_110578_2_);
      return var4;
   }

   public void func_110550_d() {
      Iterator var1 = this.field_110583_b.iterator();

      while(var1.hasNext()) {
         ITickable var2 = (ITickable)var1.next();
         var2.func_110550_d();
      }

   }

   public void func_147645_c(ResourceLocation p_147645_1_) {
      ITextureObject var2 = this.func_110581_b(p_147645_1_);
      if(var2 != null) {
         TextureUtil.func_147942_a(var2.func_110552_b());
      }

   }

   public void func_110549_a(IResourceManager resourceManager) {
      Config.dbg("*** Reloading textures ***");
      Config.log("Resource packs: " + Config.getResourcePackNames());
      Iterator it = this.field_110585_a.keySet().iterator();

      while(it.hasNext()) {
         ResourceLocation var2 = (ResourceLocation)it.next();
         if(var2.func_110623_a().startsWith("mcpatcher/")) {
            ITextureObject var3 = (ITextureObject)this.field_110585_a.get(var2);
            if(var3 instanceof AbstractTexture) {
               AbstractTexture at = (AbstractTexture)var3;
               at.func_147631_c();
            }

            it.remove();
         }
      }

      Iterator var21 = this.field_110585_a.entrySet().iterator();

      while(var21.hasNext()) {
         Entry var31 = (Entry)var21.next();
         this.func_110579_a((ResourceLocation)var31.getKey(), (ITextureObject)var31.getValue());
      }

   }

}
