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

public class PointAndClick : MonoBehaviour
{
    private float startPosX;
    private float startPosY;
    //private Vector3 touchPosition;
    //private Vector3 direction;
    //private Rigidbody2D rb;
    //private float moveSpeed = 10f;
    public bool isBeingHeld = false;
    public bool isGround;
    public bool isSupport;
    public bool affectedBySupport;
    public GameObject affetctedSupportSnap;
    public GameObject placeHolder;

    public Vector2 startPos;
    public Vector2 snapOffset;

    public string objectName;

    // Start is called before the first frame update
    void Start()
    {
        //rb = GetComponent<Rigidbody2D>();
        startPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        //if (Input.touchCount > 0)
        //{
        //    Touch touch = Input.GetTouch(0);
        //    touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
        //    touchPosition.z = 0;
        //    direction = (touchPosition - transform.position);
        //}

        if (transform.parent == null && isBeingHeld == false)
        {
            placeHolder.GetComponent<BoxCollider2D>().enabled = true;
        }

        if (isBeingHeld == true)
        {

            placeHolder.GetComponent<SpriteRenderer>().enabled = false;
            GetComponent<SpriteRenderer>().enabled = true;
            GetComponent<BoxCollider2D>().enabled = true;

            Vector3 mousePos;
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
        }

        if (transform.parent != null )
        {
            GetComponent<SpriteRenderer>().enabled = true;
            GetComponent<BoxCollider2D>().enabled = true;
            placeHolder.GetComponent<BoxCollider2D>().enabled = false;
            placeHolder.GetComponent<SpriteRenderer>().enabled = false;

            if (isGround)
            {

                if (affectedBySupport == false)
                {
                    if (transform.parent.GetComponent<ObjectCheck>().correct == false)
                    {
                       // GetComponent<BoxCollider2D>().isTrigger = true;
                    }
                    else
                    {
                       // GetComponent<BoxCollider2D>().isTrigger = false;
                    }
                }
                else
                {
                    if (affetctedSupportSnap.GetComponentInChildren<ObjectCheck>().correct == false)
                    {
                       // GetComponent<BoxCollider2D>().isTrigger = true;
                    }
                    else
                    {
                       // GetComponent<BoxCollider2D>().isTrigger = false;
                    }
                }
            }
        }
    }

    

    private void OnMouseDown()
    {
            transform.SetParent(null);

            Vector3 mousePos;
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            startPosX = mousePos.x - this.transform.localPosition.x;
            startPosY = mousePos.y - this.transform.localPosition.y;

            isBeingHeld = true;
    }

    private void OnMouseUp()
    {
        isBeingHeld = false;
    }

    private void OnTriggerStay2D(Collider2D col)
    {

        if (col.transform.childCount == 0)
            {

            if (col.CompareTag("Ground"))
            {
                if (isBeingHeld == false)
                {
                    if (isGround)
                    {
                        
        
                            transform.SetParent(col.transform);
                        transform.position = new Vector2(col.transform.position.x + snapOffset.x, col.transform.position.y + snapOffset.y);
                                                    
                        
                    }
                    else
                    {
                       // transform.position = startPos;
                    }
                }
            }

            if (col.CompareTag("Support"))
            {
                if (isBeingHeld == false)
                {
                    if (isSupport)
                    {
                        transform.SetParent(col.transform);
                        transform.position = new Vector2(col.transform.position.x + snapOffset.x, col.transform.position.y + snapOffset.y);
                        transform.Find("Forward Image").gameObject.SetActive(true);
                    }
                    else
                    {
                        // transform.position = startPos;
                        if (transform.Find("Forward Image") != null)
                        {
                            transform.Find("Forward Image").gameObject.SetActive(false);
                        }
                    }
                }
                else
                {
                    if (transform.Find("Forward Image") != null)
                    {
                        transform.Find("Forward Image").gameObject.SetActive(false);
                    }
                }
            }
        }


        //if (col.GetComponent<ObjectCheck>() != null && col.transform.childCount == 0)
        //{
        //    if (isBeingHeld == false)
        //    {
        //        transform.position = col.transform.position;
        //        transform.SetParent(col.transform);
        //    }
        //}
    }
}
