using System;
using UnityEngine;

public class SmallImage
{
	public static int[][] smallImg;

	public static SmallImage instance;

	public static Image[] imgbig;

	public static Small[] imgNew = new Small[3000];

	public static MyVector vKeys = new MyVector("vKey");

	public static Image imgEmpty = null;

	public static sbyte[] newSmallVersion;

	public static void loadBigRMS()
	{
		if (mGraphics.zoomLevel != 1)
		{
			return;
		}
		if (imgbig == null)
		{
			imgbig = new Image[5]
			{
				CCanvas.loadImageRMS("/Big0.png"),
				CCanvas.loadImageRMS("/Big1.png"),
				CCanvas.loadImageRMS("/Big2.png"),
				CCanvas.loadImageRMS("/Big3.png"),
				CCanvas.loadImageRMS("/Big4.png")
			};
			loadBigImage();
			mSystem.gcc();
		}
	}

	public static void freeBig()
	{
		imgbig = null;
		mSystem.gcc();
	}

	public static void loadBigImage()
	{
		mSystem.gcc();
		imgEmpty = Image.createRGBImage(new int[1] { 8912896 }, 1, 1, true);
	}

	public static void init()
	{
		instance = null;
		instance = new SmallImage();
	}

	public void readData(sbyte[] data)
	{
	}

	public static void readImage(sbyte[] data)
	{
		try
		{
			DataInputStream dataInputStream = new DataInputStream(Rms.loadRMS("dataImage"));
			int num = dataInputStream.readShort();
			smallImg = new int[num][];
			for (int i = 0; i < smallImg.Length; i++)
			{
				smallImg[i] = new int[5];
			}
			for (int j = 0; j < num; j++)
			{
				smallImg[j][0] = dataInputStream.readUnsignedByte();
				smallImg[j][1] = dataInputStream.readShort();
				smallImg[j][2] = dataInputStream.readShort();
				smallImg[j][3] = dataInputStream.readShort();
				smallImg[j][4] = dataInputStream.readShort();
				if (smallImg[j][1] >= 256 || smallImg[j][3] >= 256 || smallImg[j][2] >= 256 || smallImg[j][4] >= 256)
				{
					smallImg[j] = null;
				}
			}
		}
		catch (Exception)
		{
		}
	}

	public static void clearHastable()
	{
		vKeys.removeAllElements();
		imgNew = new Small[3000];
	}

	public static void unload()
	{
		vKeys.removeAllElements();
		for (int i = 0; i < 3000; i++)
		{
			imgNew[i] = null;
		}
	}

	public static void createImage(int id)
	{
		if (mGraphics.zoomLevel == 1)
		{
			Image image = CCanvas.loadImage("/Small/Small" + id + ".png");
			if (image != null)
			{
				imgNew[id] = new Small(image, id);
				return;
			}
			imgNew[id] = new Small(imgEmpty, id);
			GameService.gI().requestIcon(id);
			return;
		}
		Image image2 = CCanvas.loadImage("/Small/Small" + id + ".png");
		if (image2 != null)
		{
			imgNew[id] = new Small(image2, id);
			return;
		}
		bool flag = false;
		sbyte[] array = Rms.loadRMS(mGraphics.zoomLevel + "Small" + id);
		if (array != null)
		{
			if (newSmallVersion != null && array.Length % 127 != newSmallVersion[id])
			{
				flag = true;
			}
			if (!flag)
			{
				Image image3 = Image.createImage(array, 0, array.Length);
				if (image3 != null)
				{
					imgNew[id] = new Small(image3, id);
				}
				else
				{
					flag = true;
				}
			}
		}
		else
		{
			flag = true;
		}
		if (flag)
		{
			imgNew[id] = new Small(imgEmpty, id);
			GameService.gI().requestIcon(id);
		}
	}

	public static void drawSmallImage(mGraphics g, int id, float x, float y, int transform, int anchor, bool isClip)
	{
		if (imgbig == null)
		{
			Small small = imgNew[id];
			if (small == null)
			{
				createImage(id);
			}
			else
			{
				g.drawRegion(small.img, 0, 0, mGraphics.getImageWidth(small.img), mGraphics.getImageHeight(small.img), transform, x, y, anchor, isClip);
			}
		}
		else if (smallImg != null)
		{
			if (id >= smallImg.Length || smallImg[id] == null || smallImg[id][1] >= mGraphics.getImageWidth(imgbig[smallImg[id][0]]) || smallImg[id][3] >= mGraphics.getImageWidth(imgbig[smallImg[id][0]]) || smallImg[id][2] >= mGraphics.getImageHeight(imgbig[smallImg[id][0]]) || smallImg[id][4] >= mGraphics.getImageHeight(imgbig[smallImg[id][0]]))
			{
				Small small2 = imgNew[id];
				if (small2 == null)
				{
					createImage(id);
				}
				else
				{
					small2.paint(g, transform, x, y, anchor, isClip);
				}
			}
			else if (imgbig[smallImg[id][0]] != null)
			{
				g.drawRegion(imgbig[smallImg[id][0]], smallImg[id][1], smallImg[id][2], smallImg[id][3], smallImg[id][4], transform, x, y, anchor, isClip);
			}
		}
		else if (CCanvas.curScr != CCanvas.gameScr)
		{
			Small small3 = imgNew[id];
			if (small3 == null)
			{
				createImage(id);
			}
			else
			{
				small3.paint(g, transform, x, y, anchor, isClip);
			}
		}
	}

	public static void requestImage(int id)
	{
		if (imgbig == null)
		{
			Small small = imgNew[id];
			if (small == null)
			{
				createImage(id);
			}
		}
		else if (smallImg != null)
		{
			if (id >= smallImg.Length || smallImg[id] == null || smallImg[id][1] >= mGraphics.getImageWidth(imgbig[smallImg[id][0]]) || smallImg[id][3] >= mGraphics.getImageWidth(imgbig[smallImg[id][0]]) || smallImg[id][2] >= mGraphics.getImageHeight(imgbig[smallImg[id][0]]) || smallImg[id][4] >= mGraphics.getImageHeight(imgbig[smallImg[id][0]]))
			{
				Small small2 = imgNew[id];
				if (small2 == null)
				{
					createImage(id);
				}
			}
		}
		else if (CCanvas.curScr != CCanvas.gameScr)
		{
			Small small3 = imgNew[id];
			if (small3 == null)
			{
				createImage(id);
			}
		}
	}

	public static void update()
	{
		int num = 0;
		if (CCanvas.gameTick % 400 == 0)
		{
			for (int i = 0; i < imgNew.Length; i++)
			{
				if (imgNew[i] != null)
				{
					num++;
					imgNew[i].update();
				}
			}
		}
		if (num > 200 && BackgroundNew.lowGraphic)
		{
			imgNew = new Small[3000];
		}
	}
}
