using System;
using UnityEngine;
using UnityEngine.UI;

namespace AssemblyCSharp
{
	public class TouchControl : IPlayerController
	{
		Button left;
		Button right;
		Button jump;

		public TouchControl(Button left, Button right, Button jump) {
			this.left = left;
			this.right = right;
			this.jump = jump;
		}

		public TouchControl() {
			this.left = GameObject.Find ("LeftButton").GetComponent<Button>();
			this.right = GameObject.Find ("RightButton").GetComponent<Button>();
			this.jump = GameObject.Find ("JumpButton").GetComponent<Button>();
		}

		private bool touchIsInRect(Button button, bool isRepeat) {
			var rect = button.GetComponent<RectTransform> ();
			var bounds = new Rect (rect.TransformPoint (rect.rect.position), rect.TransformVector (rect.rect.size));
			foreach (var touch in Input.touches) {
				if (bounds.Contains (touch.rawPosition) && (!isRepeat || touch.phase == TouchPhase.Began)) {
					return true;
				}
			}
			return false;
		}

		public bool getLeft() {
			return touchIsInRect (left, false);
		}
		public bool getRight() {
			//return Input.GetKey (right);
			return touchIsInRect (right, false);
		}
		public bool getJump() {
			return touchIsInRect (jump, true);
		}
	}
}

