using System;
using Assets.src.rpg;

public class PM
{
	public static int MAX_PLAYER = 100;

	public static int NUMB_PLAYER;

	public static CPlayer[] p;

	public static CPlayer[] p2;

	public static int[] npXsend;

	public static int[] npYsend;

	public static int[] npNumSend;

	public static sbyte curP;

	public static int curUpdateP;

	public static short[] xPResult;

	public static short[] yPResult;

	public int playerCount;

	public int allCount;

	private int planeX;

	private int planeY;

	public static int tKC;

	public static int deltaYKC;

	private int[] f = new int[4] { 0, 1, 2, 1 };

	private int c;

	public static Image dot = CCanvas.loadImage("/gui2/target.png");

	public int nextTurnTimer;

	public static int nhun;

	private bool flag1;

	public void init()
	{
		npXsend = new int[MAX_PLAYER];
		npYsend = new int[MAX_PLAYER];
		npNumSend = new int[MAX_PLAYER];
		xPResult = new short[MAX_PLAYER];
		yPResult = new short[MAX_PLAYER];
	}

	public static void clearPlayer()
	{
		for (int i = 0; i < p.Length; i++)
		{
			p[i] = null;
		}
		PrepareScr.currLevel = 0;
	}

	public void initPlayer(short[] pX, short[] pY, short[] maxHP, short[] currHP, short[] maxMove)
	{
		flag1 = false;
		playerCount = 0;
		allCount = 0;
		int num = 8;
		for (int i = 0; i < num; i++)
		{
			if (p[i] != null && maxHP[i] != 0)
			{
				bool flag = MyMidlet.myInfo.IDDB != p[i].IDDB;
				p[i].init(pX[i], pY[i], flag, (i % 2 == 0) ? 2 : 0, maxHP[i], maxMove[i]);
				p[i].hp = currHP[i];
				p[i].hpRectW = p[i].hp * 100 / p[i].maxhp * 25 / 100;
				CRes.outz("o day gan hp = " + p[i].hp + "---------------------------------------------------------------");
				p[i].index = (sbyte)i;
				p[i].teamColor = (sbyte)((i % 2 != 0) ? 1 : 0);
				p[i].isRunSpeed = false;
				CRes.outz("px thu " + i + " = " + p[i].x + " px next= " + p[i].nextx + " iscom= " + flag + "-----------------------------------------------------------------------------------");
			}
			playerCount++;
		}
		CRes.outz("PLAYER COUNT= " + playerCount);
		allCount = playerCount;
		for (int j = 0; j < num; j++)
		{
			npXsend[j] = -1;
			npYsend[j] = -1;
			npNumSend[j] = j;
		}
	}

