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

[RequireComponent(typeof(MouseInputBehaviour))]
public class MouseInputRenderBehaviour : MonoBehaviour
{
    // The maximum number of lines
    public int maxLines;
    
    // The width of each lines
    public float lineWidth;

    // The camera that the mouse is using
    private new Camera camera;

    // The mouse controller
    private MouseController mouseController;

    // The rigidbody
    private new Rigidbody2D rigidbody2D;

    public LineRenderer lineRenderer;

    private void Start()
    {
        // Get the current camera
        camera = Camera.main;

        // Get the mouse controller
        mouseController = gameObject.GetComponent<MouseController>();

        // Get the rigidbody
        rigidbody2D = gameObject.GetComponent<Rigidbody2D>();

        // Get the mouse input behaviour
        MouseInputBehaviour mouseInputBehaviour = gameObject.GetComponent<MouseInputBehaviour>();

        // Register the mouse dragging rendering events
        mouseInputBehaviour.onDragHandler += OnDrag;

        // Register the mouse drag finish event
        mouseInputBehaviour.onDragFinishHandler += OnDragFinish;

        // Create the line renderers
        lineRenderer.startWidth = lineWidth;
        lineRenderer.endWidth = lineWidth;

        lineRenderer.enabled = false;
        lineRenderer.sortingOrder = 10000;
    }

    private void OnDestroy()
    {
        // Get the mouse input behaviour
        MouseInputBehaviour mouseInputBehaviour = gameObject.GetComponent<MouseInputBehaviour>();
        mouseInputBehaviour.onDragHandler -= OnDrag;
        mouseInputBehaviour.onDragFinishHandler -= OnDragFinish;
    }

    private void OnDrag(Vector2 delta)
    {
        if (mouseController.IsHanging)
        {
            // Draw the line from the mouse to the cursor
            lineRenderer.enabled = true;
            lineRenderer.positionCount = maxLines;
            lineRenderer.SetPosition(0, new Vector3(gameObject.transform.position.x + delta.x, gameObject.transform.position.y + delta.y, gameObject.transform.position.z));
            lineRenderer.SetPosition(1, new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z));

            // Get the initial velocity
            float initialVelocityX = (-delta.x * mouseController.slingSpeed) / rigidbody2D.mass;
            float initialVelocityY = (-delta.y * mouseController.slingSpeed) / rigidbody2D.mass;

            //Debug.Log("Initial velocity: " + initialVelocityX + ", " + initialVelocityY);

            // Draw the resulting projectile
            for (int i = 2; i < maxLines; ++i)
            {
                float t1 = (float)i/20;
                float x1 = gameObject.transform.position.x + initialVelocityX * t1;
                float y1 = gameObject.transform.position.y + initialVelocityY * t1 + 0.5f * Physics.gravity.y * mouseController.GravityScale * t1 * t1;

                lineRenderer.enabled = true;
                lineRenderer.SetPosition(i, new Vector3(x1, y1, gameObject.transform.position.z));
            }
        }
    }

    private void OnDragFinish(Vector2 delta)
    {
        lineRenderer.enabled = false;
    }
}
