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

public class SpinBoxButton : MonoBehaviour
{
	public void Init(SpinBoxParam spinBoxParam, UILabel lBox, UILabel lPoint)
	{
		this.m_lBox = lBox;
		this.m_lPoint = lPoint;
		this.maxSpinBoxNumber = spinBoxParam.m_maxNumber;
		this.minSpinBoxNumber = spinBoxParam.m_minNumber;
		this.amountPerPoint = spinBoxParam.amountPerPoint;
		if (BaseMgr<ProfileMgr>.Instance.m_maidStatus != null)
		{
			this.maxMaidPoint = SceneEdit.Instance.maidPoint;
		}
		this.m_myUiButton = base.gameObject.GetComponent<UIButton>();
		this.activeColor = new Color(this.m_myUiButton.defaultColor.r, this.m_myUiButton.defaultColor.g, this.m_myUiButton.defaultColor.b, 1f);
		this.inActiveColor = this.m_myUiButton.defaultColor;
		this.InitDownButton();
	}

	private void InitDownButton()
	{
		string name = base.gameObject.transform.name;
		if (name == "Down")
		{
			this.m_myUiButton.enabled = false;
		}
	}

	private void Update()
	{
		if (this.m_isPressed && this.m_updateTime < Time.realtimeSinceStartup)
		{
			this.OnUpDownButtonClick(base.transform.name);
			this.m_updateTime = Time.realtimeSinceStartup + this.m_updateInterval;
		}
		if (this.GetDividedPoint() > 0)
		{
			this.SetDownButtonActive(true);
		}
		else
		{
			this.SetDownButtonActive(false);
		}
		if (this.GetMaidPoint() > 0 && int.Parse(this.m_lBox.text) < this.maxSpinBoxNumber)
		{
			this.SetUpButtonActive(true);
		}
		else
		{
			this.SetUpButtonActive(false);
		}
	}

	private void OnPress(bool isDown)
	{
		if (isDown)
		{
			this.m_isPressed = true;
			this.m_updateTime = Time.realtimeSinceStartup + this.m_updateInterval;
			this.OnUpDownButtonClick(base.transform.name);
		}
		else
		{
			this.m_isPressed = false;
		}
	}

	public void OnUpDownButtonClick()
	{
		this.OnUpDownButtonClick(UIButton.current.name);
	}

	public void OnUpDownButtonClick(string clickBtnName)
	{
		SpinBoxButton.SpinBoxButtonType spinBoxButtonType = SpinBoxButton.SpinBoxButtonType.None;
		if (clickBtnName == SpinBoxButton.SpinBoxButtonType.Up.ToString())
		{
			spinBoxButtonType = SpinBoxButton.SpinBoxButtonType.Up;
		}
		else if (clickBtnName == SpinBoxButton.SpinBoxButtonType.Down.ToString())
		{
			spinBoxButtonType = SpinBoxButton.SpinBoxButtonType.Down;
		}
		else
		{
			Debug.LogError(string.Format("不適切なボタンが押されました。ボタン名：{0}", clickBtnName));
		}
		if (this.CanUpDownSpineBox(spinBoxButtonType))
		{
			this.UpdataPoint(spinBoxButtonType);
		}
	}

	private bool CanUpDownSpineBox(SpinBoxButton.SpinBoxButtonType spinBoxButtonType)
	{
		int num;
		if (!int.TryParse(this.m_lBox.text, out num))
		{
			return false;
		}
		if (spinBoxButtonType != SpinBoxButton.SpinBoxButtonType.Up)
		{
			return spinBoxButtonType == SpinBoxButton.SpinBoxButtonType.Down && (SceneEdit.Instance.maidPoint < this.maxMaidPoint && num > 0) && this.GetDividedPoint() > 0;
		}
		return SceneEdit.Instance.maidPoint > 0 && num < this.maxSpinBoxNumber;
	}

	private void UpdataPoint(SpinBoxButton.SpinBoxButtonType spinBoxButtonType)
	{
		int spinBoxPointToUse = this.GetSpinBoxPointToUse(spinBoxButtonType);
		int num = Math.Min(this.diffMaidPoint * this.amountPerPoint, spinBoxPointToUse);
		int num2 = 0;
		if (spinBoxButtonType != SpinBoxButton.SpinBoxButtonType.Up)
		{
			if (spinBoxButtonType == SpinBoxButton.SpinBoxButtonType.Down)
			{
				this.SetStatusDispPoint(-num);
				num2 = this.diffMaidPoint;
				this.SetDividedPoint(-num2);
			}
		}
		else
		{
			this.SetStatusDispPoint(num);
			num2 = -this.diffMaidPoint;
			this.SetDividedPoint(this.diffMaidPoint);
		}
		this.SetMaidPoint(num2);
		BaseMgr<ProfileMgr>.Instance.CloseSubWindowIfOpen();
		BaseMgr<ProfileMgr>.Instance.LoadMaidParamData();
	}

