using UnityEngine;
using System.Collections;
//using Framework.UserInterface;
//using Framework.Dbugging;
using Framework.Utility;

namespace Framework.Android
{
    public class AndroidTouch 
    {
        static private Vector2 touchBeginPosition;
        static public bool IsTouchInRect(Rect rect)
        {
            if (Input.touchCount > 0)
            {
                Touch[] touches = Input.touches;
                foreach(Touch touch in touches)
                {
                    if(PositionInRect(touch.position, rect))
                        return true;
                }
            }
            else if (Input.GetMouseButton(0))
            {
                if (PositionInRect(Input.mousePosition, rect))
                    return true;
            }
            return false;
        }

        static public Touch? GetTouchInRect(Rect rect)
        {
            if (Input.touchCount > 0)
            {
                Touch[] touches = Input.touches;
                foreach (Touch touch in touches)
                {
                    if (PositionInRect(touch.position, rect))
                        return touch;
                }
            }
            else if (Input.GetMouseButton(0))
            {
                if (PositionInRect(Input.mousePosition, rect))
                    return new Touch();
            }
            return null;
        }

        static private bool PositionInRect(Vector2 position, Rect rect)
        {
            if (position.x >= rect.x &&
                position.x <= rect.x + rect.width &&
                Screen.height - position.y >= rect.y &&
                Screen.height - position.y <= rect.y + rect.height)
            {
                return true;
            }
            else
                return false;
        }

        static public Vector2 TouchVectorFromStart()
        {
            Vector2 touchDelta = new Vector2(0, 0);
            if (Input.touchCount > 0)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    touchBeginPosition = Input.GetTouch(0).position;
                }
                if (Input.GetTouch(0).position != touchBeginPosition)
                {
                    touchDelta = (Input.GetTouch(0).position - touchBeginPosition);
                }
            }
            else 
            {
                if(Input.GetMouseButtonDown(0))
                {
                    touchBeginPosition = Input.mousePosition.ToVector2();
                }
                if (Input.GetMouseButton(0) && Input.mousePosition.ToVector2() != touchBeginPosition)
                {
                    touchDelta = (Input.mousePosition.ToVector2() - touchBeginPosition);
                }
            }
            return touchDelta;
        }

        static public Vector2 TouchVectorFromStart(Touch? touch)
        {
            Vector2 touchDelta = new Vector2(0, 0);
            if (Input.touchCount > 0)
            {
                if (touch.Value.phase == TouchPhase.Began)
                {
                    touchBeginPosition = touch.Value.position;
                }
                if (touch.Value.position != touchBeginPosition)
                {
                    touchDelta = (touch.Value.position - touchBeginPosition);
                }
            }
            else
            {
                if (Input.GetMouseButtonDown(0))
                {
                    touchBeginPosition = Input.mousePosition.ToVector2();
                }
                if (Input.GetMouseButton(0) && Input.mousePosition.ToVector2() != touchBeginPosition)
                {
                    touchDelta = (Input.mousePosition.ToVector2() - touchBeginPosition);
                }
            }
            return touchDelta;
        }




    }
}
