using UnityEngine;

namespace SpringConstrainedPickup
{
    /// <summary>
    /// 
    /// Each item, that can be picked up and thrown to the trash bin needs this script attached.
    /// Here we keep track of the interaction with the object that can be picked up.
    /// 
    /// </summary>
    [RequireComponent(typeof(Rigidbody2D))]
    public class Pickup : Hazard
    {
        // This is the anchor for the spring (it has SpringJoint2D attached)
        private Camera mainCamera;
        private Rigidbody2D rb2D;
        public bool springBroke;
        
        private void Awake()
        {
            rb2D = GetComponent<Rigidbody2D>();
            springBroke = false;
        }

        private void Start()
        {
            mainCamera = Camera.main;
        }

        private void Update()
        {
            if (Input.GetMouseButtonUp(0) && isDragging)
            {
                isDragging = false;
                OnDragEnd();
            }
        }

        private void OnMouseEnter()
        {
            // TODO: add some fancy animation
        }

        private void OnMouseExit()
        {
            // TODO: add some fancy animation
        }

        private void OnTriggerEnter2D(Collider2D other)
        {
            Debug.Log("On Trigger Enter");
            if (other.CompareTag("Trash Bin"))
            {
                // Do not allow to destroy the extinguisher
                if (gameObject.CompareTag("Extinguisher"))
                    return;
                OnDragEnd();
                PickupManager.Instance.springAnchor.GetComponent<SpringUtil>().DisableLineRenderer();
                Destroy(gameObject);
            }
        }

        private void OnMouseDrag()
        {
            if (!springBroke)
            {
                if (!isDragging)
                {
                    if (PlayerInteraction.Instance != null)
                    {
                        PlayerInteraction.Instance.StartInteraction();
                    }
                }
                
                var pickupManager = PickupManager.Instance;
                pickupManager.springAnchor.SetActive(true);
                pickupManager.springAnchor.GetComponent<SpringJoint2D>().connectedBody = GetComponent<Rigidbody2D>();
                            
                rb2D.linearDamping = 0.8f;
            
                var springUtil = PickupManager.Instance.springAnchor.GetComponent<SpringUtil>();
                
                springUtil.SetConnectedRigidBody(GetComponent<Rigidbody2D>());
                
                springUtil.SetPickup(this);
                
                springUtil.EnableLineRenderer();
            
                Vector3 mousePos = mainCamera.ScreenToWorldPoint(Input.mousePosition);
                mousePos.z = 0f;
                PickupManager.Instance.springAnchor.transform.position = mousePos;
                isDragging = true;   
            }
        }

        /// <summary>
        /// As unity does not offer a OnDragEnd/Exit we are doing this with the update call via a flag.
        /// </summary>
        public override void OnDragEnd()
        {
            isDragging = false;
            springBroke = false; // resetting the value for the broken spring
            PickupManager.Instance.springAnchor.SetActive(false);
            if (PlayerInteraction.Instance != null)
            {
                PlayerInteraction.Instance.StopInteraction();
            }
        }
    }
}
