﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AGXUnity;
public class PlotExample : MonoBehaviour
{

  AGXUnity.Constraint[] m_constraints;

  public float Frequency = 10;
  public bool Enabled = true;

  PythonPlot.Curve m_curve1;
  PythonPlot.Curve m_curve2;

  void InitPlot()
  {
    PythonPlot.PlotManager.Ipaddress = "127.0.0.1";
    
    // Get a reference to the plot client.
    var plotClient = PythonPlot.PlotManager.Instance.Plot;
    
    // Reset the plot window
    plotClient.Reset();

    // Create a new plot (window)
    var plotWindow1 = plotClient.AddPlot("Angle", "Time", "s", "Angle", "rad");

    // Create a curve in that plot window
    m_curve1 = plotWindow1.AddCurve("Hinge1", PythonPlot.Color.Green());

    // Create another plot window
    var plotWindow2 = plotClient.AddPlot("FrictionForce", "Time", "s", "FrictionForce", "N");

    // Add a curve into that plot window
    m_curve2 = plotWindow2.AddCurve("Hinge1", PythonPlot.Color.Red());
  }


  void Start()
  {
    m_constraints = GetComponentsInChildren<AGXUnity.Constraint>();
    InitPlot();
  }



  void UpdatePlot()
  {
    var plotClient = PythonPlot.PlotManager.Instance.Plot;
    plotClient.Enable = Enabled;
    plotClient.Frequency = Frequency;

    if (!Enabled)
      return;

    var angle = m_constraints[0].Native.asHinge().getAngle();
    
    // Use the curve to add data
    m_curve1.AddData(Time.time, angle);

    var frictionController = m_constraints[0].GetController<FrictionController>().Native;
    var frictionForce = frictionController.getCurrentForce();

    // Use the curve to add data
    m_curve2.AddData(Time.time, frictionForce);
  }
  // Update is called once per frame
  void Update()
  {
    UpdatePlot(); 
  }
}
