unit MainForm;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, RTTICtrls,  Forms, Controls, Graphics,
  Dialogs, StdCtrls, ComCtrls, Spin, ExtCtrls, uHCTypeDefs, uHCDefines;

type

  { TfrmMain }

  TfrmMain = class(TForm)
    AGFlbxGroup: TListBox;
    AGFlbxUnit: TListBox;
    AGFbtnSrch: TButton;
    AGFlblUnitAlt: TLabel;
    AGFlblS: TLabel;
    AGFlblE: TLabel;
    AirGrpForm: TTabSheet;
    AGFspnbxUnitAlt: TSpinEdit;
    AGFspnbxLon: TSpinEdit;
    AGFspnbxLat: TSpinEdit;
    AGFbtnGrp1: TButton;
    AGFbtnGrp2: TButton;
    AGFlblGrp1: TLabel;
    AGFlblGrp2: TLabel;
    AGFbtnMeet: TButton;
    btnMorePath: TButton;
    MLtmemLog: TMemo;
    SETlblMLcount: TLabel;
    PageControl1: TPageControl;
    MessgLog: TTabSheet;
    Settings: TTabSheet;
    SETspnMLC: TSpinEdit;
    procedure AGFbtnGrp1Enter(Sender: TObject);
    procedure AGFbtnGrp2Enter(Sender: TObject);
    procedure AGFbtnMeetClick(Sender: TObject);
    procedure AGFbtnMeetEnter(Sender: TObject);
    procedure AGFbtnSrchEnter();
    procedure AGFlbxGroupSelectionChange(Sender: TObject; User: boolean);
    procedure AGFlbxUnitSelectionChange(Sender: TObject; User: boolean);
    procedure AGFspnbxLonEditingDone(Sender: TObject);
    procedure AGFspnbxUnitAltEditingDone(Sender: TObject);
    procedure AGFUnitUpdate();
    procedure btnMorePathClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    PStartGroup, PSelectedGroup, SGroup1, SGroup2: PHCGroup;
    PSelectedUnit: PHCUnit;
    strMySide:string;
    dblLatFact: Double;
    NewPtr: THCNewPtr;
    DisposPtr: THCDisposPtr;
    del_HarpoonEvent_list: THCdel_HarpoonEvent_list;
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.lfm}

{ TfrmMain }


procedure TfrmMain.AGFbtnSrchEnter();
var
  PHCGThis: PHCGroup;
begin
  AGFspnbxUnitAlt.Clear;
  AGFlbxGroup.Items.Clear;
  AGFlbxGroup.Sorted:=true;
  PHCGThis := PStartGroup;

  while (PHCGThis <> nil) do
    begin
      If (format('%d',[PHCGThis^.side]) = strMySide)  then
         AGFlbxGroup.Items.Add(PHCGThis^.id+ format('-%d kts',[PHCGThis^.kts]));
      PHCGThis := PHCGThis^.toSouth;
    end;
end;


procedure TfrmMain.AGFbtnGrp1Enter(Sender: TObject);
begin
  AGFlblGrp1.Caption:=PSelectedGroup^.id;
  SGroup1:=PSelectedGroup;
end;

procedure TfrmMain.AGFbtnGrp2Enter(Sender: TObject);
begin
  AGFlblGrp2.Caption:=PSelectedGroup^.id;
  SGroup2:=PSelectedGroup
end;

procedure TfrmMain.AGFbtnMeetClick(Sender: TObject);
begin
end;

procedure TfrmMain.AGFbtnMeetEnter(Sender: TObject);
begin

    SGroup1^.track^.DestLat:=  (SGroup1^.sLat div (SGroup1^.kts + SGroup2^.kts)*SGroup2^.kts + SGroup2^.sLat div (SGroup1^.kts + SGroup2^.kts)*SGroup1^.kts);
    SGroup2^.track^.DestLat:=  (SGroup1^.sLat div (SGroup1^.kts + SGroup2^.kts)*SGroup2^.kts + SGroup2^.sLat div (SGroup1^.kts + SGroup2^.kts)*SGroup1^.kts);
    SGroup1^.track^.DestLon:=  (SGroup1^.sLon div (SGroup1^.kts + SGroup2^.kts)*SGroup2^.kts + SGroup2^.sLon div (SGroup1^.kts + SGroup2^.kts)*SGroup1^.kts);
    SGroup2^.track^.DestLon:=  SGroup1^.track^.DestLon;
    SGroup1^.track^.pathEvents:=nil;
    SGroup2^.track^.pathEvents:=nil;

    showmessage('set reminder to join in course editer');
end;

procedure TfrmMain.AGFlbxGroupSelectionChange(Sender: TObject; User: boolean);
var
  strSel: String;
  PHCGThis: PHCGroup;
  PHCUThis: PHCUnit;
begin
  strSel := copy(AGFlbxGroup.Items[AGFlbxGroup.ItemIndex],1,2);
  PHCGThis := PStartGroup;
  while (PHCGThis <> nil) do
  begin
    if (strSel = copy(PHCGThis^.id,1,2)) then
       break;
    PHCGThis := PHCGThis^.toSouth;
  end;
  if (PHCGThis=nil) then //the selected group is no longer valid
     begin                 //and list must be reset
     AGFbtnSrchEnter();
     PHCGThis :=PStartGroup;
     end;                                //list reset
  PSelectedGroup:=PHCGThis;
  AGFlbxUnit.Items.Clear;
  AGFlbxUnit.Sorted := True;
  PHCUThis := PHCGThis^.Units;
  while (PHCUThis <> nil) do
  begin
    If (PHCUThis^.Air<>nil) then
    AGFlbxUnit.Items.Add(Format('%0.3d: %d-%s AltBnd %2.2d, BWR: %.0n AA, %.0n ASu, %.1n AS',
       [PHCUThis^.id, PHCAirRec(PHCUThis^.Air)^.PlatNow, PHCUThis^.name,PHCUThis^.AltBand,(PHCUThis^.BestAAWRange)/ 10,(PHCUThis^.BestASuWRange)/ 10,(PHCUThis^.BestAsWRange)/ 10]));
    PHCUThis := PHCUThis^.next;
  end;
