using System;
using System.Text.RegularExpressions;
using UnityEngine;
using Wizard;

public class IDInput : MonoBehaviour
{
	[Serializable]
	public class LayoutSet
	{
		public GameObject _root;

		public UILabel[] _label;
	}

	[SerializeField]
	private UIButton[] m_InputBtns;

	[SerializeField]
	private UIButton m_InputClearBtn;

	private UIButton m_InputOKBtn;

	[SerializeField]
	private LayoutSet[] _layoutSet;

	private LayoutSet _currentLayout;

	[SerializeField]
	private UIButton _pasteButton;

	private int InputIndex;

	private int _maxIndex;

	public DialogBase CurrentDialogBase;

	private const string BrankText = "_";

	[HideInInspector]
	public string InputID { get; set; }

	public static IDInput Create(GameObject parentObj)
	{
		return NGUITools.AddChild(parentObj, UIManager.GetInstance().IdInputPrefab).GetComponent<IDInput>();
	}

	public static void StartInputDialog(string dialogTitle, int number, Action<string> onDecide)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.XL);
		dialogBase.SetTitleLabel(dialogTitle);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.DecisionBtn);
		IDInput input = Create(dialogBase.gameObject);
		input.CurrentDialogBase = dialogBase;
		input.InitInputID(number);
		dialogBase.onPushButton1 = delegate
		{
			onDecide(input.InputID);
		};
	}

	public void InitInputID(int maxCount)
	{
		InputIndex = 0;
		InputID = "";
		_maxIndex = maxCount - 1;
		for (int i = 0; i < m_InputBtns.Length; i++)
		{
			UIEventListener uIEventListener = UIEventListener.Get(m_InputBtns[i].gameObject);
			uIEventListener.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener.onClick, new UIEventListener.VoidDelegate(InputNum));
		}
		_pasteButton.onClick.Add(new EventDelegate(delegate
		{
			Paste();
		}));
		LayoutSet[] layoutSet = _layoutSet;
		foreach (LayoutSet layoutSet2 in layoutSet)
		{
			if (layoutSet2._label.Length == maxCount)
			{
				layoutSet2._root.SetActive(value: true);
				_currentLayout = layoutSet2;
			}
			else
			{
				layoutSet2._root.SetActive(value: false);
			}
		}
		if (_currentLayout == null)
		{
			Debug.LogError("未知の桁数です");
		}
		for (int num2 = 0; num2 < _currentLayout._label.Length; num2++)
		{
			_currentLayout._label[num2].text = "_";
		}
		UIEventListener uIEventListener2 = UIEventListener.Get(m_InputClearBtn.gameObject);
		uIEventListener2.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onClick, new UIEventListener.VoidDelegate(ClearNum));
		Invoke("SetButtonEnabled", 0.01f);
	}

	private void SetButtonEnabled()
	{
		CurrentDialogBase.SetButtonDisable(isEnableOK: true);
	}

	public void InputNum(GameObject g)
	{
		if (InputIndex <= _maxIndex)
		{
			string text = g.name;
			_currentLayout._label[InputIndex].text = text;
			JoinNums();
			InputIndex++;
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
			if (InputIndex > _maxIndex)
			{
				CurrentDialogBase.SetButtonDisable(isEnableOK: false);
			}
		}
	}

	public void ClearNum(GameObject g)
	{
		if (InputIndex > 0)
		{
			InputIndex--;
			_currentLayout._label[InputIndex].text = "_";
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_OFF);
			CurrentDialogBase.SetButtonDisable(isEnableOK: true);
		}
	}

	private void JoinNums()
	{
		string text = "";
		for (int i = 0; i < _currentLayout._label.Length && !(_currentLayout._label[i].text == "_"); i++)
		{
			text += _currentLayout._label[i].text;
		}
		InputID = text;
	}

	private void Paste()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
		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, _currentLayout._label.Length))
		{
			for (int num = 0; num < _currentLayout._label.Length; num++)
			{
				_currentLayout._label[num].text = "_";
			}
			for (int num2 = 0; num2 < clipboard.Length; num2++)
			{
				_currentLayout._label[num2].text = clipboard[num2].ToString();
				JoinNums();
			}
			InputIndex = clipboard.Length;
			CurrentDialogBase.SetButtonDisable(isEnableOK: false);
		}
	}
}
