using DataDisplay.Common;
using DataDisplay.DataSource;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataDisplay.Graph
{
  public class LineGraph
  {
    private SKPoint[] points=null;
    private CompositeDataSource compositeDataSource;
    private SKPaint paint;

    public LineGraph(SKPaint paint,CompositeDataSource compositeDataSource)
    {
      this.compositeDataSource = compositeDataSource;
      this.paint=paint;
      points=GetSKPoints();
    }
    public void SetPaint(SKPaint paint)
    {
      this.paint=paint;
    }
    public void SetDataSource(CompositeDataSource compositeDataSource)
    {
      this.compositeDataSource=compositeDataSource;
      points=GetSKPoints();
    }
    public void Render(SKCanvas canvas,PointMapping pointMapping)
    {
      if(null==points)return;
      SKPoint[] mappedPoints = new SKPoint[points.Length];
      for (int index = 0; index < points.Length; index++) mappedPoints[index] = pointMapping.MapPoint(points[index]);
      canvas.DrawPoints(SKPointMode.Polygon,mappedPoints,paint);
    }
    public SKPoint[] GetSKPoints()
    {
      IEnumerable<SKPoint> pointsEnumerable=DataSourceHelper.GetPoints(compositeDataSource);
      IEnumerator<SKPoint> pointsEnumerator=pointsEnumerable.GetEnumerator();
      List<SKPoint> points=new List<SKPoint>();
      while(pointsEnumerator.MoveNext())points.Add(pointsEnumerator.Current);
      return points.ToArray();
    }
    public SKPoint[] SKPoints
    {
      get{return points;}
      private set{;}
    }
    public double GetXExtent()
    {
      if(null==points)return 0.00;
      double xExtents=points.Max(x=>x.X);
      return xExtents;
    }
    public double GetXExtentMin()
    {
      if(null==points)return 0.00;
      double xExtents=points.Min(x=>x.X);
      return xExtents;
    }
    public double GetYExtent()
    {
      if(null==points)return 0.00;
      double yExtents=points.Max(x=>x.Y);
      return yExtents;
    }
    public double GetYExtentMin()
    {
      if(null==points)return 0.00;
      double yExtents=points.Min(x=>x.Y);
      return yExtents;
    }
  }
}
