using System;

public class MM
{
	public const sbyte WATER = 0;

	public const sbyte GLASS = 1;

	public const sbyte GLASS_2 = 2;

	public const sbyte WATERBUM_SMALL_THING = 0;

	public const sbyte WATERBUM_NORMAL_THING = 1;

	public const sbyte WATERBUM_BIG_THING = 2;

	public static int mapWidth;

	public static int mapHeight;

	public static MyVector maps = new MyVector();

	public static MyVector mapFiles = new MyVector();

	private int index;

	public static bool isHaveWaterOrGlass;

	public static sbyte curWaterType;

	public int mapID;

	private CMap cur;

	public static int NUM_MAP;

	public static string[] mapFileName;

	public static MyVector vHoleInfo = new MyVector();

	private int count1;

	private int count2;

	public static MyVector mapImages = new MyVector();

	public static short[] undestroyTile;

	public static int[] color = new int[116]
	{
		11899466, 11899466, 11899466, 4887114, 4887114, 12171116, 9624575, 9624575, 14077060, 14077060,
		14077060, 14077060, 14077060, 14077060, 7104880, 14285647, 11244361, 12566463, 14399049, 14399049,
		9013950, 9013950, 9013950, 2965409, 2965409, 2965409, 2965409, 2121992, 2121992, 2121992,
		9581312, 9581312, 9581312, 0, 0, 0, 14192388, 11645103, 6186362, 6186362,
		6186362, 6186362, 6186362, 6186362, 6186362, 6186362, 50175, 50175, 50175, 50175,
		50175, 50175, 50175, 16777215, 9722194, 9722194, 9722194, 9722194, 9722194, 9722194,
		9722194, 9722194, 9722194, 6710886, 6710886, 6710886, 6710886, 6710886, 0, 12617122,
		12617122, 12617122, 12617122, 12617122, 12617122, 12617122, 6186362, 6186362, 6186362, 16762447,
		6112568, 6112568, 6112568, 6112568, 6112568, 0, 0, 0, 0, 0,
		16777215, 16777215, 11908292, 11908292, 11908292, 11908292, 5649460, 6908265, 6908265, 9854045,
		9854045, 9854045, 3429946, 13494259, 13494259, 13494259, 13494259, 13494259, 13494259, 13494259,
		13494259, 13494259, 8421631, 16744591, 16744591, 8421631
	};

	public static Image rock1;

	public static Image rock2;

	public void createMap(int mapID)
	{
		isHaveWaterOrGlass = false;
		this.mapID = mapID;
		loadMapFile(mapID);
	}

	public void createBackGround()
	{
	}

	public void clearBackGround()
	{
		mapFiles.removeAllElements();
	}

	public sbyte[] getDataByID(int id)
	{
		sbyte[] result = null;
		if (mapFiles != null)
		{
			CRes.outz("!null");
		}
		else
		{
			CRes.outz("null");
		}
		CRes.outz("map file size= " + mapFiles.size());
		for (int i = 0; i < mapFiles.size(); i++)
		{
			MapFile mapFile = (MapFile)mapFiles.elementAt(i);
			if (mapFile.mapID == id)
			{
				result = mapFile.data;
			}
		}
		return result;
	}

	public static MapFile getMapFileByIndex(int idex)
	{
		for (int i = 0; i < mapFiles.size(); i++)
		{
			MapFile result = (MapFile)mapFiles.elementAt(i);
			if (i == idex)
			{
				return result;
			}
		}
		return null;
	}

	public static MapFile getMapFileByID(int id)
	{
		for (int i = 0; i < mapFiles.size(); i++)
		{
			MapFile mapFile = (MapFile)mapFiles.elementAt(i);
			if (mapFile.mapID == id)
			{
				return mapFile;
			}
		}
		return null;
	}

	public bool isTileDestroy(int id)
	{
		for (int i = 0; i < undestroyTile.Length; i++)
		{
			if (id == undestroyTile[i])
			{
				return true;
			}
		}
		return false;
	}

