using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Cute;
using SFB;
using UnityEngine;

namespace Wizard;

public class QrCamera : MonoBehaviour
{
	private enum Mode
	{
		None,
		Camera,
		CameraRoll
	}

	[SerializeField]
	private GameObject _cameraRoot;

	[SerializeField]
	private UITexture _halfTransparentTexture;

	[SerializeField]
	private UITexture _backGroundTexture;

	[SerializeField]
	private UITexture _cameraTexture;

	[SerializeField]
	private UISprite _blueFrameTexture;

	[SerializeField]
	private UITexture _cameraRollTexture;

	private string _qrCodeCameraText;

	[SerializeField]
	private UIButton _backButton;

	[SerializeField]
	private UILabel _descriptionTextLabel;

	private WebCamTexture _webCamTexture;

	private Action _qrScanSuccessee;

	private Action<string> _qrScanFailed;

	private GameObject _qrCameraObject;

	private const float DESCRIPTION_TEXT_OFFSET = 30f;

	private ScreenOrientation _preScreenOrientation = ScreenOrientation.LandscapeLeft;

	private List<string> _resourcePathList = new List<string>();

	private const string HALF_TRANSPARENT_TEXTURE_PATH = "512black_82transparent";

	private bool loadCameraRollImageCalled;

	private CardMaster.CardMasterId _cardMasterId = CardMaster.CardMasterId.Default;

	private bool webCameraSetSuccess;

	private Mode _currentMode;

	public UIButton backButton
	{
		get
		{
			return _backButton;
		}
		private set
		{
			_backButton = value;
		}
	}

	private void Awake()
	{
		_cameraRoot.gameObject.SetActive(value: false);
	}

	public void SetCallBacks(Action qrScanSuccessee, Action<string> qrScanFailed)
	{
		_qrScanSuccessee = qrScanSuccessee;
		_qrScanFailed = qrScanFailed;
	}

	public IEnumerator StartQRCamera(GameObject qrCameraObject, CardMaster.CardMasterId cardMasterId, Action onComplete, Action onFailed)
	{
		SetupWebCam();
		_cardMasterId = cardMasterId;
		if (!webCameraSetSuccess)
		{
			onFailed.Call();
			yield break;
		}
		_cameraRoot.gameObject.SetActive(value: true);
		_qrCameraObject = qrCameraObject;
		_preScreenOrientation = Screen.orientation;
		if (_preScreenOrientation == ScreenOrientation.LandscapeRight)
		{
			Vector3 localScale = _cameraTexture.transform.localScale;
			localScale.x = 0f - localScale.x;
			localScale.y = 0f - localScale.y;
			_cameraTexture.transform.localScale = localScale;
		}
		_currentMode = Mode.Camera;
		StartCoroutine(PlayCamera());
		onComplete.Call();
	}

	public void StartGetQRCodeFromImageFile(GameObject qrCameraObject, CardMaster.CardMasterId cardMasterId)
	{
		_cameraRollTexture.gameObject.SetActive(value: true);
		_cardMasterId = cardMasterId;
		_qrCameraObject = qrCameraObject;
		_currentMode = Mode.CameraRoll;
		ExtensionFilter[] extensions = new ExtensionFilter[1]
		{
			new ExtensionFilter("Image Files", "png", "jpg", "jpeg")
		};
		string[] array = StandaloneFileBrowser.OpenFilePanel(Data.SystemText.Get("Card_0271"), "", extensions, multiselect: false);
		if (array.Length != 0 && !string.IsNullOrEmpty(array[0]))
		{
			LoadCameraRollImage(array[0]);
		}
		else
		{
			UnityEngine.Object.Destroy(_qrCameraObject);
		}
	}

	public void StopQRCamera()
	{
		_cameraRoot.gameObject.SetActive(value: false);
		_currentMode = Mode.None;
		Toolbox.ResourcesManager.RemoveAssetGroup(_resourcePathList);
		_resourcePathList.Clear();
		StopCamera();
	}

