unit u_Records;

interface
uses
  zglHeader, LexUtils, Setup, uMaterials, u_Button;
type
  TRec = record
    N : String[100];
    P : Cardinal;
  end;

  TRecTable = record
    R : array[1..10] of TRec;
  end;

  t_Recs = class
    RecTable: TRecTable;
    NRN : Byte;

    Button : array[1..3] of t_Button;

    Lev : Integer;

    constructor Create;
    procedure ClearRec;
    procedure LoadTRec(FN : string);
    procedure SaveTRec(FN : string);
    function NewRec(FN : string; Point : Integer) : boolean;

    procedure Draw;
    procedure Work;
  end;

  var
    Rec : t_Recs;

implementation
uses
  u_Menu, u_Player, u_SceneM;

constructor t_Recs.Create;
begin
  NRN := 11;
  Button[1] := t_Button.Create('Назад', 0, (WinW - 180) div 2, WinH - 120, 180, 52, 0.8);
  Button[2] := t_Button.Create('Предыдущий', 0, WinW div 2 - 280, WinH - 180, 190, 52, 0.7);
  Button[3] := t_Button.Create('Следующий', 0, WinW div 2 + 90, WinH - 180, 190, 52, 0.7);
end;

procedure t_Recs.SaveTRec;
var
  F : file of TRecTable;
begin
  FN := 'Map\'+ FN +'rr';
  assignFile(F, FN);
  rewrite(F);
  write(F, RecTable);
  closefile(F);
end;

procedure t_Recs.ClearRec;
var
  I : byte;
begin
  For I:=1 to 10 do
   begin
      RecTable.R[I].N:='Игрок';
      RecTable.R[I].P:=0;
   end;
end;

procedure t_Recs.LoadTRec;
var
  F : file of TRecTable;
begin
  FN := 'Map\'+ FN +'rr';
  if LexUtils.FileEx(FN) then
    begin
      assignFile(F, FN);
      reset(F);
      read(F, RecTable);
      closefile(F);
    end
  else
 ClearRec;

end;

function t_Recs.NewRec;
var
  I, B : byte;
begin
  LoadTRec(FN);
  result:=false;
  B:=0;
  for I:=10 downto 1 do
  if Point >= RecTable.R[I].P then
    begin
      if I<NRN then
        begin
          result:=true;
          NRN:=I;
        end;
      B:=I;
    end;

  if (B>0) and (B<11) then
    begin
      RecTable.R[B].N := iPLayer.Name;
      RecTable.R[B].P := Trunc(Point);
    end;
  SaveTRec(FN);
end;

procedure t_Recs.Draw;
var
  X1, X2 : Single;
   I : Integer;
   S : String;

begin
//  Pr2D_Rect(WinW /2 - 301, 32, 602, WinH - 64, $E0A0A0, $E5, PR2D_FILL);
  ssprite2d_Draw(mBgMenu, WinW /2 - 301, 32, 602, WinH - 64, 0);
  S := 'Рекорды';
  Text_DrawEx(mFont_1,
             WinW  div 2,
             64, 1, 0,
             AnsiToUtf8(S), 255, $441111, TEXT_HALIGN_CENTER);


  X1 := WinW /2 - 301 + 56;
  X2 := WinW /2 + 301 - 128;
  for I := 1 to 10 do
  begin
    S := RecTable.R[I].N;
    Text_DrawEx(mFont_1, X1, 90 + I * 38, 0.6, 0, AnsiToUtf8(S),  225, $110101);
    Text_DrawEx(mFont_1, X2, 90 + I * 38, 0.6, 0, AnsiToUtf8(Int2S(RecTable.R[I].P)), 225, $110101);
  end;

  S := Menu.M_Level.LevelS[Lev,1];
  Text_DrawEx(mFont_1, WinW div 2,
             WinH - 220, 1, 0, AnsiToUtf8(S),  255, $772050, TEXT_HALIGN_CENTER);

  for I := 1 to 3 do
    Button[I].Draw;
end;

procedure t_Recs.Work;
var
  I :integer;
begin
  for I := 1 to 3 do
  if Button[I].Work then
  case I of
    1 : Menu.S := 0;
    2, 3 : begin
             if (I = 2) and (Lev > 1) then
               Lev := Lev - 1
             else
             if (I = 3) and (Lev < Menu.M_Level.MofLevel) then
               Lev := Lev + 1;
             LoadTRec(Menu.M_Level.LevelS[Lev, 2]);
           end;
  end;
end;

end.
