{********************************************}
{   TeeChart JSON DataSource                 }
{ Copyright (c) 2001-2020 by Steema Software }
{        All Rights Reserved                 }
{********************************************}
unit TeeJSON;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  Windows, Messages,
  {$ENDIF}
  SysUtils, Classes,

  Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls,

  TeEngine, TeeURL, Chart, Series, TeeSourceEdit, TeCanvas;

type
  {$IFDEF D16}
  [ComponentPlatformsAttribute(pidWin32 or pidWin64)]
  {$ENDIF}
  TTeeJSONSource=class(TTeeSeriesSourceFile)
  private
    FSeriesNode  : String;
    FValueSource : String;
    FJSON        : TStrings;

    //Function CreateAndLoadJSON:TStrings;
    //Procedure FillSeries(AItems:TStrings);
    //procedure LoadSeriesNode(ANode:XmlNode);
    procedure SetSeriesNode(const Value: String);
    procedure SetValueSource(const Value: String);
    procedure SetJSON(const Value: TStrings);
  protected
    procedure SetActive(const Value: Boolean); override;
  public
    Constructor Create(AOwner:TComponent); override;
    Destructor Destroy; override;

    class Function Description:String; override;
    class Function Editor:TComponentClass; override;
    Procedure Load; override;
  published
    property Active;
    property Chart;
    property FileName;
    property LoadMode;
    property Series;
    property SeriesNode:String read FSeriesNode write SetSeriesNode;
    property ValueSource:String read FValueSource write SetValueSource;
    property JSON:TStrings read FJSON write SetJSON;
  end;

implementation

uses {$IFNDEF LINUX}
     ComObj,
     {$ENDIF}
     {$IFDEF D6}
     Variants,
     {$ELSE}
     {$IFDEF LCL}
     Variants,
     {$ENDIF}
     {$ENDIF}
     TeeProcs, TeeProCo, TeePenDlg, TeeStringsEditor
     {$IFDEF AXUNICODE}
     ,TeeUnicode
     {$ENDIF}
     ;

const
  TeeMsg_JSONFile='JSON file';

class function TTeeJSONSource.Description: String;
begin
  result:=TeeMsg_JSONFile;
end;

procedure TTeeJSONSource.SetSeriesNode(const Value: String);
begin
  if FSeriesNode<>Value then
  begin
    Close;
    FSeriesNode:=Value;
  end;
end;

procedure TTeeJSONSource.SetValueSource(const Value: String);
begin
  if FValueSource<>Value then
  begin
    Close;
    FValueSource:=Value;
  end;
end;

procedure TTeeJSONSource.Load;
//var //tmpSeries : XmlNodeList;
    //t         : Integer;
    //tmp       : String;
    //tmpTitle  : XmlNode;
    //tmpFound  : Boolean;
begin
  if not (csDesigning in ComponentState) then
  if Assigned(Series) or Assigned(Chart) then
  if (FileName<>'') or (JSON.Count>0) then
  begin
    // TODO
  end;
end;

class function TTeeJSONSource.Editor: TComponentClass;
begin
  result:=nil; //'TTeeJSONSourceEditor';
end;

constructor TTeeJSONSource.Create(AOwner: TComponent);
begin
  inherited;
  FJSON:=TStringList.Create;
end;

destructor TTeeJSONSource.Destroy;
begin
  FJSON.Free;
  inherited;
end;

procedure TTeeJSONSource.SetJSON(const Value: TStrings);
begin
  FJSON.Assign(Value);
end;

procedure TTeeJSONSource.SetActive(const Value: Boolean);
begin
  if Value<>Active then
  begin
    inherited;

    if Active and (not Assigned(Series)) and (FileName<>'') then
       if not (csLoading in ComponentState) then Load;
  end;
end;

initialization
  TeeActivateGroup;
  RegisterClass(TTeeJSONSource);
  TeeSources.Add(TTeeJSONSource);
finalization
  TeeActivateGroup;
  TeeSources.Remove(TTeeJSONSource);
  UnRegisterClass(TTeeJSONSource);
end.
