using System;
using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;
using UnityEngine.UI;
using wf;
using Yotogis;

public class YotogiCommandMenuOVR : MonoBehaviour
{
	public void Awake()
	{
		this.clone_src_.gameObject.SetActive(false);
		this.command_height_ = (int)(this.clone_src_.rect.height * this.clone_src_.localScale.y);
	}

	public void Start()
	{
		this.is_attach_hand_mode_ = false;
		if (this.devideType == GameMain.VRDeviceType.RIFT_TOUCH || this.devideType == GameMain.VRDeviceType.VIVE)
		{
			this.is_attach_hand_mode_ = true;
			this.use_adjustment_transform_ = ((this.devideType != GameMain.VRDeviceType.RIFT_TOUCH) ? this.adjustment_htc_pos_ : this.adjustment_rift_pos_);
		}
		this.command_draw_mgr_.ChangeDeviceMode(this.devideType);
		this.draw_gruoup_ = this.command_draw_mgr_.GetComponent<CanvasGroup>();
		if (this.is_attach_hand_mode_)
		{
			GameMain.Instance.OvrMgr.OvrCamera.HandLimitMode = AVRControllerBehavior.LIMIT_MODE.NO_WARP;
			GameMain.Instance.OvrMgr.OvrCamera.HandYotogiMode = true;
			this.AttachHand();
		}
	}

	private void OnDestroy()
	{
		if (GameMain.Instance.VRMode)
		{
			GameMain.Instance.OvrMgr.OvrCamera.HandLimitMode = AVRControllerBehavior.LIMIT_MODE.NORMAL;
			GameMain.Instance.OvrMgr.OvrCamera.HandYotogiMode = false;
		}
	}

	private void AttachHand()
	{
		if (GameMain.Instance.VRMode)
		{
			this.controller_L_ = GameMain.Instance.OvrMgr.OvrCamera.GetVRControllerButtons(true);
			this.controller_R_ = GameMain.Instance.OvrMgr.OvrCamera.GetVRControllerButtons(false);
			this.controller_trans_L_ = this.controller_L_.transform;
			this.controller_trans_R_ = this.controller_R_.transform;
			this.controller_attach_side_L_ = false;
		}
	}

	public void SetOnEnterEvetn(Action<Skill.Data.Command.Data> call_event)
	{
		this.call_event_ = call_event;
	}

	public void ReserveSkillCommand(Skill.Data.Command skill_command)
	{
		this.DestroySkillCommand();
		for (int i = 0; i < skill_command.data.Length; i++)
		{
			if (this.command_dic_.ContainsKey(skill_command.data[i]))
			{
				return;
			}
			Skill.Data.Command.Data data = skill_command.data[i];
			RectTransform rectTransform = UnityEngine.Object.Instantiate<RectTransform>(this.clone_src_);
			rectTransform.SetParent(this.command_panel_, false);
			rectTransform.GetComponent<Text>().text = data.basic.name;
			rectTransform.GetComponent<Localize>().SetTerm(data.basic.termName);
			rectTransform.gameObject.SetActive(false);
			this.command_dic_.Add(data, rectTransform);
		}
	}

	public void ClearCommand()
	{
		foreach (KeyValuePair<Skill.Data.Command.Data, RectTransform> keyValuePair in this.command_dic_)
		{
			keyValuePair.Value.gameObject.SetActive(false);
		}
	}

	public void DestroySkillCommand()
	{
		foreach (KeyValuePair<Skill.Data.Command.Data, RectTransform> keyValuePair in this.command_dic_)
		{
			UnityEngine.Object.Destroy(keyValuePair.Value.gameObject);
		}
		this.command_dic_.Clear();
		this.select_command_ = null;
	}

	public void AddCommand(Skill.Data.Command.Data command_data)
	{
		if (!this.command_dic_.ContainsKey(command_data))
		{
			return;
		}
		this.command_dic_[command_data].gameObject.SetActive(true);
	}

