using System;
using Cute;
using UnityEngine;

namespace Wizard;

public class WebViewHelper
{
	public enum WebViewType
	{
		NONE,
		INFO,
		HELP,
		COPYRIGHT,
		TOS,
		PRIVACY_POLICY,
		KOR_AUTHORITY,
		GACHARATE,
		LEGALTEXT,
		FUND_SETTLEMENT,
		TELECOMMUNICATION_POLICY,
		KOREA_CRYSTAL_PAGE,
		ANNOUNCE,
		SEALED_GACHARATE,
		LIMITED_INFO
	}

	public delegate void ActionWebviewOpen(DialogBase webviewDialog);

	public enum UrlType
	{
		POLICY,
		FACEBOOK,
		RECOMMENDED_DEVICE,
		GAME_GUIDE,
		NICONICO,
		NICONICO_PUBLISH
	}

	private const int NICONICO_NOTIFICATION_URLDIALOG_DEPTH = 3000;

	private bool _isDestroyedWebView;

	private DialogBase _webViewDialog;

	public DialogBase WebViewDialog => _webViewDialog;

	public void OpenWebView(WebViewType webviewtype, ActionWebviewOpen cbWebviewOpen = null, Action cbWebviewClose = null, Action onButton1 = null, Action onCancel = null)
	{
		_OpenWebView(webviewtype, cbWebviewOpen, cbWebviewClose, 0, 0, null, "", onButton1, onCancel);
	}

	public void OpenGachaRateWebView(int ratePackId, int packCategory = 0)
	{
		_OpenWebView(WebViewType.GACHARATE, null, null, ratePackId, packCategory);
	}

	public void OpenAnnounceWebView(string announceId, Action<DialogBase> onDialogOpening = null)
	{
		_OpenWebView(WebViewType.ANNOUNCE, null, null, 0, 0, announceId, "", null, null, onDialogOpening);
	}

	public void OpenWebViewDirectly(string openUrl, bool isLimited = false)
	{
		string directOpenUrl = CustomPreference.GetApplicationServerURL() + openUrl + GetWebviewBaseQueryParameters();
		if (isLimited)
		{
			_OpenWebView(WebViewType.LIMITED_INFO, null, null, 0, 0, null, directOpenUrl);
		}
		else
		{
			_OpenWebView(WebViewType.INFO, null, null, 0, 0, null, directOpenUrl);
		}
	}

	public void OpenWebviewWithPageId(string pageId, bool isLimited = false)
	{
		string text = CustomPreference.GetApplicationServerURL() + "webview2/index#/info_detail" + GetWebviewBaseQueryParameters();
		if (!string.IsNullOrEmpty(pageId))
		{
			text = text + "&id=" + pageId;
		}
		if (isLimited)
		{
			_OpenWebView(WebViewType.LIMITED_INFO, null, null, 0, 0, null, text);
		}
		else
		{
			_OpenWebView(WebViewType.INFO, null, null, 0, 0, null, text);
		}
	}

	private void _OpenWebView(WebViewType webviewtype, ActionWebviewOpen cbWebviewOpen = null, Action cbWebviewClose = null, int ratePackId = 0, int packCategory = 0, string announceId = null, string directOpenUrl = "", Action onButton1 = null, Action onCancel = null, Action<DialogBase> onDialogOpening = null)
	{
		VideoHostingUtil.CheckVideoHostingAndExecAction(delegate
		{
			if (webviewtype == WebViewType.ANNOUNCE)
			{
				OpenBrowserInsteadOfWebView(webviewtype, onDialogOpening, announceId);
			}
			else if (webviewtype == WebViewType.SEALED_GACHARATE)
			{
				OpenBrowserInsteadOfWebView(webviewtype, onDialogOpening, Data.ArenaData.SealedMyPageResponseData.ScheduleId.ToString());
			}
			else
			{
				string text = ratePackId.ToString();
				string text2 = packCategory.ToString();
				OpenBrowserInsteadOfWebView(webviewtype, onDialogOpening, text, text2);
			}
		});
	}

	private string GetWebviewBaseQueryParameters()
	{
		string encodedViewerId = Certification.GetEncodedViewerId();
		string encodedSessionId = Certification.GetEncodedSessionId();
		string encodedShortUdid = Certification.GetEncodedShortUdid();
		return "?lang=" + CustomPreference.GetTextLanguage() + "&HASH_ID=" + encodedViewerId + "&SID=" + encodedSessionId + "&SUDID=" + encodedShortUdid;
	}

