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

public class InflateDeformation : BaseDeformation
{
    public override string DeformationName { get; protected set; } = "Inflate/Deflate";

    public override string Description { get; set; } = "Inflate or deflate mesh";

    private float _intensity = 1; 
    public override float Intensity
    {
        get
        {
            return _intensity;
        }

        set
        {
            if (value > maxIntensity)
            {
                _intensity = maxIntensity;
            }
            else if(value < minIntensity)
            {
                _intensity = minIntensity;
            } else
            {
                _intensity = value;
            }
        }
    }

    private Coroutine _deformationCoroutine;

    public void Update()
    {
        
        if (IsDeforming)
        {
            computeShaderManager.InflateDeformation(DeformationManager.LocalCurrentPosition, DeformationManager.ScaledAreaOfEffect, Intensity, DeformationManager.CurrentVertices, DeformationManager.DeformingMesh, 1);
        }
    }

    private IEnumerator DeformingRoutine()
    {
        while (IsDeforming)
        {
            computeShaderManager.InflateDeformation(DeformationManager.LocalCurrentPosition, DeformationManager.ScaledAreaOfEffect, Intensity, DeformationManager.CurrentVertices, DeformationManager.DeformingMesh, 1);
            yield return new WaitForSecondsRealtime(0.1f);
        }
    }

    [SerializeField]
    private ComputeShaderManager computeShaderManager;

    public override void InitializeDeformation(DeformationManager deformationManager)
    {
        DeformationManager = deformationManager;
        if (computeShaderManager.VerticesCount != deformationManager.CurrentVertexColors.Length)
        {
            computeShaderManager.SetupBuffers(deformationManager.CurrentVertexColors.Length);
        }
    }

    public override void StartDeforming()
    {
        if (IsDeforming)
        {
            return;
        }
        computeShaderManager.StartInflate(DeformationManager.MaskedVertices, DeformationManager.CurrentVertices, DeformationManager.DeformingMesh.normals);
        IsDeforming = true;
    }

    public override void StopDeforming()
    {
        IsDeforming = false;
        DeformationManager.DeformingMesh.RecalculateNormals();
        DeformationManager.DeformingMesh.RecalculateBounds();
    }
}
