using System;
using System.Collections.Generic;
using System.IO;
using Ionic.Zlib;
using UnityEngine;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

namespace wf
{
	public static class Utility
	{
		public static GameObject CreatePrefab(GameObject parent, string path, bool trans_reset = true)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load(path)) as GameObject;
			if (parent != null)
			{
				gameObject.transform.SetParent(parent.transform, false);
			}
			else
			{
				gameObject.transform.SetParent(null, false);
			}
			if (trans_reset)
			{
				gameObject.transform.localPosition = Vector3.zero;
				gameObject.transform.localScale = Vector3.one;
			}
			return gameObject;
		}

		public static Sprite CreateTextureSprite(string texturFileName)
		{
			if (!string.IsNullOrEmpty(texturFileName))
			{
				texturFileName = Path.ChangeExtension(texturFileName, ".tex");
			}
			if (!GameUty.FileSystem.IsExistentFile(texturFileName))
			{
				return null;
			}
			Texture2D texture2D = ImportCM.CreateTexture(texturFileName);
			Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
			sprite.name = texturFileName;
			return sprite;
		}

		public static Sprite CreateTextureSpriteFromImageFile(string pngFilePath)
		{
			if (string.IsNullOrEmpty(pngFilePath))
			{
				return null;
			}
			if (!File.Exists(pngFilePath))
			{
				return null;
			}
			Texture2D texture2D = null;
			try
			{
				byte[] array = File.ReadAllBytes(pngFilePath);
				if (array == null || array.Length == 0)
				{
					return null;
				}
				texture2D = new Texture2D(32, 32);
				texture2D.LoadImage(array);
			}
			catch (Exception ex)
			{
				Debug.LogError(ex.Message + "\n\n" + ex.StackTrace);
				return null;
			}
			if (texture2D == null || texture2D.width == 0 || texture2D.height == 0)
			{
				return null;
			}
			Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
			sprite.name = Path.GetFileName(pngFilePath);
			return sprite;
		}

		public static void ResetNGUI(UIGrid grid)
		{
			grid.Reposition();
		}

		public static void ResetNGUI(UITable table)
		{
			table.Reposition();
		}

		public static void ResetNGUI(UIScrollBar scroll_bar)
		{
			scroll_bar.value = 0f;
		}

		public static void ResetNGUI(UIScrollView scroll_view)
		{
			scroll_view.currentMomentum = Vector3.zero;
			scroll_view.Press(true);
			scroll_view.Press(false);
			scroll_view.ResetPosition();
		}

		public static void ResizeUILabelFontSize(UILabel label, int limitWidth)
		{
			int width = label.width;
			label.width = 0;
			label.MakePixelPerfect();
			if (label.width <= limitWidth)
			{
				label.width = width;
				return;
			}
			while (limitWidth < label.width && 1 <= label.fontSize)
			{
				label.fontSize--;
				label.width = 0;
				label.MakePixelPerfect();
			}
		}

		public static float VolumeToDecibel(float volume)
		{
			return 20f * Mathf.Log10(Mathf.Clamp(volume, 0.0001f, 1f));
		}

		public static float DecibelToVolume(float db)
		{
			float num = Mathf.Pow(10f, Mathf.Clamp(db, -80f, 0f) / 20f);
			return (num > 0.0001f) ? num : 0f;
		}

		public static string ConvertMoneyText(int num)
		{
			return string.Format("{0:#,##0}", num);
		}

		public static string ConvertMoneyText(long num)
		{
			return string.Format("{0:#,##0}", num);
		}

		public static bool IsNumericOnlyText(string text)
		{
			bool result = true;
			foreach (char c in text)
			{
				if ((c < '0' || c > '9') && c != ' ' && c != '-')
				{
					result = false;
					break;
				}
			}
			return result;
		}

		public static byte[] ZlibCompresss(byte[] data)
		{
			return DeflateStream.CompressBuffer(data);
		}

		public static byte[] ZlibUncompress(byte[] data)
		{
			return DeflateStream.UncompressBuffer(data);
		}

		public static Texture2D CreateQRCodeTexture(string text, int width = 512, int height = 512)
		{
			QRCodeWriter qrcodeWriter = new QRCodeWriter();
			BitMatrix bitMatrix = qrcodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, new Dictionary<EncodeHintType, object>
			{
				{
					EncodeHintType.CHARACTER_SET,
					"ISO-8859-1"
				},
				{
					EncodeHintType.ERROR_CORRECTION,
					ErrorCorrectionLevel.L
				}
			});
			int width2 = bitMatrix.Width;
			int height2 = bitMatrix.Height;
			Texture2D texture2D = new Texture2D(width2, height2, TextureFormat.ARGB32, false);
			for (int i = 0; i < height2; i++)
			{
				for (int j = 0; j < width2; j++)
				{
					texture2D.SetPixel(j, i, (!bitMatrix[j, i]) ? Color.white : Color.black);
				}
			}
			texture2D.Apply();
			return texture2D;
		}

		public static Texture2D CreateTexture2DFromRenderTexture(RenderTexture render_tex)
		{
			Texture2D texture2D = new Texture2D(render_tex.width, render_tex.height, TextureFormat.ARGB32, false);
			RenderTexture active = RenderTexture.active;
			RenderTexture.active = render_tex;
			texture2D.ReadPixels(new Rect(0f, 0f, (float)render_tex.width, (float)render_tex.height), 0, 0);
			texture2D.Apply();
			RenderTexture.active = active;
			return texture2D;
		}
	}
}
