using System;
using System.Collections.Generic;
using System.Globalization;
using Cute;
using UnityEngine;

namespace Wizard;

public class GatheringStartTimeSelectDialog : MonoBehaviour
{
	[SerializeField]
	private UtilityDrumrollScroll _daySelectDrum;

	[SerializeField]
	private UtilityDrumrollScroll _hourSelectDrum;

	[SerializeField]
	private UtilityDrumrollScroll _minuteSelectDrum;

	[SerializeField]
	private UtilityDrumrollScroll _amPmSelectDrum;

	[SerializeField]
	private UIPanel[] _alphaAnimationPanel;

	[SerializeField]
	private GameObject _daySelectArrowUp;

	[SerializeField]
	private GameObject _daySelectArrowDown;

	[SerializeField]
	private GameObject _hourSelectArrowUp;

	[SerializeField]
	private GameObject _hourSelectArrowDown;

	[SerializeField]
	private GameObject _minuteSelectArrowUp;

	[SerializeField]
	private GameObject _minuteSelectArrowDown;

	[SerializeField]
	private GameObject _ampmSelectArrowUp;

	[SerializeField]
	private GameObject _ampmSelectArrowDown;

	[SerializeField]
	private GameObject _amPmSelectRoot;

	[SerializeField]
	private GameObject _rootObject;

	private DialogBase _parentDialog;

	private List<DateTime> _dayList = new List<DateTime>();

	private List<int> _hourList = new List<int>();

	private List<int> _minuteList = new List<int>();

	private DateTime _selectDay;

	private int _selectHour;

	private int _selectMinute;

	private bool _isAm;

	private readonly Vector3 ENG_ROOT_POSITION = new Vector3(-65f, 0f, 0f);

	private const int DAY_SELECT_LIMIT = 5;

	public const int MINUTE_SELECT_INTERVAL = 15;

	public const int DEFAULT_START_TIME_MINUTE = 16;

	private const int AM_INDEX = 0;

	private const int PM_INDEX = 1;

	private int SelectHour
	{
		get
		{
			if (!IsEnableAmPmSelect)
			{
				return _selectHour;
			}
			if (_selectHour == 12)
			{
				if (!_isAm)
				{
					return 12;
				}
				return 0;
			}
			if (_isAm)
			{
				return _selectHour;
			}
			return _selectHour + 12;
		}
	}

	private DateTime SelectTime => new DateTime(_selectDay.Year, _selectDay.Month, _selectDay.Day, SelectHour, _selectMinute, 0);

	private bool IsEnableAmPmSelect => CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Eng.ToString();

