using System.Threading;
using UnityEngine;

public class Main : MonoBehaviour
{
	public const sbyte IP_JB = 3;

	public const sbyte PC_VERSION = 4;

	public const sbyte IP_APPSTORE = 5;

	public const sbyte WINDOWSPHONE = 6;

	public const sbyte GOOGLE_PLAY = 7;

	public static Main main;

	public static mGraphics g;

	public static MyMidlet midlet;

	public static string res = "res";

	public static string mainThreadName;

	public static bool started;

	public static bool isIpod;

	public static bool isIphone4;

	public static bool isPC = true;

	public static bool isWindowsPhone;

	public static bool isIPhone;

	public static bool IphoneVersionApp;

	public static string IMEI;

	public static int versionIp;

	public static int numberQuit = 1;

	public static int clientType = 4;

	private int level;

	public static int FPS = 30;

	private int updateCount;

	private int paintCount;

	private int count;

	private bool isRun;

	public static int waitTick;

	public static int f;

	public static bool isResume;

	public static bool isMiniApp = true;

	public static bool isQuitApp;

	private Vector2 lastMousePos = default(Vector2);

	public static int a = 1;

	public static bool isCompactDevice = true;

	private void Start()
	{
		if (started)
		{
			return;
		}
		DetectRuntimePlatform();
		if (Thread.CurrentThread.Name != "Main")
		{
			Thread.CurrentThread.Name = "Main";
		}
		mainThreadName = Thread.CurrentThread.Name;
		started = true;
		if (isPC)
		{
			level = Rms.loadRMSInt("levelScreenKN");
			if (level == 1)
			{
				Screen.SetResolution(480, 320, false);
			}
			else
			{
				Screen.SetResolution(1080, 720, false);
			}
		}
	}

	private void OnGUI()
	{
		if (count >= 10)
		{
			if (!IsRuntimeReady())
			{
				return;
			}
			checkInput();
			Session_ME.update();
			if (Event.current != null && Event.current.type.Equals(EventType.Repaint) && paintCount <= updateCount)
			{
				MyMidlet.canvas.paint(g);
				paintCount++;
				g.reset();
			}
		}
	}

	public void setsizeChange()
	{
		if (!isRun)
		{
			try
			{
				DetectRuntimePlatform();
				Screen.orientation = ScreenOrientation.LandscapeLeft;
				Application.runInBackground = true;
				Application.targetFrameRate = 30;
				base.useGUILayout = false;
				isCompactDevice = detectCompactDevice();
				if (main == null)
				{
					main = this;
				}
				ScaleGUI.initScaleGUI();
				IMEI = SystemInfo.deviceUniqueIdentifier;
				if (isPC)
				{
					Screen.fullScreen = false;
				}
				if (isWindowsPhone)
				{
					clientType = 6;
				}
				if (isPC)
				{
					clientType = 4;
				}
				if (IphoneVersionApp)
				{
					clientType = 5;
				}
				SyncPlatformFlags();
				if (iPhoneSettings.generation == iPhoneGeneration.iPodTouch4Gen)
				{
					isIpod = true;
				}
				if (iPhoneSettings.generation == iPhoneGeneration.iPhone4)
				{
					isIphone4 = true;
				}
				g = new mGraphics();
				midlet = new MyMidlet();
				if (MyMidlet.canvas == null)
				{
					throw new System.NullReferenceException("MyMidlet.canvas was not created.");
				}
				isRun = true;
				try
				{
					SoundMn.gI().loadSound(0);
					g.CreateLineMaterial();
				}
				catch (System.Exception ex)
				{
					Cout.LogTryCatch("Main optional init failed: " + ex);
				}
			}
			catch (System.Exception ex)
			{
				isRun = false;
				Cout.LogTryCatch("Main.setsizeChange failed: " + ex);
			}
		}
	}

	private static bool IsRuntimeReady()
	{
		return g != null && midlet != null && MyMidlet.canvas != null;
	}

	private static void DetectRuntimePlatform()
	{
		RuntimePlatform platform = Application.platform;
		isWindowsPhone = false;
		isIPhone = platform == RuntimePlatform.IPhonePlayer;
		IphoneVersionApp = isIPhone;
		isPC = platform != RuntimePlatform.Android && platform != RuntimePlatform.IPhonePlayer;
		if (platform == RuntimePlatform.Android)
		{
			clientType = GOOGLE_PLAY;
		}
		else if (platform == RuntimePlatform.IPhonePlayer)
		{
			clientType = IP_APPSTORE;
		}
		else
		{
			clientType = PC_VERSION;
		}
		SyncPlatformFlags();
	}

	private static void SyncPlatformFlags()
	{
		mSystem.isPC = isPC;
		mSystem.isIphone = isIPhone;
		mSystem.clientType = clientType;
		mSystem.isAppStore = clientType == IP_APPSTORE;
		mSystem.isSmartPhone = !isPC;
	}