	private void Update()
	{
		if (_currentMode != Mode.Camera || !(_webCamTexture != null) || !_webCamTexture.isPlaying)
		{
			return;
		}
		if (_preScreenOrientation != Screen.orientation)
		{
			ChangeCameraTextrueDirection();
		}
		_qrCodeCameraText = QRCodeUtility.DecodeContentText(_webCamTexture);
		if (!string.IsNullOrEmpty(_qrCodeCameraText))
		{
			StopCamera();
			if (QRCodeUtility.SetDeckFromQRCodeText(_qrCodeCameraText, _cardMasterId))
			{
				_qrScanSuccessee.Call();
			}
			else
			{
				_qrScanFailed.Call(Data.SystemText.Get("Card_0267"));
			}
			UnityEngine.Object.Destroy(_qrCameraObject);
		}
	}

	private void ChangeCameraTextrueDirection()
	{
		switch (Screen.orientation)
		{
		case ScreenOrientation.LandscapeLeft:
		{
			_preScreenOrientation = Screen.orientation;
			Vector3 localScale2 = _cameraTexture.transform.localScale;
			localScale2.x = 0f - localScale2.x;
			localScale2.y = 0f - localScale2.y;
			_cameraTexture.transform.localScale = localScale2;
			break;
		}
		case ScreenOrientation.LandscapeRight:
		{
			_preScreenOrientation = Screen.orientation;
			Vector3 localScale = _cameraTexture.transform.localScale;
			localScale.x = 0f - localScale.x;
			localScale.y = 0f - localScale.y;
			_cameraTexture.transform.localScale = localScale;
			break;
		}
		}
	}