	public static DialogBase Create(GameObject prefab, Action<DateTime> onDecide, DateTime defaultTime)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		dialogBase.SetPanelDepth(14);
		dialogBase.SetTitleLabel(Data.SystemText.Get("MyPage_0082"));
		GatheringStartTimeSelectDialog startDialog = UnityEngine.Object.Instantiate(prefab).GetComponent<GatheringStartTimeSelectDialog>();
		dialogBase.SetObj(startDialog.gameObject);
		startDialog.Initialize(defaultTime, dialogBase);
		dialogBase.onPushButton1 = delegate
		{
			onDecide(startDialog.SelectTime);
		};
		return dialogBase;
	}

	private void Initialize(DateTime defaultTime, DialogBase parent)
	{
		_parentDialog = parent;
		InitializeDaySelectDrum(defaultTime);
		InitializeHourSelectDrum(defaultTime);
		InitializeMinuteSelectDrum(defaultTime);
		if (IsEnableAmPmSelect)
		{
			_rootObject.transform.localPosition = ENG_ROOT_POSITION;
			InitializeAmPmSelectDrum();
		}
		_amPmSelectRoot.SetActive(IsEnableAmPmSelect);
	}

	private void InitializeDaySelectDrum(DateTime defaultTime)
	{
		int num = 0;
		List<string> list = new List<string>();
		DateTime item = PlayerStaticData.UserTime.GetNowTime().AddMinutes(15.0);
		string text = Data.SystemText.Get("System_DateLong");
		CultureInfo cultureInfo = new CultureInfo(Data.SystemText.Get("System_CultureInfo"), useUserOverride: false);
		for (int i = 0; i < 5; i++)
		{
			int year = item.Year;
			int month = item.Month;
			int day = item.Day;
			if (year == defaultTime.Year && month == defaultTime.Month && day == defaultTime.Day)
			{
				num = i;
			}
			list.Add(item.ToString(text, cultureInfo));
			_dayList.Add(item);
			item = item.AddDays(1.0);
		}
		_selectDay = _dayList[num];
		DaySelectCallBack(num);
		StartCoroutine(_daySelectDrum.CreateDrumrollScroll_Coroutine(list, num, DaySelectCallBack));
	}

	private void InitializeHourSelectDrum(DateTime defaultTime)
	{
		int num = 0;
		List<string> list = new List<string>();
		int num2 = 0;
		int num3 = 23;
		int num4 = defaultTime.Hour;
		if (IsEnableAmPmSelect)
		{
			num2 = 1;
			num3 = 12;
			_isAm = true;
			if (num4 == 0)
			{
				num4 = 12;
			}
			else if (num4 == 12)
			{
				_isAm = false;
			}
			else if (num4 > 12)
			{
				num4 -= 12;
				_isAm = false;
			}
		}
		for (int i = num2; i <= num3; i++)
		{
			list.Add(i.ToString("00"));
			if (i == num4)
			{
				num = i - num2;
			}
			_hourList.Add(i);
		}
		_selectHour = _hourList[num];
		HourSelectCallBack(num);
		StartCoroutine(_hourSelectDrum.CreateDrumrollScroll_Coroutine(list, num, HourSelectCallBack));
	}

	private void InitializeMinuteSelectDrum(DateTime defaultTime)
	{
		int num = 0;
		List<string> list = new List<string>();
		for (int i = 0; i < 60; i += 15)
		{
			list.Add(i.ToString("00"));
			if (i == defaultTime.Minute)
			{
				num = i / 15;
			}
			_minuteList.Add(i);
		}
		_selectMinute = _minuteList[num];
		MinuteSelectCallback(num);
		StartCoroutine(_minuteSelectDrum.CreateDrumrollScroll_Coroutine(list, num, MinuteSelectCallback));
	}

	private void DaySelectCallBack(int index)
	{
		_selectDay = _dayList[index];
		_daySelectArrowUp.SetActive(index > 0);
		_daySelectArrowDown.SetActive(index < _dayList.Count - 1);
	}

	private void HourSelectCallBack(int index)
	{
		_selectHour = _hourList[index];
		_hourSelectArrowUp.SetActive(index > 0);
		_hourSelectArrowDown.SetActive(index < _hourList.Count - 1);
	}

	private void MinuteSelectCallback(int index)
	{
		_selectMinute = _minuteList[index];
		_minuteSelectArrowUp.SetActive(index > 0);
		_minuteSelectArrowDown.SetActive(index < _minuteList.Count - 1);
	}

	private void InitializeAmPmSelectDrum()
	{
		int num = ((!_isAm) ? 1 : 0);
		List<string> list = new List<string>();
		list.Add(Data.SystemText.Get("System_0059"));
		list.Add(Data.SystemText.Get("System_0060"));
		StartCoroutine(_amPmSelectDrum.CreateDrumrollScroll_Coroutine(list, num, AmPmSelectCallBack));
		AmPmSelectCallBack(num);
	}

	private void AmPmSelectCallBack(int index)
	{
		_isAm = index == 0;
		_ampmSelectArrowUp.SetActive(index != 0);
		_ampmSelectArrowDown.SetActive(index == 0);
	}

	private void Update()
	{
		float panelAlpha = _parentDialog.PanelAlpha;
		UIPanel[] alphaAnimationPanel = _alphaAnimationPanel;
		foreach (UIPanel uIPanel in alphaAnimationPanel)
		{
			if (uIPanel != null)
			{
				uIPanel.alpha = panelAlpha;
			}
		}
	}
}