	public void FixCommand()
	{
		int num = int.MaxValue;
		int num2 = 0;
		Skill.Data.Command.Data data = null;
		foreach (KeyValuePair<Skill.Data.Command.Data, RectTransform> keyValuePair in this.command_dic_)
		{
			if (keyValuePair.Value.gameObject.activeSelf)
			{
				Vector3 localPosition = keyValuePair.Value.localPosition;
				localPosition.y = (float)(this.command_height_ * (num2++ * -1));
				keyValuePair.Value.localPosition = localPosition;
				if (keyValuePair.Key.basic.id < num)
				{
					num = keyValuePair.Key.basic.id;
					data = keyValuePair.Key;
				}
			}
		}
		if (data != null)
		{
			if (this.select_command_ == null)
			{
				this.select_command_ = data;
			}
			bool visible = this.visible;
			if (!visible)
			{
				this.visible = true;
			}
			this.Select(this.select_command_);
			if (!visible)
			{
				this.visible = false;
			}
		}
		else
		{
			this.select_command_ = null;
			this.visible = false;
		}
	}

	public bool Next()
	{
		if (!this.visible)
		{
			return false;
		}
		if (this.cbl_class != null)
		{
			this.cbl_class.Next();
			this.cbl_class.Enter();
		}
		List<Skill.Data.Command.Data> list = new List<Skill.Data.Command.Data>();
		foreach (KeyValuePair<Skill.Data.Command.Data, RectTransform> keyValuePair in this.command_dic_)
		{
			if (keyValuePair.Value.gameObject.activeSelf)
			{
				list.Add(keyValuePair.Key);
			}
		}
		list.Sort((Skill.Data.Command.Data a, Skill.Data.Command.Data b) => a.basic.id - b.basic.id);
		int i = 0;
		while (i < list.Count)
		{
			if (list[i] != this.select_command_)
			{
				i++;
			}
			else
			{
				if (this.is_commandloop_)
				{
					if (i + 1 < list.Count)
					{
						this.Select(list[i + 1]);
					}
					else
					{
						this.Select(list[0]);
					}
					return true;
				}
				if (i + 1 < list.Count)
				{
					this.Select(list[i + 1]);
					return true;
				}
				return false;
			}
		}
		return false;
	}

	public bool Prev()
	{
		if (!this.visible)
		{
			return false;
		}
		if (this.cbl_class != null)
		{
			this.cbl_class.Prev();
			this.cbl_class.Enter();
		}
		List<Skill.Data.Command.Data> list = new List<Skill.Data.Command.Data>();
		foreach (KeyValuePair<Skill.Data.Command.Data, RectTransform> keyValuePair in this.command_dic_)
		{
			if (keyValuePair.Value.gameObject.activeSelf)
			{
				list.Add(keyValuePair.Key);
			}
		}
		list.Sort((Skill.Data.Command.Data a, Skill.Data.Command.Data b) => a.basic.id - b.basic.id);
		int i = 0;
		while (i < list.Count)
		{
			if (list[i] != this.select_command_)
			{
				i++;
			}
			else
			{
				if (this.is_commandloop_)
				{
					if (0 <= i - 1)
					{
						this.Select(list[i - 1]);
					}
					else
					{
						this.Select(list[list.Count - 1]);
					}
					return true;
				}
				if (0 <= i - 1)
				{
					this.Select(list[i - 1]);
					return true;
				}
				return false;
			}
		}
		return false;
	}

	public void Enter()
	{
		if (!this.visible)
		{
			return;
		}
		if (this.select_command_ != null && this.call_event_ != null)
		{
			this.call_event_(this.select_command_);
		}
	}

