unit OpenProjectManager;

interface

uses
  Classes, ZhAPI, UtilMethods, Windows, ProjectData, sdDB;

type
  TOpenProjectManager = class
  private
    FProjectList: TList;
    FCurProjectIndex: Integer;

    function GetProjectCount: Integer;
    procedure SetCurProjectIndex(const Value: Integer);
    function GetCurProjectData: TProjectData;

    function CheckIndexAvailable(AIndex: Integer): Boolean;
    function GetProjectData(AIndex: Integer): TProjectData;
  public
    constructor Create;
    destructor Destroy; override;

    function Open(ARec: TsdDataRecord): TProjectData;
    procedure Save(AIndex: Integer);
    procedure SaveAll;
    procedure Delete(AIndex: Integer);

    function FindProjectData(AProjectID: Integer): TProjectData;
    function ProjectIndex(AProjectID: Integer): Integer;

    property CurProjectIndex: Integer read FCurProjectIndex write SetCurProjectIndex;
    property CurProjectData: TProjectData read GetCurProjectData;
    property ProjectData[AIndex: Integer]: TProjectData read GetProjectData;
    property ProjectCount : Integer read GetProjectCount;
  end;

implementation

{ TOpenProjectManager }

function TOpenProjectManager.CheckIndexAvailable(AIndex: Integer): Boolean;
begin
  Result := (AIndex >= 0) and (AIndex < FProjectList.Count);
end;

constructor TOpenProjectManager.Create;
begin
  FProjectList := TList.Create;
end;

procedure TOpenProjectManager.Delete(AIndex: Integer);
begin
  if CheckIndexAvailable(AIndex) then
  begin
    TProjectData(FProjectList[AIndex]).Free;
    FProjectList.Delete(AIndex);
  end;
end;

destructor TOpenProjectManager.Destroy;
begin
  ClearObjects(FProjectList);
  FProjectList.Free;
  inherited;
end;

function TOpenProjectManager.FindProjectData(
  AProjectID: Integer): TProjectData;
var
  iIndex: Integer;
begin
  Result := nil;
  for iIndex := 0 to FProjectList.Count - 1 do
    if TProjectData(FProjectList.Items[iIndex]).ProjectID = AProjectID then
    begin
      Result := TProjectData(FProjectList.Items[iIndex]);
    end;
end;

function TOpenProjectManager.GetCurProjectData: TProjectData;
begin
  Result := TProjectData(FProjectList.Items[FCurProjectIndex]);
end;

function TOpenProjectManager.GetProjectCount: Integer;
begin
  Result := FProjectList.Count;
end;

function TOpenProjectManager.GetProjectData(AIndex: Integer): TProjectData;
begin
  Result := TProjectData(FProjectList.Items[AIndex]);
end;

function TOpenProjectManager.Open(ARec: TsdDataRecord): TProjectData;
begin
  Result := TProjectData.Create;
  Result.Open(ARec);
  CurProjectIndex := FProjectList.Add(Result);
end;

function TOpenProjectManager.ProjectIndex(
  AProjectID: Integer): Integer;
var
  iIndex: Integer;
begin
  Result := -1;
  for iIndex := 0 to FProjectList.Count - 1 do
    if TProjectData(FProjectList.Items[iIndex]).ProjectID = AProjectID then
    begin
      Result := iIndex;
      Break;
    end;
end;

procedure TOpenProjectManager.Save(AIndex: Integer);
begin
  if CheckIndexAvailable(AIndex) then
    TProjectData(FProjectList.Items[AIndex]).SaveAndCheck;
end;

procedure TOpenProjectManager.SaveAll;
var
  iIndex: Integer;
begin
  for iIndex := 0 to FProjectList.Count - 1 do
    TProjectData(FProjectList.Items[iIndex]).SaveAndCheck;
end;

procedure TOpenProjectManager.SetCurProjectIndex(const Value: Integer);
begin
  if CheckIndexAvailable(Value) then
    FCurProjectIndex := Value;
end;

end.