	public void addImage(int id, Image image)
	{
		for (int i = 0; i < maps.size(); i++)
		{
			CMap cMap = (CMap)maps.elementAt(i);
			if (cMap.id == id)
			{
				cMap.createRGB(image);
			}
		}
		mSystem.gcc();
		count2++;
		CRes.outz("count 1= " + count1 + " count 2= " + count2);
		if (count2 == count1)
		{
			if (PrepareScr.currLevel == 5)
			{
				GameService.gI().startGame();
			}
			if (MyMidlet.trainingSuccess && !MenuScr.isTraining && PrepareScr.currLevel != 5)
			{
				CCanvas.endDlg();
				InfoDlg.hide();
				if (CCanvas.curScr != CCanvas.prepareScr)
				{
					CCanvas.prepareScr.show();
				}
			}
		}
		mapImages.addElement(new MapImage(image, id));
		int num = ((!BackgroundNew.lowGraphic) ? 30 : 5);
		if (mapImages.size() >= num)
		{
			while (mapImages.size() > num)
			{
				mapImages.removeElementAt(0);
			}
		}
		CRes.outz("Image map size= " + mapImages.size());
	}

	public bool containsImage(int id)
	{
		for (int i = 0; i < mapImages.size(); i++)
		{
			MapImage mapImage = (MapImage)mapImages.elementAt(i);
			if (mapImage.id == id)
			{
				return true;
			}
		}
		return false;
	}

	public Image getImage(int id)
	{
		for (int i = 0; i < mapImages.size(); i++)
		{
			MapImage mapImage = (MapImage)mapImages.elementAt(i);
			if (mapImage.id == id)
			{
				return mapImage.image;
			}
		}
		return null;
	}

	public static bool isExistId(int id)
	{
		for (int i = 0; i < maps.size(); i++)
		{
			CMap cMap = (CMap)maps.elementAt(i);
			if (cMap.aMap != null && cMap.id == id)
			{
				return true;
			}
		}
		return false;
	}

	public static int[] rgbMap(int id)
	{
		for (int i = 0; i < maps.size(); i++)
		{
			CMap cMap = (CMap)maps.elementAt(i);
			if (cMap.id == id)
			{
				return cMap.aMap;
			}
		}
		return null;
	}

	private void loadMapFile(int id)
	{
		sbyte[] dataByID = getDataByID(id);
		CRes.outz("########## " + dataByID.Length);
		DataInputStream dataInputStream = new DataInputStream(dataByID);
		count1 = 0;
		count2 = 0;
		mapWidth = dataInputStream.readShort();
		mapHeight = dataInputStream.readShort();
		BackgroundNew.yfog = mapHeight - 160;
		CRes.outz("2 map w= " + mapWidth + " mapHeight=" + mapHeight);
		int num = dataInputStream.readUnsignedByte();
		CRes.outz("nLand= " + num);
		int[] array = new int[num];
		MyHashTable myHashTable = new MyHashTable();
		CMap cMap = null;
		for (int i = 0; i < num; i++)
		{
			array[i] = dataInputStream.readUnsignedByte();
			CRes.outz("i= " + i + " = " + array[i]);
			if (containsImage(array[i]))
			{
				cMap = new CMap(array[i], dataInputStream.readShort(), dataInputStream.readShort(), getImage(array[i]), !isTileDestroy(array[i]));
				cMap.index = i;
				addMap(cMap);
				continue;
			}
			cMap = new CMap(array[i], dataInputStream.readShort(), dataInputStream.readShort(), null, !isTileDestroy(array[i]));
			cMap.index = i;
			addMap(cMap);
			if (!myHashTable.containsKey(array[i] + string.Empty))
			{
				GameService.gI().getMaterialIcon((short)array[i]);
			}
			myHashTable.put(array[i] + string.Empty, new MapImage(null, array[i]));
		}
		dataByID = null;
		dataInputStream.close();
		if (myHashTable != null)
		{
			count1 = myHashTable.size();
		}
		myHashTable.clear();
		myHashTable = null;
		BackgroundNew.loadBG(getMapFileByID(mapID).bgID);
	}

	public void addMap(CMap m)
	{
		maps.addElement(m);
	}

	public CMap getMap(int i)
	{
		return (CMap)maps.elementAt(i);
	}

	public bool isLand(int x, int y)
	{
		for (int i = 0; i < maps.size(); i++)
		{
			cur = (CMap)maps.elementAt(i);
			if (CRes.inRect(x, y, cur.x, cur.y, cur.width, cur.height) && CRes.isLand(cur.getPixel(x - cur.x, y - cur.y)))
			{
				return true;
			}
		}
		return false;
	}

	public static bool checkWaterBum(int x, int y, sbyte bumType)
	{
		return false;
	}

	public static void loadImage()
	{
		rock1 = CCanvas.loadImage("/eff/rock1.png");
		rock2 = CCanvas.loadImage("/eff/rock2.png");
	}

