using DataDisplay.Common;
using DataDisplay.DataSource;
using DataDisplay.Graph;
using DataDisplay.Renderers;
using SkiaSharp;
using System;

namespace Navigator.Renderers
{
		public class PreMarketRenderer : IRenderer
		{
      private SKPaint paintPremarket=new SKPaint{Style=SKPaintStyle.StrokeAndFill,Color=new SKColor((uint)SKColors.Black),StrokeWidth=5f};
      private LineGraph premarketLineGraph;

		  public event EventHandler RefreshRequested;

      public void Refresh()
      {
        RefreshRequested?.Invoke(this, EventArgs.Empty);
      }

      public double XDataExtent{get;set;}
      public double XDataExtentMin{get;set;}
      public double XRange{get{return XDataExtent-XDataExtentMin;}}
      public double YDataExtent{get;set;}
      public double YDataExtentMin{get;set;}
      public double YRange{get{return YDataExtent-YDataExtentMin;}}

      public CompositeDataSource PreMarketElements
      {
        set
        {
          if(null==premarketLineGraph)premarketLineGraph=new LineGraph(paintPremarket,value);
          else premarketLineGraph.SetDataSource(value); 
        }
      }
      public bool AnyData()
      {
        if(null==premarketLineGraph)return false;
        return true;
      }

      public void SetColorRed()
      {
        if(!AnyData())return;
        paintPremarket.Color=new SKColor((uint)SKColors.Red);
      }

      public void SetColorGreen()
      {
        if(!AnyData())return;
        paintPremarket.Color=new SKColor((uint)SKColors.Green);
      }

		// ************************************************************************************************************************************
		// *********************************************************  R E N D E R E R *********************************************************
		// ************************************************************************************************************************************
		public void PaintSurface(SKSurface surface,SKImageInfo imageInfo)
      {
        SKCanvas canvas = surface.Canvas; 
        canvas.Clear();
        if(!AnyData())return;

        int width=imageInfo.Width;
        int height=imageInfo.Height;

        XDataExtent=GetXDataExtent(); 
        YDataExtent=GetYDataExtent();
        XDataExtentMin=GetXDataExtentMin(); 
        YDataExtentMin=GetYDataExtentMin();

        PointMapping pointMapping=new PointMapping(width,height,XDataExtent,XDataExtentMin,YDataExtent,YDataExtentMin,80,50);

        GridLines gridLines=new GridLines(10,10);
        gridLines.AssignXDataType=GetXDataType;
        gridLines.AssignGetYDataType=GetYDataType;
        gridLines.Render(canvas,pointMapping);
        if(null!=premarketLineGraph)premarketLineGraph.Render(canvas,pointMapping);
      }
      private double GetXDataExtent()
      {
        if(null==premarketLineGraph)return 0.00;
        return premarketLineGraph.GetXExtent(); 
      }
      private double GetYDataExtent()
      {
        if(null==premarketLineGraph)return 0.00;
        double maxDataExtent=premarketLineGraph.GetYExtent(); 
        return maxDataExtent;
      }
      private double GetXDataExtentMin()
      {
        if(null==premarketLineGraph)return 0.00;
        return premarketLineGraph.GetXExtentMin();
      }

      private double GetYDataExtentMin()
      {
        if(null==premarketLineGraph)return 0.00;
        return premarketLineGraph.GetYExtentMin();
      }

      private Type GetXDataType()
      {
        return typeof(long);
      }

      private Type GetYDataType()
      {
        return typeof(double);
      }
		}
	}
