unit AddWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, engine;

type
  TAddSensorDlg = class(TForm)
    Label1: TLabel;
    plugins: TComboBox;
    Label2: TLabel;
    sensors: TComboBox;
    RadioGroup1: TRadioGroup;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Bevel1: TBevel;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label3: TLabel;
    CheckBox1: TCheckBox;
    Label4: TLabel;
    Edit2: TEdit;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    procedure RadioGroup1Click(Sender: TObject);
    procedure pluginsChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    Procedure CreateGen;
  public
    CreateInNode: TEdRawNode;
    Procedure Execute(GUID: TGUID; InNode: TEdRawNode; SelectPlugin: String = ''; SelectSensor: String = '');
  end;

var
  AddSensorDlg: TAddSensorDlg;

implementation

uses main, popupHint, objbrowsewin;

{$R *.dfm}

Procedure TAddSensorDlg.Execute(GUID: TGUID; InNode: TEdRawNode; SelectPlugin: String = ''; SelectSensor: String = '');
Var
 I: Integer;
 Sel: String;
begin
 CreateInNode := InNode;
 Plugins.Clear;
 Sensors.Clear;
 For I := 0 to oemain.gpl.Count - 1 do begin
  Plugins.Items.Add(oeMain.gpl[I].Name);
  If GUIDToString(oeMain.GPL[I].GUID) = GUIDToString(GUID) then begin
   Sel := oeMain.gpl[I].Name;
  end;
 end;
 For I := 0 to plugins.Items.count - 1 do begin
  if sel = plugins.ITems[I] then plugins.ItemIndex := I;
 end;
 pluginsChange(self);
 RadioGroup1.ItemIndex := 0;
 If Sensors.Items.Count = 1 then Sensors.ItemIndex := 0;
 If SelectPlugIn <> '' then begin
  For I := 0 to Plugins.Items.count - 1 do begin
   If Plugins.items[I] = SelectPlugin then begin
    Plugins.ItemIndex := I;
    pluginsChange(self);
    Break;
   end;
  end;
 end;
 If SelectSensor <> '' then begin
  For I := 0 to Sensors.items.count - 1 do begin
   If sensors.items[I] = SelectSensor then begin
    Sensors.ItemIndex := I;
    break;
   end;
  end;
 end;
 Show;
 If oeMain.TutorialList.WaitingForCallback <> '' then begin
  oeMain.TutorialList.Callback(self, 'DRAGGENERAL');
 end;
 BringToFront;
// ModalResult := mrNone;
// while ModalResult = mrNone do begin
//  application.ProcessMessages;
// end;
// Hide;
// Result := ModalResult = mrOK;
end;

procedure TAddSensorDlg.RadioGroup1Click(Sender: TObject);
begin
 Case radiogroup1.ItemIndex of
  0: // Interval
     Begin
      Groupbox1.Enabled := true;
      Groupbox2.Enabled := False;
     End;
  1: // Fixed
     Begin
      Groupbox1.Enabled := false;
      Groupbox2.Enabled := true;
     End;
 end;
 Edit1.Enabled := Groupbox1.Enabled;
 Checkbox1.Enabled := Groupbox1.Enabled;
 Label3.Enabled := Groupbox1.Enabled;
 Edit2.Enabled := Groupbox2.Enabled;
 Button3.Enabled := Groupbox2.Enabled;
 Button4.Enabled := Groupbox2.Enabled;
 Button5.Enabled := Groupbox2.Enabled;
 Label4.Enabled := Groupbox2.Enabled;
end;

procedure TAddSensorDlg.pluginsChange(Sender: TObject);
Var
 I, II: Integer;
begin
 Sensors.Clear;
 For I := 0 to oeMain.GPL.Count - 1 do begin
  if plugins.Text = oeMain.gpl[I].Name then begin
   For II := 0 to oeMain.GPL[I].SensorList.Count - 1 do begin
    Sensors.Items.Add(oeMain.GPL[I].SensorList[II].Name);
   end;
  end;
 end;
end;

procedure TAddSensorDlg.Button1Click(Sender: TObject);
Var
 OkToExit: boolean;
begin
 OKtoExit := true;
 if sensors.ItemIndex = -1 then begin
  sensors.SetFocus;
  HintWin.ShowHint(Sensors, adAuto, 'Error', 'You need to select the sensor you wish to poll.', 5000);
  exit;
 end;
 If radiogroup1.ItemIndex = -1 then begin
  radiogroup1.SetFocus;
  HintWin.ShowHint(Radiogroup1, adAuto, 'Error', 'You need to select how the sensor should update.', 5000);
  exit;
 end;
 If Radiogroup1.ItemIndex = 0 then begin
  If Edit1.text = '' then begin
   edit1.SetFocus;
   HintWin.ShowHint(Edit1, adAuto, 'Error', 'You need to provide an update value.', 5000);
   exit;
  end;
 end;
 If Radiogroup1.ItemIndex = 1 then begin
  If Edit2.text = '' then begin
   edit2.setfocus;
   HintWin.ShowHint(Edit2, adAuto, 'Error', 'You need to provide an update value.', 5000);
   exit;
  end;
 end;
 // Check for tut call back
 If oeMain.TutorialList.WaitingForCallback <> '' then begin
  If not oeMain.TutorialList.Callback(self, plugins.Text + '.' + sensors.text) then begin
   OkToExit := false;
  end;
 end;
 If oktoexit then begin
  CreateGen;
  hide;
 end;
end;

procedure TAddSensorDlg.FormShow(Sender: TObject);
begin
 sensors.SetFocus;
end;

procedure TAddSensorDlg.Button3Click(Sender: TObject);
begin
 edit2.Text := 'Min00';
end;

procedure TAddSensorDlg.Button4Click(Sender: TObject);
begin
 EDit2.Text := 'Hour00';
end;

procedure TAddSensorDlg.Button5Click(Sender: TObject);
begin
 Edit2.Text := 'Day00:00';
end;

procedure TAddSensorDlg.CreateGen;
Var
  PlugInName, SensorName: String;
  UpdateStr: String;
  Dedicated: Boolean;
  SensNode: TEdSensorNode;
begin
 If CreateInNode = Nil then exit;
 PlugInName := AddSensorDlg.plugins.Text;
 SensorName := AddSensorDlg.sensors.Text;
 Dedicated := False;
 UpdateStr := '1';
 Case AddSensorDlg.RadioGroup1.ItemIndex of
  0: begin
      Dedicated := AddSensorDlg.CheckBox1.Checked;
      UpdateStr := AddSensorDlg.Edit1.Text;
     end;
  1: begin
      Dedicated := False;
      UpdateStr := AddSensorDlg.Edit2.Text;
     end;
 end;
 Case CreateInNode.NodeType of
  ntVisual: Begin
             (createInNode as TEdVisualNode).AddSensorNode(PlugInName, SensorName, UpdateStr, Dedicated);
            end;
  ntParameter: begin
                SensNode := ((CreateInNode as TEdParameterNode).Add(ntSensor) as TEdSensorNode);
                SensNode.SetupSelf(PlugInName, SensorName, UpdateStr, Dedicated, '');
                oeMain.SelectNode(SensNode);
               end;
 end;
 ObjBrowser.LoadFromNode();
end;

procedure TAddSensorDlg.Button2Click(Sender: TObject);
begin
 hide;
end;

end.
