using System;
using System.Text.RegularExpressions;
using Cute;
using UnityEngine;

namespace Wizard;

public class GatheringIDInput : MonoBehaviour
{
	[SerializeField]
	private UIButton _searchButton;

	[SerializeField]
	private UIButton _pasteButton;

	[SerializeField]
	private UIInputWizard _idInput;

	public Action<GatheringGetInfoTask> OnGetInfo { get; set; }

	private void Start()
	{
		_searchButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickSearchButton();
		}));
		_idInput.onChange.Add(new EventDelegate(delegate
		{
			OnChangeInputId();
		}));
		_pasteButton.onClick.Add(new EventDelegate(delegate
		{
			Paste();
		}));
		UIManager.SetObjectToGrey(_searchButton.gameObject, b: true);
	}

	private void OnChangeInputId()
	{
		UIManager.SetObjectToGrey(_searchButton.gameObject, !UIUtil.IsValidIdDigits(_idInput.value, 6));
	}

	private void OnClickSearchButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		GatheringGetInfoTask task = new GatheringGetInfoTask();
		task.SetParameter(_idInput.value);
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			OnSuccessGetGatheringInfo(task);
		}));
	}

	private void OnSuccessGetGatheringInfo(GatheringGetInfoTask task)
	{
		OnGetInfo(task);
	}

	private void Paste()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		string clipboard = ClipboardHelper.Clipboard;
		clipboard = Regex.Replace(clipboard, "[０-９]", (Match match) => ((char)(match.Value[0] - 65296 + 48)).ToString());
		clipboard = Regex.Replace(clipboard, "\\s", "");
		if (UIUtil.IsValidIdDigits(clipboard, 6))
		{
			_idInput.value = clipboard;
			OnChangeInputId();
		}
	}
}
