using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Framework.Utility;

namespace Assets.FrameworkAssets.Classes.Android
{
    public class TouchRegion
    {
        private Rect rect;
        private Vector2 touchBeginPosition;
        private bool mouse;

        public TouchRegion(Rect rect)
        {
            this.rect = rect;
            this.mouse = false;
        }

        public void SetRect(Rect rect)
        {
            this.rect = rect;
        }

        public void SetTouchBeginPosition(Vector2 position)
        {
            this.touchBeginPosition = position;
        }

        public bool TouchInRegion()
        {
            if (Input.touchCount > 0)
            {
                Touch[] touches = Input.touches;
                foreach (Touch touch in touches)
                {
                    if (PositionInRect(touch.position, this.rect))
                    {
                        mouse = false;
                        return true;
                    }
                }
            }
            else if (Input.GetMouseButton(0) && PositionInRect(Input.mousePosition, rect))
            {mouse = true; return true;}
            return false;
        }

        public Touch GetTouchInRegion()
        {
            Touch touchInRegion = new Touch();
            if (Input.touchCount > 0)
            {
                Touch[] touches = Input.touches;
                foreach (Touch touch in touches)
                {
                    if (PositionInRect(touch.position, this.rect))
                        touchInRegion = touch;
                }
            }
            return touchInRegion;
        }

        public Vector2 TouchVectorInRegion()
        {
            Vector2 touchDelta = new Vector2(0, 0);
            if (!mouse)
            {
                if (TouchInRegion())
                {
                    Touch touch = GetTouchInRegion();
                    if (touch.phase == TouchPhase.Began)
                    {
                        this.touchBeginPosition = touch.position;
                    }
                    if (touch.position != this.touchBeginPosition)
                    {
                        touchDelta = (touch.position - touchBeginPosition);
                    }
                }
            }
            else
            {
                if (Input.GetMouseButtonDown(0))
                {
                    this.touchBeginPosition = Input.mousePosition.ToVector2();
                }
                if (Input.GetMouseButton(0) && Input.mousePosition.ToVector2() != this.touchBeginPosition)
                {
                    touchDelta = (Input.mousePosition.ToVector2() - this.touchBeginPosition);
                }
            }
            return touchDelta;
        }

        public Vector2 TouchDeltaVectorInRegion()
        {
            Vector2 touchDelta = new Vector2(0, 0);
            if (!mouse)
            {
                if (TouchInRegion())
                {
                    Touch touch = GetTouchInRegion();
                    touchDelta = touch.deltaPosition;
                }
            }
            else
            {
                if (Input.GetMouseButtonDown(0))
                {
                    this.touchBeginPosition = Input.mousePosition.ToVector2();
                }
                if (Input.GetMouseButton(0) && Input.mousePosition.ToVector2() != this.touchBeginPosition)
                {
                    touchDelta = (Input.mousePosition.ToVector2() - this.touchBeginPosition);
                }
            }
            return touchDelta;
        }

        public GameObject TouchObject()
        {
            if (!mouse)
            {
                if (TouchInRegion())
                {
                    Touch touch = GetTouchInRegion();
                    if (touch.phase == TouchPhase.Ended)
                    {
                        RaycastHit hit;
                        Ray ray = Camera.main.ScreenPointToRay(touch.position);
                        if (Physics.Raycast(ray, out hit, 100f))
                        {
                            return hit.transform.gameObject;
                        }
                    }
                }
            }
            else if (Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 100f))
                {
                    return hit.transform.gameObject;
                }
            }
            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;
        }
    }
}