	public string getPlayerNameFromID(int id)
	{
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i].IDDB == id)
			{
				return p[i].name;
			}
		}
		return string.Empty;
	}

	public void initBoss(short[] bX, short[] bY)
	{
		CRes.outz("HERE 6");
		allCount += CCanvas.prepareScr.bossInfos.size();
		CRes.outz("HERE 7");
		CCanvas.prepareScr.bossInfos.removeAllElements();
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] != null)
			{
				CRes.outz("i player= " + i);
			}
		}
	}

	public static void getXYResult()
	{
		for (int i = 0; i < p.Length; i++)
		{
			CPlayer cPlayer = p[i];
			if (cPlayer != null)
			{
				xPResult[i] = (short)p[i].x;
				yPResult[i] = (short)p[i].y;
			}
			else
			{
				xPResult[i] = -1;
				yPResult[i] = -1;
			}
		}
	}

	public static int getIndexByIDDB(int iddb)
	{
		for (int i = 0; i < p.Length; i++)
		{
			CPlayer cPlayer = p[i];
			if (cPlayer != null && cPlayer.IDDB == iddb)
			{
				return i;
			}
		}
		return -1;
	}

	public static CPlayer getCurPlayer()
	{
		if (p[curP] != null)
		{
			return p[curP];
		}
		return null;
	}

	public static CPlayer getMyPlayer()
	{
		return p[GameScr.myIndex];
	}

	public static CPlayer getPlayerByIndex(int index)
	{
		try
		{
			return p[index];
		}
		catch (Exception)
		{
			return null;
		}
	}

	public static CPlayer getPlayerByIndex2(int index)
	{
		try
		{
			return p2[index];
		}
		catch (Exception)
		{
			return null;
		}
	}

	public static CPlayer getPlayerByIDDB(int IDDB)
	{
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] != null && p[i].IDDB == IDDB)
			{
				return p[i];
			}
		}
		return null;
	}

	public static CPlayer findPlayerByIndex(int index)
	{
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] != null && p[i].index == index)
			{
				return p[i];
			}
		}
		return null;
	}

	public void input()
	{
		p[GameScr.myIndex].input();
	}

	public void flyAnimation()
	{
		if (CCanvas.gameTick % 2 == 0)
		{
			tKC++;
			if (tKC == 10)
			{
				deltaYKC = 0;
				tKC = 0;
			}
			if (tKC <= 5)
			{
				deltaYKC++;
			}
			else
			{
				deltaYKC--;
			}
		}
	}

	public void updateFrame()
	{
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] != null)
			{
				curUpdateP = i;
				p[i].updateFrameStand();
			}
		}
	}

	public void update()
	{
		if (nextTurnTimer > 0 && !CCanvas.gameScr.isSplash)
		{
			nextTurnTimer--;
			if (CCanvas.gameTick % 2 == 0)
			{
				nhun++;
				if (nhun > 5)
				{
					nhun = 0;
				}
			}
		}
		sbyte b = 0;
		bool flag = false;
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] != null)
			{
				curUpdateP = i;
				p[i].update();
				if (p[i].isAllowSendPosAfterShoot)
				{
					npNumSend[b] = i;
					npXsend[b] = p[i].x;
					npYsend[b] = p[i].y;
					b++;
				}
				if (p[i].falling && p[i].state != 5)
				{
					flag = true;
				}
			}
		}
		flyAnimation();
		if (BM.allSendENDSHOOT && !flag)
		{
			GameService.gI().shootResult();
			CPlayer.isShooting = false;
			getCurPlayer().shootFrame = false;
			BM.allSendENDSHOOT = false;
			GameService.gI().holeInfo(MM.vHoleInfo);
		}
	}

	public void addPlayer(CPlayer p1)
	{
		for (int i = 0; i < MAX_PLAYER; i++)
		{
			if (p[i] == null)
			{
				p[i] = p1;
				playerCount++;
				break;
			}
		}
	}

	public void update2()
	{
		if (CCanvas.gameTick % 2 == 0)
		{
			nhun++;
			if (nhun > 5)
			{
				nhun = 0;
			}
		}
		for (int i = 0; i < p2.Length; i++)
		{
			if (p2[i] != null)
			{
				curUpdateP = i;
				p2[i].update2();
			}
		}
	}

	public void paint2(mGraphics g)
	{
		for (int i = 0; i < p2.Length; i++)
		{
			if (p2[i] != null)
			{
				p2[i].paint2(g);
				if (GameScr.iconOn)
				{
					p2[i].paintLevel(g, p2[i].x - 17, p2[i].y - 67);
				}
			}
		}
	}

	public static int getIndexByIDDB2(int iddb)
	{
		for (int i = 0; i < p2.Length; i++)
		{
			CPlayer cPlayer = p2[i];
			if (cPlayer != null && cPlayer.IDDB == iddb)
			{
				return i;
			}
		}
		return -1;
	}

	public static CPlayer getMyPlayer2()
	{
		if (GameScrRPG.myIndex >= p2.Length)
		{
			return null;
		}
		return p2[GameScrRPG.myIndex];
	}

	public static CPlayer getPlayerByIDDB2(int IDDB)
	{
		for (int i = 0; i < p2.Length; i++)
		{
			if (p2[i] != null && p2[i].IDDB == IDDB)
			{
				return p2[i];
			}
		}
		return null;
	}

	public static bool isLand(sbyte index)
	{
		CPlayer playerByIndex = getPlayerByIndex(index);
		if (playerByIndex == null)
		{
			return false;
		}
		if (GameScr.mm.isLand(playerByIndex.x, playerByIndex.y))
		{
			return true;
		}
		return false;
	}

	public void paint(mGraphics g)
	{
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] == null || p[i].x + 75 <= Camera.x || p[i].x - 75 >= Camera.x + CScreen.w)
			{
				continue;
			}
			if (GameScr.cantSee)
			{
				if (i == GameScr.myIndex)
				{
					p[i].paintBiaMo(g);
				}
			}
			else
			{
				p[i].paintBiaMo(g);
			}
		}
		for (int j = 0; j < p.Length; j++)
		{
			if (p[j] == null)
			{
				continue;
			}
			if (p[j].x + 75 > Camera.x && p[j].x - 75 < Camera.x + CScreen.w)
			{
				if (GameScr.cantSee)
				{
					if (j == GameScr.myIndex)
					{
						p[j].paint(g);
						if (GameScr.iconOn)
						{
							p[j].paintLevel(g, p[j].x - 17, p[j].y - 67);
						}
					}
				}
				else
				{
					p[j].paint(g);
					if (GameScr.iconOn)
					{
						p[j].paintLevel(g, p[j].x - 17, p[j].y - 67);
					}
				}
			}
			if (p[j].index == curP && !CCanvas.gameScr.isSplash && nextTurnTimer > 0)
			{
				g.drawImage(GameScr.imgHandThang, p[j].x, p[j].y - 71 + nhun, 3, false);
			}
		}
		for (int k = 0; k < p.Length; k++)
		{
			if (p[k] != null && GameScr.pm.isYourTurn() && GameScr.myIndex == p[k].index && p[k].state != 8)
			{
				p[k].paintCrosshair(g);
			}
		}
	}

	public void drawTargetDot(mGraphics g)
	{
		c++;
		if (c > f.Length - 1)
		{
			c = 0;
		}
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] != null && p[i].state != 5 && !p[i].isInvisible)
			{
				bool flag = true;
				int num = p[i].x;
				int num2 = p[i].y - CPlayer.pH / 2;
				if (num < Camera.x + 5)
				{
					num = Camera.x + 5;
				}
				else if (num > Camera.x + CScreen.w - 10)
				{
					num = Camera.x + CScreen.w - 10;
				}
				else if (num2 > Camera.y + 20 && num2 < Camera.y + CScreen.h - 30)
				{
					flag = false;
				}
				if (num2 < Camera.y + 20)
				{
					num2 = Camera.y + 20;
				}
				else if (num2 > Camera.y + CScreen.h - 30)
				{
					num2 = Camera.y + CScreen.h - 30;
				}
				else if (num > Camera.x + 5 && num < Camera.x + CScreen.w - 10)
				{
					flag = false;
				}
				if (flag)
				{
					g.drawRegion(dot, 0, 9 * f[c], 9, 9, 0, num, num2, 3, false);
				}
			}
		}
	}

	public bool isYourTurn()
	{
		if (curP == GameScr.myIndex)
		{
			return true;
		}
		return false;
	}

	public bool isMyPlayerUpdate()
	{
		if (curUpdateP == GameScr.myIndex)
		{
			return true;
		}
		return false;
	}

	public void setNextPlayer(sbyte whoNext, int X, int Y)
	{
		CPlayer.isStopFire = false;
		nextTurnTimer = 100;
		CRes.outz("SET NEXT PLAYER " + whoNext + "X= " + X + " Y= " + Y + "------------------------------------");
		if (GameScr.res != string.Empty || p[whoNext] == null)
		{
			return;
		}
		p[whoNext].active = true;
		if (p[whoNext].state != 8)
		{
			p[whoNext].state = 0;
			p[whoNext].updateFrameAngle();
		}
		p[whoNext].movePoint = 0f;
		p[whoNext].itemUsed = -1;
		p[whoNext].isSecondPower = false;
		p[whoNext].isHoldFire = false;
		p[whoNext].state = 0;
		p[whoNext].force = 0f;
		p[whoNext].force_2 = 0f;
		p[whoNext].isSecondPower = false;
		p[whoNext].x = X;
		p[whoNext].y = Y;
		p[whoNext].updateAngry(p[whoNext].currAngry);
		if (CCanvas.isTouch)
		{
			p[whoNext].isMoveAvenger = false;
		}
		else
		{
			p[whoNext].isMoveAvenger = true;
		}
		if (p[GameScr.myIndex] != null)
		{
			p[GameScr.myIndex].isUsedItem = false;
		}
		if (p[whoNext].isUsedItem)
		{
			p[whoNext].isUsedItem = false;
		}
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] != null)
			{
				p[i].shootFrame = false;
				if (p[i].state != 5)
				{
					p[i].isAllowSendPosAfterShoot = false;
				}
				BM.nBum = 0;
				if (i == GameScr.myIndex && !p[i].chophepGuiUpdateXY)
				{
					GameService.gI().requiredUpdateXY((short)p[i].x, (short)p[i].y);
					p[i].chophepGuiUpdateXY = true;
				}
			}
		}
		GameScr.time.resetTime();
		if (curP == whoNext && curP == GameScr.myIndex)
		{
			if (flag1)
			{
				CCanvas.gameScr.strYourTurn = L.yourTurn2();
			}
			else
			{
				CCanvas.gameScr.strYourTurn = L.yourTurn();
				flag1 = true;
			}
			CCanvas.gameScr.isOpenUTurn = true;
			curP = whoNext;
		}
		else
		{
			curP = whoNext;
			if (GameScr.pm.isYourTurn())
			{
				CCanvas.gameScr.strYourTurn = L.yourTurn();
				CCanvas.gameScr.isOpenUTurn = true;
			}
		}
		GameScr.cam.setPlayerMode(whoNext);
		if (GameScr.pm.isYourTurn())
		{
			GameScr.isPlayItem = true;
			p[whoNext].isHawlSpecial = false;
			p[whoNext].isUltronSpecial = false;
			p[whoNext].isThorSpecial = false;
			p[whoNext].isLokiSpecial = false;
		}
		else
		{
			GameScr.isPlayItem = false;
		}
		CRes.outz("Next Player");
		if (CCanvas.menu.showMenu)
		{
			CCanvas.menu.showMenu = false;
		}
	}

	public void movePlayer(int whoMove, short x, short y)
	{
		CRes.outz("LAST X= " + p[whoMove].x + "nexx=  " + x + " Last y= " + p[whoMove].y + "next y= " + y);
		p[whoMove].nextx = x;
		p[whoMove].nexty = y;
		p[whoMove].isMove = true;
		p[whoMove].tMove = 0;
		if (whoMove == GameScr.myIndex)
		{
			p[whoMove].resetLastUpdateXY(x, y);
		}
	}

	public void movePlayer2(int whoMove, short x, short y)
	{
		p2[whoMove].nextx = x;
		p2[whoMove].nexty = y;
		p2[whoMove].isMove = true;
		p2[whoMove].tMove = 0;
		if (whoMove == GameScr.myIndex)
		{
			p2[whoMove].resetLastUpdateXY(x, y);
		}
	}

	public void flyTo(int whoFly, short xF, short yF)
	{
		p[whoFly].flyToPoint(xF, yF);
	}

	public void updatePlayerXY(int whoUpdateXY, short x, short y)
	{
		p[whoUpdateXY].x = x;
		p[whoUpdateXY].y = y;
		p[whoUpdateXY].nextx = x;
		p[whoUpdateXY].lastx = x;
		p[whoUpdateXY].nexty = y;
		p[whoUpdateXY].lasty = y;
		if (whoUpdateXY == GameScr.myIndex)
		{
			p[whoUpdateXY].resetLastUpdateXY(x, y);
		}
	}

	public void setPlayerAfterSetWin(bool teamWin)
	{
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] == null)
			{
				continue;
			}
			p[i].active = false;
			if (p[i].hp > 0)
			{
				if (p[i].team == CPlayer.TEAM_GREEN && teamWin == CPlayer.TEAM_GREEN)
				{
					p[i].setWin();
				}
				else if (p[i].team == CPlayer.TEAM_YELLOW && teamWin == CPlayer.TEAM_YELLOW)
				{
					p[i].setWin();
				}
				if (i == GameScr.myIndex)
				{
					CRes.outz("After Set Win");
				}
			}
		}
	}

	public void setPlayerAfterDraw()
	{
		for (int i = 0; i < p.Length; i++)
		{
			if (p[i] != null)
			{
				p[i].active = false;
				if (i == GameScr.myIndex)
				{
					CRes.outz("After Draw");
				}
			}
		}
	}
}