	public void Select(Skill.Data.Command.Data command_data)
	{
		if (!this.visible)
		{
			return;
		}
		if (!this.command_dic_[command_data].gameObject.activeSelf)
		{
			int num = -1;
			foreach (KeyValuePair<Skill.Data.Command.Data, RectTransform> keyValuePair in this.command_dic_)
			{
				if (keyValuePair.Value.gameObject.activeSelf && num < keyValuePair.Key.basic.id)
				{
					num = keyValuePair.Key.basic.id;
					command_data = keyValuePair.Key;
				}
			}
		}
		int num2 = 0;
		foreach (KeyValuePair<Skill.Data.Command.Data, RectTransform> keyValuePair2 in this.command_dic_)
		{
			if (keyValuePair2.Value.gameObject.activeSelf)
			{
				keyValuePair2.Value.GetComponent<Text>().color = ((keyValuePair2.Key != command_data) ? Color.gray : Color.white);
				if (keyValuePair2.Key == command_data)
				{
					Vector3 localPosition = this.command_panel_.transform.localPosition;
					localPosition.y = (float)this.GetPanelPosY(num2);
					if (this.select_command_ == command_data)
					{
						this.command_panel_.transform.localPosition = localPosition;
					}
					else
					{
						iTween.Stop(this.command_panel_.gameObject);
						iTween component = this.command_panel_.GetComponent<iTween>();
						if (component != null)
						{
							UnityEngine.Object.DestroyImmediate(component);
						}
						Hashtable args = TweenHash.EaseOutSine(TweenHash.Type.Position, localPosition, 0.15f);
						iTween.MoveTo(this.command_panel_.gameObject, args);
					}
					this.select_command_ = command_data;
				}
				num2++;
			}
		}
	}

	public bool visible
	{
		get
		{
			return base.gameObject.activeSelf;
		}
		set
		{
			base.gameObject.SetActive(value);
		}
	}

	public float alpha
	{
		get
		{
			return this.draw_gruoup_.alpha;
		}
		set
		{
			this.draw_gruoup_.alpha = value;
		}
	}

	public YotogiCommandMenuOVRWithChubLip cbl_class
	{
		get
		{
			return this.cbl_class_;
		}
	}

