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

public class SpawnRopeOnClickNoResources : MonoBehaviour
{
    public GameObject ropePrefab;
    public GameObject placerGizmo;
    public Vector3 ropeOffset = new Vector3(0f, 2f, 0f);
    public float connectRadius = 5f;
    public LayerMask placableGround;
    private bool placing = false;
    private RopeSim currentRope;
    private List<GameObject> fullRope;
    private GameObject gizmo;
    private RopeAttachPoint currentConnection;
    private RopeAttachPoint start;

    void Start()
    {
        fullRope = new List<GameObject>();
        gizmo = Instantiate(placerGizmo);
        gizmo.active = false;
    }

    // Update is called once per frame
    void Update()
    {
        // Delete a rope
        if (!placing && Input.GetMouseButtonDown(1))
        {
            RaycastHit ohit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out ohit, 1000f, ~(0), QueryTriggerInteraction.Collide))
            {
                if (ohit.collider.gameObject.transform.parent.GetComponentInChildren<RopeSim>() != null)
                {
                    Destroy(ohit.collider.gameObject.transform.parent.gameObject);
                }
            }
            return;
        }
        RopeAttachPoint connection = null;
        RaycastHit hit;
        if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000f, placableGround, QueryTriggerInteraction.Ignore))
        {
            return;
        }
        connection = null;
        foreach (Collider col in Physics.OverlapSphere(hit.point, connectRadius, ~(0), QueryTriggerInteraction.Ignore))
        {
            RopeAttachPoint point = col.gameObject.GetComponent<RopeAttachPoint>();
            if (point != null && point.port == true)
            {
                hit.point = point.transform.position - ropeOffset;
                connection = point;
                break;
            }
        }
        hit.point += ropeOffset;
        // Start placing a rope
        if (!placing && Input.GetMouseButtonDown(0))
        {
            if (!connection)
            {
                return;
            }
            start = connection;
            fullRope.Clear();
            placing = true;
            fullRope.Add(Instantiate(ropePrefab, Vector3.zero, Quaternion.identity));
            currentRope = fullRope[fullRope.Count - 1].GetComponentInChildren<RopeSim>();
            currentRope.start.position = hit.point;
            currentRope.end.position = hit.point + new Vector3(0, 1.5f, 0);
            connection.next.Add(currentRope.start.GetComponent<RopeAttachPoint>());
            currentRope.start.GetComponent<RopeAttachPoint>().prev.Add(connection);
            currentRope.Regenerate();
            //currentRope.GetComponent<Destructible>().enabled = false;
            return;
        }
        // Place Gizmo
        if (placing)
        {
            gizmo.active = true;
            gizmo.transform.position = hit.point;
        }
        else
        {
            gizmo.active = false;
        }
        // Continue placing ropes
        if (placing)
        {
            currentRope.UpdateLength();
            // give up and delete our ropes
            if (Input.GetMouseButtonDown(1))
            {
                foreach (GameObject obj in fullRope)
                {
                    Destroy(obj);
                }
                Destroy(currentRope);
                placing = false;
                fullRope.Clear();
                return;
            }
           // float dist = Vector3.Distance(currentRope.start.position, hit.point);
            //if (dist < GameManager.instance.HarvestedKids)
         //   {
                currentRope.end.position = hit.point;
          //  }
          //  else
           // {
            //    return;
           // }
            // Finish spawning rope
            if (Input.GetMouseButtonDown(0) && (connection == null || (start.gameObject != connection.gameObject)))
            {
               // GameManager.instance.HarvestedKids -= dist;
                if (connection != null && start.gameObject != connection.gameObject)
                {
                    connection.prev.Add(currentRope.end.GetComponent<RopeAttachPoint>());
                    currentRope.end.GetComponent<RopeAttachPoint>().next.Add(connection);
                    //currentRope.GetComponent<Destructible>().enabled = true;
                    placing = false;
                    fullRope.Clear();
                    return;
                }
                // Spawn a new rope because we clicked far away from a connection
                //currentRope.GetComponent<Destructible>().enabled = true;
                RopeSim lastRope = currentRope;
                fullRope.Add(Instantiate(ropePrefab, Vector3.zero, Quaternion.identity));
                currentRope = fullRope[fullRope.Count - 1].GetComponentInChildren<RopeSim>();
                currentRope.start.position = hit.point;
                currentRope.end.position = hit.point + new Vector3(0, 1.5f, 0);
                currentRope.start.GetComponent<RopeAttachPoint>().prev.Add(lastRope.end.GetComponent<RopeAttachPoint>());
                currentRope.Regenerate();
                //currentRope.GetComponent<Destructible>().enabled = false;
                lastRope.end.GetComponent<RopeAttachPoint>().next.Add(currentRope.start.GetComponent<RopeAttachPoint>());
            }
        }
    }
}