	public void makeHole(int x, int y, sbyte bulletType)
	{
		CRes.outz("makehole:     x= " + x + " y=" + y);
		int holeW = CMap.getHoleW(bulletType);
		int holeH = CMap.getHoleH(bulletType);
		int holeW2 = CMap.getHoleW2(bulletType);
		int holeH2 = CMap.getHoleH2(bulletType);
		Smoke.vImageID.removeAllElements();
		Smoke.vImageIDF.removeAllElements();
		for (int i = 0; i < maps.size(); i++)
		{
			cur = (CMap)maps.elementAt(i);
			if (cur.isDestroy && CRes.isHit(x - holeW / 2, y - holeH / 2, holeW, holeH, cur.x, cur.y, cur.width, cur.height))
			{
				if (cur.id <= color.Length - 1 && !Smoke.isExistImgRock(cur.id) && color[cur.id - 1] != 0 && !BackgroundNew.lowGraphic)
				{
					Smoke.vImageID.addElement(new ImageRock(cur.id, mGraphics.blend(rock1, 0.6f, color[cur.id - 1])));
				}
				if (cur.id <= color.Length - 1 && !Smoke.isExistImgRockF(cur.id) && color[cur.id - 1] != 0 && !BackgroundNew.lowGraphic)
				{
					Smoke.vImageIDF.addElement(new ImageRock(cur.id, mGraphics.blend(rock2, 0.6f, color[cur.id - 1])));
				}
				((CMap)maps.elementAt(i)).makeHole(x, y, bulletType);
			}
		}
		for (int j = 0; j < maps.size(); j++)
		{
			cur = (CMap)maps.elementAt(j);
			if (cur.isDestroy && CRes.isHit(x - holeW2 / 2, y - holeH2 / 2, holeW2, holeH2, cur.x, cur.y, cur.width, cur.height))
			{
				((CMap)maps.elementAt(j)).makeHoleForHD(x, y, bulletType);
			}
		}
		for (int k = 0; k < PM.p.Length; k++)
		{
			if (PM.p[k] != null && CRes.inRect(PM.p[k].x, PM.p[k].y, x - holeW / 2, y - holeH / 2, holeW, holeH))
			{
				if (PM.p[k].state != 5 && PM.p[k].bulletType != 30)
				{
					PM.p[k].activeHurt((x <= PM.p[k].x) ? 2 : 0);
				}
				PM.p[k].isActiveFall = false;
				PM.p[k].activeFallbyEx = true;
				PM.p[k].chophepGuiUpdateXY = true;
			}
		}
		for (int l = 0; l < GameScr.timeBombs.size(); l++)
		{
			TimeBomb timeBomb = (TimeBomb)GameScr.timeBombs.elementAt(l);
			if (timeBomb != null && !timeBomb.isFall)
			{
				timeBomb.isFall = true;
			}
		}
	}

	public void update()
	{
		for (int i = 0; i < maps.size(); i++)
		{
			if ((CMap)maps.elementAt(i) != null)
			{
				((CMap)maps.elementAt(i)).update();
			}
		}
	}

	public void paint(mGraphics g)
	{
		if (GameScr.cantSee)
		{
			return;
		}
		for (int i = 0; i < maps.size(); i++)
		{
			if ((CMap)maps.elementAt(i) != null)
			{
				((CMap)maps.elementAt(i)).paint(g);
			}
		}
	}

	public void paintBackGround(mGraphics g)
	{
		if (!GameScr.cantSee)
		{
		}
	}

	public void paintWater(mGraphics g)
	{
		if (!GameScr.cantSee)
		{
		}
	}

	public static void readDataMap(myReader d)
	{
		try
		{
			NUM_MAP = d.readUnsignedByte();
			CRes.outz("num = " + NUM_MAP);
			mapFileName = new string[NUM_MAP];
			mapFiles.removeAllElements();
			for (int i = 0; i < NUM_MAP; i++)
			{
				sbyte b = d.readByte();
				short num = d.readShort();
				sbyte[] data = new sbyte[num];
				d.read(ref data, 0, num);
				string mapName = d.readUTF();
				short icon = d.readShort();
				sbyte bgID = d.readByte();
				MapFile o = new MapFile(data, b, mapName, icon, bgID);
				mapFiles.addElement(o);
				data = null;
			}
			CRes.outz("DOC XONG DU LIEU MAP");
		}
		catch (Exception)
		{
			Cout.LogTryCatch("readDataMap MM");
		}
	}

	public static void unload()
	{
		vHoleInfo.removeAllElements();
		maps.removeAllElements();
		mapImages.removeAllElements();
	}
}
