unit class_point;

interface

uses class_param, user_types, class_rd_image, class_UI_values;

const PICTURES_DLL_NAME = 'pictures.dll';

{$REGION 'КЛАСС "ТОЧКА"'}
type
  TRDPoint = class (TObject)
    private
      name: string;
      description: string;
      index: integer;

    public
      image: TRDPointImage;

      U_Values: TRDValue_UI;
      I_Values: TRDValue_UI;

      Params: TRDParam_List;

      function Get_point_name: string;
      function Get_point_description: string;
      function Get_point_index: integer;

      constructor Create ( in_point_name, in_point_desc: string; in_point_index: integer);
      destructor Destroy; override;
end;
{$ENDREGION}

{$REGION 'КЛАСС "Массив ТОЧЕК"'}
type
  TRDPoint_List = class (TObject)
    private
      points : array of TRDPoint;
      function Find_point_by_name( in_point_name: string): TRDPoint;
    public
      count:integer;
      function Add_point( in_point_name, in_point_desc: string): TRDPoint;
      function Delete_point( in_point: TRDPoint): boolean;

      function Get_point_by_name( in_name: string):TRDPoint;

      constructor Create ( in_point_name, in_point_desc: string; in_point_index: integer); virtual;
      destructor Destroy; override;
end;
{$ENDREGION}

implementation
{$REGION 'КЛАСС "ТОЧКА" РЕАЛИЗАЦИЯ'}
constructor TRDPoint.Create;
begin
Assert((in_point_name<>''),'Входное поле ИМЯ ТОЧКИ класса RDPoint пустое!'+'{3E287545-CFD4-47F1-A439-3D7D7B600877}');
  inherited Create;
  self.name := in_point_name;
  self.description := in_point_desc;
  self.index := in_point_index;

  self.params := TRDParam_List.Create;
  self.image := TRDPointImage.Create( PICTURES_DLL_NAME, in_point_name );
  self.U_Values := TRDValue_UI.Create;
  self.I_Values := TRDValue_UI.Create;
end;

destructor TRDPoint.Destroy;
begin
  self.name := '';
  self.description := '';
  self.index := -1;
  self.params.Free;
  self.image.Free;
  self.U_Values.Free;
  self.I_Values.Free;
  inherited Destroy;
end;

function TRDPoint.Get_Point_index:integer;
begin
Assert( ( self.index > -1 ),
        'Поле ИНДЕКС класса RDPoint вышло за пределы диапазона!'+'{04879968-E708-4E9B-A81D-6F628A9A504C}');
  result := self.index;
end;

function TRDPoint.Get_Point_name:string;
begin
  Assert( (self.name <> '' ),
        'Поле ИМЯ ТОЧКИ класса RDPoint пустое!'+'{94F8E6FD-ABCA-471C-9A0F-709CFF7C68BB}');
    result := self.name;
end;

function TRDPoint.Get_Point_description:string;
begin
    result := self.description;
end;
{$ENDREGION}

{$REGION 'КЛАСС "Массив ТОЧЕК" РЕАЛИЗАЦИЯ'}
constructor TRDPoint_List.Create;
begin
  inherited;
  SetLength( points, 0 );
  count := 0;
end;
destructor TRDPoint_List.Destroy;
var
  i: Integer;
begin
  inherited;
  if ( length( points ) > 0 ) then
  begin
    for i := 0 to length( points ) - 1 do
    begin
      points[ i ].Free;
    end;
  end;
end;

function TRDPoint_List.Find_point_by_name(in_point_name:string):TRDPoint;
var i: integer;
begin
result := nil;
if ( in_point_name <> '' ) then
  begin
    for i := 0 to length( self.points ) - 1 do
    begin
      if ( self.points[ i ].name = in_point_name ) then
      begin
        result := self.points[ i ];
      end;
    end;
  end;
end;

function TRDPoint_List.Add_Point(in_point_name, in_point_desc:string):TRDPoint;
var points_amount,
    current_point_number:integer;
begin
Assert( ( in_Point_name <> '' ),
        'Входное поле ИМЯ МОДЕЛИ класса RDPoint пустое!'+'{0AD87B83-D99D-42A4-B1B9-C4F2B088BAA2}');
  if Get_point_by_name( in_point_name ) = nil then
  begin
    //получаем номер для вставки
    points_amount := length( self.points );
    SetLength( self.points, points_amount + 1 );
    //позиция точки в массиве
    current_point_number := points_amount;
    try
      self.points[ current_point_number ]:=TRDPoint.Create( in_point_name, in_point_desc, current_point_number );
      result := self.points[ current_point_number ];
    finally
      inc(count);
    end;
  end
  else
  begin
    //Обработка ошибки уже существующей точки
    result:=nil;//showmessage( 'Точка уже существует' );
  end;
end;

function TRDPoint_List.Delete_point( in_point: TRDPoint ): boolean;
var current_index:integer;
    i: Integer;
begin
   current_index := in_point.Get_point_index;
   try
      in_point.Free;
   finally
     dec(count);
     //если удаляется не конечный элемент
     if current_index < length(self.points) - 1 then
     begin
        //сдвигаем все модели вниз на 1
        for i := current_index to length(self.points) - 1 do
        begin
          self.points[ i ] := self.points[ i+1 ];
        end;
        SetLength( self.points, length( self.points ) - 1);
        result := true;
       end
     else
     begin
        SetLength( self.points, length( self.points ) - 1);
        result := true;
     end;
   end;
end;

function TRDPoint_List.Get_point_by_name(in_name:string):TRDPoint;
begin
  result := find_point_by_name( in_name );
end;


{$ENDREGION}



end.