	private string GetWebviewBaseQueryParametersLangOnly()
	{
		return "?lang=" + CustomPreference.GetTextLanguage();
	}

	private DialogBase OpenWebView_Inner(WebViewType webviewtype)
	{
		_isDestroyedWebView = false;
		string webViewDialogTitleId = GetWebViewDialogTitleId(webviewtype);
		SetWebviewOpenUrl(webviewtype);
		DialogBase dialogBase = (_webViewDialog = UIManager.GetInstance().CreateDialogClose(isSystem: false, dontChangeLabelColor: true));
		dialogBase.SetSize(DialogBase.Size.XL);
		if (webViewDialogTitleId != null)
		{
			dialogBase.SetTitleLabel(Data.SystemText.Get(webViewDialogTitleId));
		}
		else
		{
			dialogBase.SetTitleLabel(string.Empty);
			dialogBase.SetTitleLineVisible(visible: false);
			dialogBase.CloseOnOff(flag: false);
		}
		if (webviewtype == WebViewType.INFO || webviewtype == WebViewType.LIMITED_INFO || webviewtype == WebViewType.ANNOUNCE)
		{
			dialogBase.SetButtonLayout(DialogBase.ButtonLayout.NONE);
		}
		else
		{
			dialogBase.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		}
		dialogBase.SetButtonDelegate(delegate
		{
			DestroyWebView();
		}, delegate
		{
			DestroyWebView();
		}, delegate
		{
			DestroyWebView();
		});
		dialogBase.onCloseWithoutSelect = delegate
		{
			DestroyWebView();
		};
		dialogBase.SetAnchors();
		dialogBase.SetPanelDepth(3000);
		WebViewScreen webViewScreen = WebViewManager.getInstance().m_WebViewScreen;
		if (webviewtype == WebViewType.INFO || webviewtype == WebViewType.LIMITED_INFO || webviewtype == WebViewType.ANNOUNCE)
		{
			Vector4 webViewDisplayRect = dialogBase.GetWebViewDisplayRect();
			webViewScreen.CurBoxCollider.size = new Vector3(webViewDisplayRect.z, webViewDisplayRect.w, 0f);
			webViewScreen.CurBoxCollider.center = new Vector3(webViewDisplayRect.x, webViewDisplayRect.y, 0f);
		}
		else
		{
			webViewScreen.CurBoxCollider.size = Global.WEBVIEW_NORMAL_SIZE;
			webViewScreen.CurBoxCollider.center = Vector2.zero;
		}
		return dialogBase;
	}

	private string GetWebViewDialogTitleId(WebViewType webviewtype)
	{
		string result = null;
		switch (webviewtype)
		{
		case WebViewType.INFO:
		case WebViewType.ANNOUNCE:
			result = "Common_0036";
			break;
		case WebViewType.LIMITED_INFO:
			result = "Mission_0048";
			break;
		case WebViewType.HELP:
			result = "OtherTop_0006";
			break;
		case WebViewType.TOS:
			result = ((PlayerStaticData._tosAgreementState == PlayerStaticData.AgreementState.Reset) ? "OtherTop_0010_Update" : "OtherTop_0010");
			break;
		case WebViewType.PRIVACY_POLICY:
			result = ((PlayerStaticData._privacyPolicyAgreementState == PlayerStaticData.AgreementState.Reset) ? "OtherTop_0009_Update" : "OtherTop_0009");
			break;
		case WebViewType.TELECOMMUNICATION_POLICY:
			result = "OtherTop_0069";
			break;
		case WebViewType.KOR_AUTHORITY:
			result = ((PlayerStaticData.KorAuthorityAgreementState == PlayerStaticData.AgreementState.Reset) ? "OtherTop_0067_Update" : "OtherTop_0067");
			break;
		case WebViewType.COPYRIGHT:
			result = "OtherTop_0022";
			break;
		case WebViewType.GACHARATE:
			result = "Shop_0004";
			break;
		case WebViewType.LEGALTEXT:
			result = "Shop_0005";
			break;
		case WebViewType.FUND_SETTLEMENT:
			result = "Shop_0077";
			break;
		case WebViewType.KOREA_CRYSTAL_PAGE:
			result = "Shop_0126";
			break;
		case WebViewType.SEALED_GACHARATE:
			result = "Sealed_GachaRateWebViewTitle";
			break;
		}
		return result;
	}

	private void SetWebviewOpenUrl(WebViewType webviewtype)
	{
		switch (webviewtype)
		{
		}
	}