	private void SetStatusDispPoint(int spinBoxDispPoint)
	{
		if (this.GetSpinBoxName() == "Lovely")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseLovely += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "Elegance")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseElegance += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "Charm")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseCharm += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "Hentai")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseHentai += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "MValue")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseMvalue += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "Inran")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseInyoku += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "Housi")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseHousi += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "Cooking")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseCooking += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "Dance")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseDance += spinBoxDispPoint;
		}
		else if (this.GetSpinBoxName() == "Vocal")
		{
			BaseMgr<ProfileMgr>.Instance.m_maidStatus.baseVocal += spinBoxDispPoint;
		}
	}

	private int GetSpinBoxPointToUse(SpinBoxButton.SpinBoxButtonType spinBoxButtonType)
	{
		if (spinBoxButtonType == SpinBoxButton.SpinBoxButtonType.Up)
		{
			return this.maxSpinBoxNumber - int.Parse(this.m_lBox.text);
		}
		if (spinBoxButtonType != SpinBoxButton.SpinBoxButtonType.Down)
		{
			return 0;
		}
		return int.Parse(this.m_lBox.text);
	}

	private void SetMaidPoint(int number)
	{
		SceneEdit.Instance.maidPoint += number;
	}

	private int GetMaidPoint()
	{
		return SceneEdit.Instance.maidPoint;
	}

	private int GetDividedPoint()
	{
		GameObject gameObject = base.transform.parent.gameObject;
		int result = 0;
		if (BaseMgr<ProfileMgr>.Instance.m_dicDividedPoint.TryGetValue(gameObject.name, out result))
		{
			return result;
		}
		Debug.LogError("不適切なスピンボックスボタンが押下されました。");
		return result;
	}

	private void SetDividedPoint(int point)
	{
		string spinBoxName = this.GetSpinBoxName();
		if (BaseMgr<ProfileMgr>.Instance.m_dicDividedPoint.ContainsKey(spinBoxName))
		{
			Dictionary<string, int> dicDividedPoint;
			string key;
			(dicDividedPoint = BaseMgr<ProfileMgr>.Instance.m_dicDividedPoint)[key = spinBoxName] = dicDividedPoint[key] + point;
		}
		else
		{
			Debug.LogError("不適切なスピンボックスボタンが押下されました。");
		}
	}

	private string GetSpinBoxName()
	{
		return base.transform.parent.gameObject.name;
	}

	private void SetUpButtonActive(bool active)
	{
		string name = base.gameObject.transform.name;
		if (name == "Up")
		{
			if (active)
			{
				if (active != this.m_currentUpActive)
				{
					this.m_myUiButton.defaultColor = this.activeColor;
					this.m_myUiButton.enabled = active;
					this.m_currentUpActive = active;
				}
			}
			else if (active != this.m_currentUpActive)
			{
				this.m_myUiButton.defaultColor = this.inActiveColor;
				this.m_myUiButton.enabled = active;
				this.m_currentUpActive = active;
			}
		}
	}

	private void SetDownButtonActive(bool active)
	{
		string name = base.gameObject.transform.name;
		if (name == "Down")
		{
			if (active)
			{
				if (active != this.m_currentDownActive)
				{
					this.m_myUiButton.defaultColor = this.activeColor;
					this.m_myUiButton.enabled = active;
					this.m_currentDownActive = active;
				}
			}
			else if (active != this.m_currentDownActive)
			{
				this.m_myUiButton.defaultColor = this.inActiveColor;
				this.m_myUiButton.enabled = active;
				this.m_currentDownActive = active;
			}
		}
	}

	private UILabel m_lBox;

	private UILabel m_lPoint;

	private UIButton m_myUiButton;

	private bool m_isPressed;

	private bool m_currentDownActive;

	private bool m_currentUpActive;

	private int maxSpinBoxNumber = 9999;

	private int minSpinBoxNumber;

	private int maxMaidPoint;

	private int amountPerPoint;

	private int diffMaidPoint = 1;

	[SerializeField]
	private float m_updateInterval;

	private float m_updateTime;

	private Color activeColor;

	private Color inActiveColor;

	private enum SpinBoxButtonType
	{
		None = -1,
		Up,
		Down
	}
}