end;

procedure TfrmMain.AGFlbxUnitSelectionChange(Sender: TObject; User: boolean);
var
  strSel: String;
  PHCUThis: PHCUnit;
begin
  strSel := AGFlbxUnit.Items[AGFlbxUnit.ItemIndex];
  PHCUThis := PSelectedGroup^.Units;
  while (PHCUThis <> nil) do
  begin
    if (Pos(Format('%0.3d', [PHCUThis^.id]), strSel) = 1) then
    begin
      break;
    end;
    PHCUThis := PHCUThis^.next;
  end;
  PSelectedUnit := PHCUThis;
  AGFspnbxUnitAlt.Value:=PSelectedUnit^.AltBand;
  dblLatFact:= Cos((90-(PSelectedUnit^.sLat/sliver1))/57.2957);
  AGFspnbxLon.Value:=(PselectedUnit^.sLon - PSelectedGroup^.sLon)/ uHCDefines.NM1*dblLatFact;
  AGFspnbxLat.Value:=(PSelectedUnit^.sLat - PSelectedGroup^.sLat)/ uHCDefines.NM1;
end;

procedure TfrmMain.AGFUnitUpdate();
begin
  AGFlbxUnit.Items.Strings[AGFlbxUnit.ItemIndex]:=Format('%0.3d: %d-%s AltBnd %2.2d BWR: %.0n AA, %.0n ASu, %.1n AS',
  [PSelectedUnit^.id, PHCAirRec(PSelectedUnit^.Air)^.PlatNow, PSelectedUnit^.name, PSelectedUnit^.AltBand,(PSelectedUnit^.BestAAWRange)/ 10,(PSelectedUnit^.BestASuWRange)/ 10,(PSelectedUnit^.BestAsWRange)/ 10]);
end;

procedure TfrmMain.btnMorePathClick(Sender: TObject);
var
  PPathLeg, PPathLegPrev, PPathLegNext: PHCPathRec;
  legNum: Integer;
begin
  // ALE 20130411 Delete the existing path leg by leg.
  //  We should also free any path events but we won't,
  //   that will be leaked memory to simplify our lives
  PPathLeg := SGroup1^.track;
  if ((PPathLeg <> nil) AND (PPathLeg^.next <> nil)) then
  begin
    PPathLegPrev := nil;
    while (PPathLeg <> nil) do
    begin
      PPathLegPrev := PPathLeg;
      PPathLeg := PPathLeg^.next;
    end;
    PPathLeg := PPathLegPrev;
  end;
  // ALE 20130411 PPathLeg should now point to the last
  //  leg of the group's path, work backwards freeing them.
  if (PPathLeg <> nil) then
  begin
    while (PPathLeg <> SGroup1^.track) do
    begin
      // ALE 20130411 we have a multi-leg path
      PPathLegPrev := PPathLeg^.prev;
      //Freemem(PPathLeg);
      del_HarpoonEvent_list(PPathLeg^.pathEvents);
      DisposPtr(PPathLeg);
      PPathLeg := PPathLegPrev;
    end;
    // ALE 20130411 free the initial point
    //Freemem(PPathLeg);
    del_HarpoonEvent_list(PPathLeg^.pathEvents);
    DisposPtr(PPathLeg);
  end;

  // ALE 20130411 Build up a new path
  //New(PPathLeg);
  PPathLeg := NewPtr(SizeOf(THCPathRec));
  PPathLeg^.DestLat := SGroup1^.sLat + 100000;
  PPathLeg^.DestLon := SGroup1^.sLon;
  PPathLeg^.pathEvents := nil;
  //New(PPathLegNext);
  PPathLegNext := NewPtr(SizeOf(THCPathRec));
  PPathLeg^.next := PPathLegNext;
  PPathLegNext^.prev := PPathLeg;
  SGroup1^.track := PPathLeg;

  PPathLeg := PPathLeg^.next;
  PPathLeg^.DestLat := SGroup1^.sLat + 100000;
  PPathLeg^.DestLon := SGroup1^.sLon + 100000;
  PPathLeg^.pathEvents := nil;
  //New(PPathLegNext);
  PPathLegNext := NewPtr(SizeOf(THCPathRec));
  PPathLeg^.next := PPathLegNext;
  PPathLegNext^.prev := PPathLeg;

  PPathLeg := PPathLeg^.next;
  PPathLeg^.DestLat := SGroup1^.sLat;
  PPathLeg^.DestLon := SGroup1^.sLon + 1000;
  PPathLeg^.pathEvents := nil;
  PPathLeg^.next := nil;
end;

procedure TfrmMain.AGFspnbxLonEditingDone(Sender: TObject);
begin
  PselectedUnit^.sLon:= Round( uHCDefines.NM1*(AGFspnbxLon.Value)/dblLatFact) + PSelectedGroup^.sLon;
  PSelectedUnit^.sLat:= uHCDefines.NM1*(AGFspnbxLat.Value) + PSelectedGroup^.sLat;
  AGFUnitUpdate();
end;

procedure TfrmMain.AGFspnbxUnitAltEditingDone(Sender: TObject);
begin
     PSelectedUnit^.AltBand:=AGFspnbxUnitAlt.Value;
     AGFUnitUpdate();
end;

end.

