using System.IO;
using UnityEngine;
using System;

public class Curve : IDisposable
{
    private AntaresBezierController curveController;
    public float curveLength = 0;
    public Vector3[] curvePoints;
    protected bool disposed;

    private static string _pathToGizmos = "";
    public static string pathToGizmos
    {
        get
        {
            if (_pathToGizmos == "")
                _pathToGizmos = "Antares" + Path.DirectorySeparatorChar;
            return _pathToGizmos;
        }
    }

    public Curve(GameObject curveObject)
    {
        curveController = curveObject.GetComponent<AntaresBezierController>();
        GetCurveLength(true);
        curvePoints = curveController.wayPoints;
    }

    public void Dispose()
    {
        if (disposed) return;
        disposed = true;
        curvePoints = null;
        curveController = null;
    }

    public int GetCurveControlPointsCount()
    {
        return curveController.transform.GetComponentsInChildren<AntaresCurvePoint>().Length;
    }

    public int GetCurveSmoothPointsCount()
    {
        return curveController.wayPoints.Length;
    }

    public void RefreshCurve()
    {
        curveController.RefreshCurve();
        curvePoints = curveController.wayPoints;
    }

    public void AddCurveControlPoint(Vector3 postion, bool refreshCarve)
    {
        curveController.AddCurvePointDuringRuntime(postion);
        if (refreshCarve) curveController.RefreshCurve();
        curvePoints = curveController.wayPoints;
    }

    public void RemoveCurveControlPoint(int indexOfPoint, bool refreshCarve)
    {
        int count = curveController.transform.GetComponentsInChildren<AntaresCurvePoint>().Length;
        if (indexOfPoint < 0 || indexOfPoint > count) return;
        GameObject.Destroy(curveController.transform.GetChild(indexOfPoint).gameObject);
        if (refreshCarve) curveController.RefreshCurve();
        curvePoints = curveController.wayPoints;
    }

    public void RefreshLineRenderers()
    {
        curveController.RefreshLineRenderers();
        curvePoints = curveController.wayPoints;
    }

    public GameObject GetNearestControlPoint(Vector3 position)
    {
        AntaresCurvePoint[] points = curveController.transform.GetComponentsInChildren<AntaresCurvePoint>();
        int count = points.Length;
        float minDistance = 10000;
        GameObject closestPont = null;
        for(int i = 0; i < count; i++)
        {
            float dst = Vector3.Distance(position, points[i].transform.position);
            if (dst < minDistance)
            {
                minDistance = dst;
                closestPont = points[i].gameObject;
            }
        }
        return closestPont;
    }

    public int GetNearestCurvePoint(Vector3 position)
    {
        Vector3[] p = curveController.wayPoints;
        int count = p.Length;
        float minDistance = 10000;
        int index = -1;
        for (int i = 0; i < count; i++)
        {
            float dst = Vector3.Distance(position, p[i]);
            if (dst < minDistance)
            {
                minDistance = dst;
                index = i;
            }
        }
        return index;
    }

    /// <summary>
    /// Get nearest smooth pont
    /// </summary>
    /// <param name="position">must be in range 0.0f - 1.0f </param>
    /// <returns>index in curvePonts</returns>
    public int GetNearestCurveSmoothPoint(float position)
    {
        float percent = 1/curveController.wayPoints.Length;
        return (int)(position * percent);
    }

    public void InvertCurve()
    {
        curveController.InvertCurve();
        curvePoints = curveController.wayPoints;
    }

    public float GetCurveLength(bool calculateAgain)
    {
        if (!calculateAgain) return curveLength;
        Vector3[] p = curveController.wayPoints;
        int count = p.Length - 1;
        curveLength = 0;
        for (int i = 0; i < count; i++) curveLength += Vector3.Distance(p[i], p[i+1]);
        return curveLength;
    }
}