	private void SetupWebCam()
	{
		ResourcesManager resMgr = Toolbox.ResourcesManager;
		_resourcePathList.Add(resMgr.GetAssetTypePath("512black_82transparent", ResourcesManager.AssetLoadPathType.UiOtherTexture));
		UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_resourcePathList, delegate
		{
			_halfTransparentTexture.mainTexture = resMgr.LoadObject(resMgr.GetAssetTypePath("512black_82transparent", ResourcesManager.AssetLoadPathType.UiOtherTexture, isfetch: true)) as Texture;
		}));
		WebCamDevice[] devices = WebCamTexture.devices;
		if (devices != null && devices.Length != 0)
		{
			float num = (float)Screen.width / (float)Screen.height;
			if (num != 1.7777778f)
			{
				float aspectRatio = _cameraTexture.aspectRatio;
				float num2 = ((num < 1.7777778f) ? 1f : (num / aspectRatio));
				float num3 = ((num < 1.7777778f) ? (aspectRatio / num) : 1f);
				_halfTransparentTexture.SetDimensions(Mathf.CeilToInt((float)_halfTransparentTexture.width * num2), Mathf.CeilToInt((float)_halfTransparentTexture.height * num3));
				_backGroundTexture.SetDimensions(Mathf.CeilToInt(_halfTransparentTexture.width), Mathf.CeilToInt(_halfTransparentTexture.height));
			}
			_webCamTexture = new WebCamTexture(devices[0].name, Screen.width, Screen.height, 30);
			webCameraSetSuccess = true;
		}
		else
		{
			webCameraSetSuccess = false;
		}
	}

	private IEnumerator PlayCamera()
	{
		if (!(_webCamTexture == null) && !_webCamTexture.isPlaying)
		{
			_halfTransparentTexture.ResizeCollider();
			_backGroundTexture.ResizeCollider();
			_cameraTexture.mainTexture = _webCamTexture;
			_descriptionTextLabel.gameObject.SetActive(value: false);
			_blueFrameTexture.gameObject.SetActive(value: false);
			_cameraTexture.gameObject.SetActive(value: false);
			_halfTransparentTexture.gameObject.SetActive(value: false);
			_webCamTexture.Play();
			while (_webCamTexture.width <= 16 && _webCamTexture.height <= 16)
			{
				yield return null;
			}
			float num = (float)_halfTransparentTexture.width / (float)_halfTransparentTexture.height;
			float num2 = (float)_webCamTexture.width / (float)_webCamTexture.height;
			if (num > num2)
			{
				_cameraTexture.SetDimensions(Mathf.CeilToInt((float)_halfTransparentTexture.height / (float)_webCamTexture.height * (float)_webCamTexture.width), _halfTransparentTexture.height);
			}
			else if (Mathf.Approximately(num, num2))
			{
				_cameraTexture.SetDimensions(_halfTransparentTexture.width, _halfTransparentTexture.height);
			}
			else
			{
				_cameraTexture.SetDimensions(_halfTransparentTexture.width, Mathf.CeilToInt((float)_halfTransparentTexture.width / (float)_webCamTexture.width * (float)_webCamTexture.height));
			}
			_cameraTexture.SetDimensions(Mathf.CeilToInt((float)_cameraTexture.width * 0.8f), Mathf.CeilToInt((float)_cameraTexture.height * 0.8f));
			int num3 = Mathf.FloorToInt((float)Mathf.Min(_halfTransparentTexture.width, _halfTransparentTexture.height) * 0.5f);
			_blueFrameTexture.SetDimensions(num3, num3);
			_blueFrameTexture.gameObject.SetActive(value: true);
			Vector3 localPosition = _descriptionTextLabel.transform.localPosition;
			localPosition.y = (float)num3 * 0.5f;
			localPosition.y += 30f;
			_descriptionTextLabel.transform.localPosition = localPosition;
			_descriptionTextLabel.gameObject.SetActive(value: true);
			float num4 = (float)Screen.width / (float)Screen.height;
			Rect uvRect = default(Rect);
			if (num4 > 1f)
			{
				uvRect.height = 1.359375f;
				uvRect.width = uvRect.height * num4;
				uvRect.x = (1f - uvRect.width) * 0.5f;
				uvRect.y = (1f - uvRect.height) * 0.5f;
			}
			else
			{
				uvRect.width = 1.359375f;
				uvRect.height = uvRect.width / num4;
				uvRect.x = (1f - uvRect.width) * 0.5f;
				uvRect.y = (1f - uvRect.height) * 0.5f;
			}
			_halfTransparentTexture.uvRect = uvRect;
			_halfTransparentTexture.gameObject.SetActive(value: true);
			yield return null;
			_cameraTexture.gameObject.SetActive(value: true);
		}
	}

	private void StopCamera()
	{
		if (!(_webCamTexture == null) && _webCamTexture.isPlaying)
		{
			_webCamTexture.Stop();
		}
	}

	private void LoadCameraRollImage(string path)
	{
		if (!loadCameraRollImageCalled)
		{
			loadCameraRollImageCalled = true;
			byte[] data = File.ReadAllBytes(path);
			Texture2D texture2D = new Texture2D(2, 2);
			texture2D.LoadImage(data);
			_qrCodeCameraText = QRCodeUtility.DecodeContentText(texture2D.GetPixels32(), texture2D.width, texture2D.height, isFromCamera: false);
			bool flag = false;
			if (!string.IsNullOrEmpty(_qrCodeCameraText) && QRCodeUtility.SetDeckFromQRCodeText(_qrCodeCameraText, _cardMasterId))
			{
				_qrScanSuccessee.Call();
				flag = true;
			}
			if (!flag)
			{
				string arg = ((!string.IsNullOrEmpty(_qrCodeCameraText)) ? Data.SystemText.Get("Card_0267") : Data.SystemText.Get("Card_0268"));
				_qrScanFailed.Call(arg);
			}
			UnityEngine.Object.Destroy(_qrCameraObject);
		}
	}
}
