using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class LinePainter : MonoBehaviour
{

    public bool Paint = true;
    [Range(0,1)]
    public float NewSegmentDistance;

    [Range(0.01f, 1)]
    public float LineWidth;

    public float MaxInk;
    public float InkUsed;
    public Color32 InkColor;

    public GameObject LinePrefab;
    public GameObject SplatPrefab;

    public float waveHeight;
    public float waveSpeed;

    public Slider Slider;

    [Header("Make Private Later")]
    public Collider2D Col;

    [Header("Current Line")]
    public int Points;

    public Vector3 LastPos;
    public List<Vector2> Positions2D = new List<Vector2>();
    public List<Vector3> Positions3D = new List<Vector3>();

    public Transform Line;
    public LineRenderer LineRenderer;
    public EdgeCollider2D LineCollider;

    public float DrawOffset;

    public Vector3 DrawPoint
    {
        get
        {
            return new Vector3(transform.position.x, transform.position.y + DrawOffset + (waveHeight * Mathf.Sin(Time.time * waveSpeed)));
        }
    }
    


    void Start()
    {
        Col = GetComponent<Collider2D>();

        Line = Instantiate(LinePrefab).transform;
        Line.gameObject.layer = gameObject.layer;
        LineRenderer = Line.gameObject.GetComponent<LineRenderer>();
        LineCollider = Line.gameObject.GetComponent<EdgeCollider2D>();
        
        LineRenderer.startColor = InkColor;
        LineRenderer.endColor = InkColor;
        LineRenderer.widthMultiplier = LineWidth;
        DrawOffset = -(Col.bounds.extents.y - (LineRenderer.widthMultiplier));
        LineCollider.offset = new Vector2(0, 0.5f * LineRenderer.widthMultiplier);

        Invoke("PaintOn", 0.5f);
        Slider.maxValue = MaxInk;
        Slider.value = MaxInk - InkUsed;
    }

    public void PaintOn()
    {
        Paint = true;
        Splat(0.5f);
    }

    private void FixedUpdate()
    {
        if (Paint && Vector3.Distance(DrawPoint,LastPos) >= NewSegmentDistance)
        {
            
            Positions3D.Add(DrawPoint);
            LineRenderer.numPositions = Positions3D.Count;
            LineRenderer.SetPositions(Positions3D.ToArray());

            Positions2D.Add(DrawPoint);
            if (Positions2D.Count > 1)
            {
                LineCollider.points = Positions2D.ToArray();
            }

            InkUsed += Vector3.Distance(DrawPoint, LastPos);

            Points++;
            LastPos = DrawPoint;
        }
        if (Random.Range(0, 100)>90)
        {
            Splat(Random.Range(0.0f,0.5f));
        }

        if (InkUsed > MaxInk)
        {
            Explode();
            
        }

        InkUsed += 2*Time.fixedDeltaTime;

        Slider.value = MaxInk - InkUsed;
    }

    public void Explode()
    {
        Splat(1.0f);
        gameObject.SetActive(false);
        Time.timeScale = 1;
    }

    public void Splat(float sizeMultiplier)
    {
        var splat = Instantiate(SplatPrefab);
        splat.transform.position = DrawPoint;
        splat.transform.localScale = splat.transform.localScale*sizeMultiplier;
        splat.transform.rotation = transform.rotation;
        splat.layer = gameObject.layer;
        splat.GetComponent<SpriteRenderer>().color = InkColor;
    }


}