	private string GetBrowserUrl(WebViewType webviewtype)
	{
		string text = null;
		switch (webviewtype)
		{
		case WebViewType.INFO:
		case WebViewType.LIMITED_INFO:
			text = "URL_0004";
			break;
		case WebViewType.HELP:
			text = "URL_0011";
			break;
		case WebViewType.TOS:
			text = "URL_0005";
			break;
		case WebViewType.PRIVACY_POLICY:
			text = "URL_0007";
			break;
		case WebViewType.TELECOMMUNICATION_POLICY:
			text = "URL_0018";
			break;
		case WebViewType.COPYRIGHT:
			text = "URL_0006";
			break;
		case WebViewType.GACHARATE:
			text = "URL_0010";
			break;
		case WebViewType.LEGALTEXT:
			text = "URL_0009";
			break;
		case WebViewType.FUND_SETTLEMENT:
			text = "URL_0008";
			break;
		case WebViewType.KOREA_CRYSTAL_PAGE:
			text = "URL_0015";
			break;
		case WebViewType.ANNOUNCE:
			text = "URL_0016";
			break;
		case WebViewType.SEALED_GACHARATE:
			text = "URL_0017";
			break;
		}
		return text + "_steam";
	}

	private void OpenBrowserInsteadOfWebView(WebViewType webviewtype, Action<DialogBase> onDialogOpening, params string[] param)
	{
		string webViewDialogTitleId = GetWebViewDialogTitleId(webviewtype);
		string browserUrl = GetBrowserUrl(webviewtype);
		SystemText systemText = Data.SystemText;
		UIManager.ShowDialogUrl(systemText.Get(webViewDialogTitleId), systemText.Get(browserUrl, param), onDialogOpening);
	}

	public void DestroyWebView()
	{
		if (!_isDestroyedWebView && WebViewManager.HasInstance)
		{
			_isDestroyedWebView = true;
			WebViewManager instance = WebViewManager.getInstance();
			instance.UnloadWebView();
			instance.SetCallback(null);
		}
	}

	public void CloseWebViewDialog()
	{
		if (IsOpenWebViewDialog())
		{
			_webViewDialog.Close();
			_webViewDialog = null;
			DestroyWebView();
		}
	}

	public bool IsOpenWebViewDialog()
	{
		if (_webViewDialog != null)
		{
			return _webViewDialog.IsOpen();
		}
		return false;
	}

	public DialogBase CreateOpenURLWindow(UrlType type)
	{
		string applicationServerURL = CustomPreference.GetApplicationServerURL();
		string text = "?lang=" + CustomPreference.GetTextLanguage();
		string openUrl = null;
		string key = null;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		SystemText systemText = Data.SystemText;
		switch (type)
		{
		case UrlType.POLICY:
			dialogBase.SetPanelDepth(3000);
			openUrl = applicationServerURL + "information/policy" + text;
			key = "OtherTop_0009";
			break;
		case UrlType.FACEBOOK:
			key = "Account_0038";
			break;
		case UrlType.RECOMMENDED_DEVICE:
			key = "Common_0134";
			openUrl = "https://shadowverse.jp/device.php";
			break;
		case UrlType.GAME_GUIDE:
			openUrl = systemText.Get("URL_0012_steam");
			key = "OtherTop_0021";
			break;
		case UrlType.NICONICO:
			dialogBase.SetPanelDepth(3000);
			key = "VideoHosting_0034";
			openUrl = "https://account.nicovideo.jp/rules/account";
			break;
		case UrlType.NICONICO_PUBLISH:
			dialogBase.SetPanelDepth(3000);
			key = "VideoHosting_0035";
			openUrl = "http://live.nicovideo.jp/static/rule.html";
			break;
		}
		dialogBase.SetTitleLabel(systemText.Get(key));
		dialogBase.SetText(systemText.Get("Common_0208"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetButtonText(systemText.Get("Dia_Web_001_Button"));
		dialogBase.onPushButton1 = delegate
		{
			PrepareOpenUrl(delegate
			{
				if (type == UrlType.FACEBOOK)
				{
					UIManager.GetInstance().AccountTransferHelper.DataTransferByFaceBook();
				}
				else
				{
					BrowserURL.Open(openUrl);
				}
			});
		};
		return dialogBase;
	}

	public void PrepareOpenUrl(Action onFinish)
	{
		onFinish();
	}

	public void OpenUrl(string url)
	{
		PrepareOpenUrl(delegate
		{
			BrowserURL.Open(url);
		});
	}
}
