using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Wizard;

namespace Cute;

public class WebViewManager : MonoBehaviour
{
	private static WebViewManager instance;

	[SerializeField]
	public WebViewScreen m_WebViewScreen;

	private Action<string> onError;

	private Action<string> onLoaded;

	private Vector4 marginNow;

	private float screenDpi;

	private bool screenDpiChangedWhileInvisible;

	private static readonly Dictionary<string, string> FontFamilyDict = new Dictionary<string, string>
	{
		{
			Global.LANG_TYPE.Jpn.ToString(),
			"font_jpn"
		},
		{
			Global.LANG_TYPE.Kor.ToString(),
			"font_kor"
		},
		{
			Global.LANG_TYPE.Cht.ToString(),
			"font_cht"
		},
		{
			Global.LANG_TYPE.Chs.ToString(),
			"font_chs"
		}
	};

	public Action<string> Callback { get; set; }

	public static bool HasInstance => instance != null;

	public Action<string> OnError
	{
		set
		{
			onError = value;
		}
	}

	public Action<string> OnLoaded
	{
		set
		{
			onLoaded = value;
		}
	}

	public bool Visible { get; private set; }

	public Action OnDpiChangedAction { private get; set; }

	public static WebViewManager getInstance()
	{
		return instance;
	}

	private void UpdateScreenDpi()
	{
		screenDpi = Screen.dpi;
	}

	private bool IsScreenDpiChanged()
	{
		return Math.Abs(screenDpi - Screen.dpi) > 0.1f;
	}

	private void Awake()
	{
		instance = this;
		SetMargins(Screen.width / 30, Screen.height / 5, Screen.width / 30, Screen.height / 14);
		UpdateScreenDpi();
	}

	private void OnLoadedCallback(string msg)
	{
		if (onLoaded != null)
		{
			onLoaded(msg);
		}
	}

	private string GetFontFamilyName()
	{
		if (!FontFamilyDict.TryGetValue(CustomPreference.GetTextLanguage(), out var value))
		{
			return "font_alphabet";
		}
		return value;
	}

	private void OnErrorCallback(string error)
	{
		if (onError != null)
		{
			onError(error);
		}
	}

	private void OnApplicationFocus(bool focus)
	{
		if (focus && IsScreenDpiChanged())
		{
			UpdateScreenDpi();
			if (Visible)
			{
				OnDpiChange();
			}
			else
			{
				screenDpiChangedWhileInvisible = true;
			}
		}
	}

	private void OnDpiChange()
	{
		if (OnDpiChangedAction != null)
		{
			OnDpiChangedAction();
		}
		Vector4 marginBackCoroutine = marginNow;
		SetMargins((int)marginBackCoroutine.x, (int)marginBackCoroutine.y, (int)marginBackCoroutine.z - 1, (int)marginBackCoroutine.w - 1);
		StartCoroutine(SetMarginBackCoroutine(marginBackCoroutine));
	}

	private IEnumerator SetMarginBackCoroutine(Vector4 oldMargin)
	{
		yield return new WaitForSecondsRealtime(0.5f);
		SetMargins((int)oldMargin.x, (int)oldMargin.y, (int)oldMargin.z, (int)oldMargin.w);
	}

	public void ClearFontFilePaths()
	{
	}

	private void OnDestroy()
	{
		instance = null;
		ClearFontFilePaths();
	}

	public void InitCustomFontFileInfo(Dictionary<string, string> fileNamePathDict, int currentCustomFontFileIndex)
	{
	}

	private void InitIOSCustomFont()
	{
	}

	private void DisableIOSCustomFont()
	{
	}

	public void OpenWeb(string url)
	{
		m_WebViewScreen.CurBoxCollider.enabled = true;
		SetMargins(UIManager.GetInstance().UIManagerRoot, m_WebViewScreen);
		LoadWeb(url);
		SetVisible(visible: true);
	}

	public void LoadWeb(string url)
	{
	}

	public void ClearHistory()
	{
	}

	public void SetMargins(int leftMargin, int topMargin, int rightMargin, int bottomMargin)
	{
		marginNow = new Vector4(leftMargin, topMargin, rightMargin, bottomMargin);
	}

	public void SetMargins(UIRoot trgUIRoot, WebViewScreen trgScreen)
	{
		float arg = trgUIRoot.activeHeight;
		Func<float, float, float, float, int> func = delegate(float deviceSize, float activeScreenSize, float screenSize, float pos)
		{
			screenSize += pos * 2f;
			screenSize /= activeScreenSize;
			screenSize = 1f - screenSize;
			return (int)(screenSize * deviceSize * 0.5f);
		};
		Vector2 deviceResolution = Toolbox.QualityManager.deviceResolution;
		float num = Mathf.Max(deviceResolution.x, deviceResolution.y);
		BoxCollider curBoxCollider = trgScreen.CurBoxCollider;
		int num2 = (int)(num - num * AspectCamera.SafeAreaRate);
		int num3 = (int)(num * AspectCamera.SafeAreaRate) / 30 + num2 / 2;
		int rightMargin = num3;
		int topMargin = func(deviceResolution.y, arg, curBoxCollider.size.y, curBoxCollider.center.y);
		int bottomMargin = func(deviceResolution.y, arg, curBoxCollider.size.y, 0f - curBoxCollider.center.y);
		getInstance().SetMargins(num3, topMargin, rightMargin, bottomMargin);
	}

	public void UnloadWebView()
	{
		m_WebViewScreen.CurBoxCollider.enabled = false;
		SetVisible(visible: false);
		LoadWeb(CustomPreference.GetApplicationServerURL() + "information/blank");
		Callback = null;
		OnLoaded = null;
		OnError = null;
	}

	public void DestroyWebViewObject()
	{
	}

	public void EvaluateJS(string js)
	{
	}

	public void SetVisible(bool visible)
	{
		Visible = visible;
		if (visible && screenDpiChangedWhileInvisible)
		{
			screenDpiChangedWhileInvisible = false;
			OnDpiChange();
		}
	}

	public void ClearCaches()
	{
	}

	public void Reload()
	{
	}

	public bool CanGoBack()
	{
		return false;
	}

	public void GoBack()
	{
	}

	public void Screenshot()
	{
	}

	public void SetScreenshotData(int width, int height)
	{
	}

	public void SetCallback(Action<string> cb)
	{
		Callback = cb;
	}

	public void CacheClear()
	{
		LoadWeb(CustomPreference.GetApplicationServerURL() + "information/blank");
	}
}
