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

public class Drag : MonoBehaviour
{
    //The distance from the obj to the camera
    private float distance;
    public int count = 0;
    public float zpos;
    public bool dragging;
    private Vector3 lastTouch;
    public float speed = 0.1f;

    private Shrinking shrink;
    private bool candrag;
    // private Drag drag;

    // Use this for initialization
    void Start()
    {
        zpos = transform.position.z;
        shrink = FindObjectOfType<Shrinking>();
        // drag = FindObjectOfType<Drag>();
    }

    // Update is called once per frame
    void Update()
    {

        //Works, but doesn't follow the player exactly
        // if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        // {
        //     // Get movement of the finger since last frame
        //     Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
        //
        //     // Move object across XY plane
        //     transform.Translate(touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
        // }
        if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Moved) && !shrink.died)
        {

            distance = Vector3.Distance(transform.position, Camera.main.transform.position);
            Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            Vector3 rayPoint = raycast.GetPoint(distance);
            Vector3 v = new Vector3(rayPoint.x, rayPoint.y, 0.0f);

            float step = speed * Time.deltaTime;

           // RaycastHit2D yes = Physics2D.Raycast(v, Vector3.forward);
           // if (yes.collider.CompareTag("Draggable"))
           // {
           //     transform.position = v;
           // }
            //transform.position = v;
            transform.position = Vector3.MoveTowards(transform.position, v, step);
            // transform.position = rayPoint;
        }

    }
}


//if(Input.GetTouch(0).phase == TouchPhase.Ended)
//{
//    //make it float a little or smth
//}

//Won't detect
//private void OnTriggerEnter2D(Collider2D collision)
//{
//    if(collision.gameObject.CompareTag("Bubble"))
//    {
//        Debug.Log("Crashed with Bubble");
//        collision.GetComponent<Drag>().enabled = true;
//        this.gameObject.SetActive(false);
//    }
//}