	private void Update()
	{
		if (this.is_attach_hand_mode_)
		{
			this.alpha = (float)((!GameMain.Instance.OvrMgr.OvrCamera.IsHandPenMode) ? 1 : 0);
			if (GameMain.Instance.OvrMgr.OvrCamera.IsHandPenMode)
			{
				return;
			}
			bool handYotogiMode = GameMain.Instance.OvrMgr.ovr_obj.left_controller.controller.HandYotogiMode;
			bool handYotogiMode2 = GameMain.Instance.OvrMgr.ovr_obj.right_controller.controller.HandYotogiMode;
			if (this.m_bBackHandL != handYotogiMode)
			{
				if (handYotogiMode)
				{
					this.controller_attach_side_L_ = true;
				}
				else if (handYotogiMode2)
				{
					this.controller_attach_side_L_ = false;
				}
				this.m_bBackHandL = handYotogiMode;
			}
			if (this.m_bBackHandR != handYotogiMode2)
			{
				if (handYotogiMode2)
				{
					this.controller_attach_side_L_ = false;
				}
				else if (handYotogiMode)
				{
					this.controller_attach_side_L_ = true;
				}
				this.m_bBackHandR = handYotogiMode2;
			}
			if (!handYotogiMode && !handYotogiMode2)
			{
				this.alpha = 0f;
				return;
			}
			if (this.controller_attach_side_L_ && !handYotogiMode)
			{
				return;
			}
			if (!this.controller_attach_side_L_ && !handYotogiMode2)
			{
				return;
			}
			AVRControllerButtons avrcontrollerButtons;
			Transform transform;
			if (this.controller_attach_side_L_)
			{
				avrcontrollerButtons = this.controller_L_;
				transform = this.controller_trans_L_;
				if (handYotogiMode2 && this.controller_R_.GetPressDown(AVRControllerButtons.BTN.TRIGGER))
				{
					this.controller_attach_side_L_ = false;
					this.change_hand_ = true;
				}
			}
			else
			{
				avrcontrollerButtons = this.controller_R_;
				transform = this.controller_trans_R_;
				if (handYotogiMode && this.controller_L_.GetPressDown(AVRControllerButtons.BTN.TRIGGER))
				{
					this.controller_attach_side_L_ = true;
					this.change_hand_ = true;
				}
			}
			if (avrcontrollerButtons != null)
			{
				if (avrcontrollerButtons.Type == AVRControllerButtons.TYPE.TOUCH)
				{
					Vector2 axis = avrcontrollerButtons.GetAxis();
					if (axis.y < -0.3f)
					{
						if (!this.stick_down_)
						{
							this.Next();
						}
						this.stick_down_ = true;
					}
					else if (axis.y > 0.3f)
					{
						if (!this.stick_down_)
						{
							this.Prev();
						}
						this.stick_down_ = true;
					}
					else
					{
						this.stick_down_ = false;
					}
				}
				else if (avrcontrollerButtons.Type == AVRControllerButtons.TYPE.VIVE && avrcontrollerButtons.GetPressUp(AVRControllerButtons.BTN.STICK_PAD))
				{
					Vector2 axis2 = avrcontrollerButtons.GetAxis();
					if (axis2.y < -0.3f)
					{
						this.Next();
					}
					else if (axis2.y > 0.3f)
					{
						this.Prev();
					}
				}
			}
			if (avrcontrollerButtons != null && !avrcontrollerButtons.GetPress(AVRControllerButtons.BTN.VIRTUAL_L_CLICK) && avrcontrollerButtons.GetPressUp(AVRControllerButtons.BTN.TRIGGER))
			{
				if (!this.change_hand_)
				{
					this.Enter();
				}
				this.change_hand_ = false;
			}
			if (transform != null)
			{
				base.transform.position = transform.position;
				base.transform.rotation = transform.rotation;
				this.command_draw_mgr_.imagePositionOffset = this.use_adjustment_transform_.anchoredPosition;
				this.command_draw_mgr_.rotationOffset = this.use_adjustment_transform_.localEulerAngles;
			}
		}
		if (!this.is_attach_hand_mode_)
		{
			bool flag = false;
			if (GameMain.Instance.OvrMgr != null)
			{
				flag = GameMain.Instance.OvrMgr.OvrCamera.IsUIShow;
			}
			this.command_draw_mgr_.isVisible = !flag;
			if (this.command_draw_mgr_.isVisible)
			{
				if (Input.GetKeyDown(KeyCode.DownArrow))
				{
					this.Next();
				}
				if (Input.GetKeyDown(KeyCode.UpArrow))
				{
					this.Prev();
				}
				if (Input.GetKeyDown(KeyCode.Return))
				{
					this.Enter();
				}
			}
		}
	}

	private int GetPanelPosY(int select_command_no)
	{
		return this.command_height_ * select_command_no;
	}

	private GameMain.VRDeviceType devideType
	{
		get
		{
			return GameMain.Instance.VRDeviceTypeID;
		}
	}

	private bool m_bBackHandL;

	private bool m_bBackHandR;

	[SerializeField]
	private RectTransform command_panel_;

	[SerializeField]
	private RectTransform clone_src_;

	[SerializeField]
	private YotogiCommandMenuOVRWithChubLip cbl_class_;

	[SerializeField]
	private VRCommandMenuDrawManager command_draw_mgr_;

	[SerializeField]
	private bool is_commandloop_;

	[SerializeField]
	private RectTransform adjustment_rift_pos_;

	[SerializeField]
	private RectTransform adjustment_htc_pos_;

	private int command_height_;

	private bool is_attach_hand_mode_;

	private Dictionary<Skill.Data.Command.Data, RectTransform> command_dic_ = new Dictionary<Skill.Data.Command.Data, RectTransform>();

	private Skill.Data.Command.Data select_command_;

	private Action<Skill.Data.Command.Data> call_event_;

	private CanvasGroup draw_gruoup_;

	private bool controller_attach_side_L_;

	private AVRControllerButtons controller_L_;

	private AVRControllerButtons controller_R_;

	private Transform controller_trans_L_;

	private Transform controller_trans_R_;

	private RectTransform use_adjustment_transform_;

	private bool change_hand_;

	private bool stick_down_;
}
