using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class InfinityColorTextureCache
{
	public InfinityColorTextureCache(Maid maid)
	{
		this.maid_ = maid;
		if (InfinityColorTextureCache.render_cache_ == null)
		{
			InfinityColorTextureCache.render_cache_ = new RenderTextureCache();
		}
	}

	public static RenderTextureCache render_cache
	{
		get
		{
			if (InfinityColorTextureCache.render_cache_ == null)
			{
				InfinityColorTextureCache.render_cache_ = new RenderTextureCache();
			}
			return InfinityColorTextureCache.render_cache_;
		}
	}

	public void Update()
	{
		InfinityColorTextureCache.render_cache_.Update();
	}

	public void AddTexture(int mat_no, string prop_name, Texture base_tex)
	{
		if (base_tex == null)
		{
			return;
		}
		InfinityColorTextureCache.TextureSet textureSet = this.GetTextureSet(mat_no, prop_name, true);
		textureSet.base_tex = base_tex;
		textureSet.modified_tex = null;
		textureSet.parts_color = MaidParts.PARTS_COLOR.NONE;
	}

	public void AddTexture(int mat_no, string prop_name, Texture base_tex, MaidParts.PARTS_COLOR parts_color)
	{
		if (base_tex == null)
		{
			return;
		}
		if (parts_color <= MaidParts.PARTS_COLOR.NONE || MaidParts.PARTS_COLOR.MAX <= parts_color)
		{
			this.AddTexture(mat_no, prop_name, base_tex);
			return;
		}
		InfinityColorTextureCache.TextureSet textureSet = this.GetTextureSet(mat_no, prop_name, true);
		textureSet.base_tex = base_tex;
		textureSet.parts_color = parts_color;
		Action<RenderTexture> value = delegate(RenderTexture tex)
		{
			InfinityColorTextureCache.TextureSet textureSet2 = null;
			foreach (KeyValuePair<int, Dictionary<string, InfinityColorTextureCache.TextureSet>> keyValuePair in this.tex_dic_)
			{
				Dictionary<string, InfinityColorTextureCache.TextureSet> value2 = keyValuePair.Value;
				foreach (KeyValuePair<string, InfinityColorTextureCache.TextureSet> keyValuePair2 in value2)
				{
					if (!(keyValuePair2.Value.modified_tex != tex))
					{
						textureSet2 = keyValuePair2.Value;
					}
				}
			}
			if (textureSet2 != null)
			{
				this.UpdateTexture(textureSet2.base_tex, textureSet2.parts_color, tex);
			}
		};
		textureSet.modified_tex = InfinityColorTextureCache.render_cache_.GetTexture(textureSet.base_tex.width, textureSet.base_tex.height, RenderTextureFormat.ARGB32, new KeyValuePair<int, Action<RenderTexture>>(10, value));
		this.UpdateTexture(textureSet.base_tex, textureSet.parts_color, textureSet.modified_tex);
	}

	public void RemoveTexture(int mat_no, string prop_name)
	{
		if (!this.tex_dic_.ContainsKey(mat_no))
		{
			return;
		}
		Dictionary<string, InfinityColorTextureCache.TextureSet> dictionary = this.tex_dic_[mat_no];
		if (!dictionary.ContainsKey(prop_name))
		{
			return;
		}
		InfinityColorTextureCache.TextureSet textureSet = dictionary[prop_name];
		if (textureSet == null)
		{
			return;
		}
		if (textureSet.base_tex != null)
		{
			if (textureSet.base_tex is RenderTexture)
			{
				InfinityColorTextureCache.render_cache_.RemoveTexture(textureSet.base_tex as RenderTexture);
			}
			else
			{
				UnityEngine.Object.Destroy(textureSet.base_tex);
			}
		}
		if (textureSet.modified_tex != null)
		{
			InfinityColorTextureCache.render_cache_.RemoveTexture(textureSet.modified_tex);
		}
		dictionary.Remove(prop_name);
		if (dictionary.Count <= 0)
		{
			this.tex_dic_.Remove(mat_no);
		}
	}

	public void RemoveTexture()
	{
		foreach (KeyValuePair<int, Dictionary<string, InfinityColorTextureCache.TextureSet>> keyValuePair in this.tex_dic_)
		{
			foreach (KeyValuePair<string, InfinityColorTextureCache.TextureSet> keyValuePair2 in keyValuePair.Value)
			{
				InfinityColorTextureCache.TextureSet value = keyValuePair2.Value;
				if (value != null)
				{
					if (value.base_tex != null)
					{
						if (value.base_tex is RenderTexture)
						{
							InfinityColorTextureCache.render_cache_.RemoveTexture(value.base_tex as RenderTexture);
						}
						else
						{
							UnityEngine.Object.Destroy(value.base_tex);
						}
					}
					if (value.modified_tex != null)
					{
						InfinityColorTextureCache.render_cache_.RemoveTexture(value.modified_tex);
					}
				}
			}
		}
		this.tex_dic_.Clear();
	}

	public bool UpdateTexture(int mat_no, string prop_name)
	{
		InfinityColorTextureCache.TextureSet textureSet = this.GetTextureSet(mat_no, prop_name, false);
		return this.UpdateTexture(textureSet.base_tex, textureSet.parts_color, textureSet.modified_tex);
	}

	public bool UpdateTexture(Texture base_tex, MaidParts.PARTS_COLOR parts_color, RenderTexture target_tex)
	{
		if (base_tex == null || target_tex == null)
		{
			return false;
		}
		if (parts_color <= MaidParts.PARTS_COLOR.NONE || MaidParts.PARTS_COLOR.MAX <= parts_color)
		{
			return false;
		}
		RenderTexture active = RenderTexture.active;
		Material systemMaterial = GameUty.GetSystemMaterial(GameUty.SystemMaterial.InfinityColor);
		systemMaterial.SetTexture("_MultiColTex", this.maid_.Parts.GetPartsColorTableTex(parts_color));
		Graphics.Blit(base_tex, target_tex, systemMaterial);
		RenderTexture.active = active;
		return true;
	}

	public bool UpdateColor(MaidParts.PARTS_COLOR parts_color)
	{
		if (parts_color <= MaidParts.PARTS_COLOR.NONE || MaidParts.PARTS_COLOR.MAX <= parts_color)
		{
			return false;
		}
		bool result = false;
		foreach (KeyValuePair<int, Dictionary<string, InfinityColorTextureCache.TextureSet>> keyValuePair in this.tex_dic_)
		{
			Dictionary<string, InfinityColorTextureCache.TextureSet> value = keyValuePair.Value;
			foreach (KeyValuePair<string, InfinityColorTextureCache.TextureSet> keyValuePair2 in value)
			{
				InfinityColorTextureCache.TextureSet value2 = keyValuePair2.Value;
				if (value2.parts_color == parts_color && value2.base_tex != null && value2.modified_tex != null)
				{
					result = true;
					this.UpdateTexture(value2.base_tex, value2.parts_color, value2.modified_tex);
				}
			}
		}
		return result;
	}

	public Texture GetModifiedTexture(int mat_no, string prop_name)
	{
		InfinityColorTextureCache.TextureSet textureSet = this.GetTextureSet(mat_no, prop_name, false);
		if (textureSet == null)
		{
			return null;
		}
		return (!(textureSet.modified_tex != null)) ? textureSet.base_tex : textureSet.modified_tex;
	}

	private InfinityColorTextureCache.TextureSet GetTextureSet(int mat_no, string prop_name, bool create_tex_set)
	{
		if (!this.tex_dic_.ContainsKey(mat_no))
		{
			if (!create_tex_set)
			{
				return null;
			}
			this.tex_dic_.Add(mat_no, new Dictionary<string, InfinityColorTextureCache.TextureSet>());
		}
		Dictionary<string, InfinityColorTextureCache.TextureSet> dictionary = this.tex_dic_[mat_no];
		if (!dictionary.ContainsKey(prop_name))
		{
			if (!create_tex_set)
			{
				return null;
			}
			dictionary.Add(prop_name, new InfinityColorTextureCache.TextureSet());
		}
		return dictionary[prop_name];
	}

	public static string GetTimeFileName()
	{
		string fullPath = Path.GetFullPath(".\\");
		string text = fullPath + "ScreenShot/_tmp";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		return text + "\\";
	}

	private static RenderTextureCache render_cache_;

	private Maid maid_;

	private Dictionary<int, Dictionary<string, InfinityColorTextureCache.TextureSet>> tex_dic_ = new Dictionary<int, Dictionary<string, InfinityColorTextureCache.TextureSet>>();

	public class TextureSet
	{
		public Texture base_tex;

		public RenderTexture modified_tex;

		public MaidParts.PARTS_COLOR parts_color;
	}
}