	public void doClearRMS()
	{
		if (isPC)
		{
			int num = Rms.loadRMSInt("lastZoomlevel");
			if (num != mGraphics.zoomLevel)
			{
				Rms.clearAll();
				Rms.saveRMSInt("lastZoomlevel", mGraphics.zoomLevel);
				Rms.saveRMSInt("levelScreenKN", level);
			}
		}
	}

	public static void closeKeyBoard()
	{
		if (TField.kb != null)
		{
			TField.kb.active = false;
			TField.kb = null;
		}
	}

	private void FixedUpdate()
	{
		Rms.update();
		count++;
		if (count >= 10)
		{
			setsizeChange();
			if (!IsRuntimeReady())
			{
				return;
			}
			updateCount++;
			ipKeyboard.update();
			MyMidlet.canvas.update();
			Image.update();
			DataInputStream.update();
			mySMS.update();
			Net.update();
			f++;
			if (f > 8)
			{
				f = 0;
			}
		}
	}

	private void Update()
	{
	}

	private void checkInput()
	{
		if (Input.GetMouseButtonDown(0))
		{
			Vector3 mousePosition = Input.mousePosition;
			MyMidlet.canvas.pointerPressed((int)(mousePosition.x / (float)mGraphics.zoomLevel), (int)(((float)Screen.height - mousePosition.y) / (float)mGraphics.zoomLevel) + mGraphics.addYWhenOpenKeyBoard);
			lastMousePos.x = mousePosition.x / (float)mGraphics.zoomLevel;
			lastMousePos.y = mousePosition.y / (float)mGraphics.zoomLevel + (float)mGraphics.addYWhenOpenKeyBoard;
		}
		if (Input.GetMouseButton(0))
		{
			Vector3 mousePosition2 = Input.mousePosition;
			MyMidlet.canvas.pointerDragged((int)(mousePosition2.x / (float)mGraphics.zoomLevel), (int)(((float)Screen.height - mousePosition2.y) / (float)mGraphics.zoomLevel) + mGraphics.addYWhenOpenKeyBoard);
			lastMousePos.x = mousePosition2.x / (float)mGraphics.zoomLevel;
			lastMousePos.y = mousePosition2.y / (float)mGraphics.zoomLevel + (float)mGraphics.addYWhenOpenKeyBoard;
		}
		if (Input.GetMouseButtonUp(0))
		{
			Vector3 mousePosition3 = Input.mousePosition;
			lastMousePos.x = mousePosition3.x / (float)mGraphics.zoomLevel;
			lastMousePos.y = mousePosition3.y / (float)mGraphics.zoomLevel + (float)mGraphics.addYWhenOpenKeyBoard;
			MyMidlet.canvas.pointerReleased((int)(mousePosition3.x / (float)mGraphics.zoomLevel), (int)(((float)Screen.height - mousePosition3.y) / (float)mGraphics.zoomLevel) + mGraphics.addYWhenOpenKeyBoard);
		}
		if (Input.anyKeyDown && Event.current.type == EventType.KeyDown)
		{
			int num = MyKeyMap.map(Event.current.keyCode);
			if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
			{
				switch (Event.current.keyCode)
				{
				case KeyCode.Alpha2:
					num = 64;
					break;
				case KeyCode.Minus:
					num = 95;
					break;
				}
			}
			if (num != 0)
			{
				MyMidlet.canvas.keyPressedz(num);
			}
		}
		if (Event.current.type == EventType.KeyUp)
		{
			int num2 = MyKeyMap.map(Event.current.keyCode);
			if (num2 != 0)
			{
				MyMidlet.canvas.keyReleasedz(num2);
			}
		}
		if (!isPC)
		{
		}
	}

	private void OnApplicationQuit()
	{
		Debug.LogWarning("APP QUIT");
		CCanvas.isRunning = false;
		Session_ME.gI().close();
		if (isPC)
		{
			Application.Quit();
		}
	}

	private void OnApplicationPause(bool paused)
	{
		isResume = false;
		if (paused)
		{
			if (CCanvas.isWaiting())
			{
				isQuitApp = true;
			}
		}
		else
		{
			isResume = true;
		}
		if (TField.kb != null)
		{
			TField.kb.active = false;
			TField.kb = null;
		}
		if (isQuitApp)
		{
			Application.Quit();
		}
	}

	public static void exit()
	{
		if (isPC)
		{
			main.OnApplicationQuit();
		}
		else
		{
			a = 0;
		}
	}

	public static bool detectCompactDevice()
	{
		if (iPhoneSettings.generation == iPhoneGeneration.iPhone || iPhoneSettings.generation == iPhoneGeneration.iPhone3G || iPhoneSettings.generation == iPhoneGeneration.iPodTouch1Gen || iPhoneSettings.generation == iPhoneGeneration.iPodTouch2Gen)
		{
			return false;
		}
		return true;
	}

	public static bool checkCanSendSMS()
	{
		if (iPhoneSettings.generation == iPhoneGeneration.iPhone3GS || iPhoneSettings.generation == iPhoneGeneration.iPhone4 || iPhoneSettings.generation > iPhoneGeneration.iPodTouch4Gen)
		{
			return true;
		}
		return false;
	}
}
