unit u_Menu_Level;

interface
uses
  LexUtils, u_Button;

const
  c_List = 9;
type
  T_LevelButton = class(T_Button)
    LevelN : Integer;
    Text : array[1..2] of String;
    procedure Draw;
  end;

  T_M_Level = class
    NP,  Sz : TM2DPoint;
    P : TM2DPoint;
    Button  : array[1..3] of T_Button;
    LB      : array[1..c_List] of T_LevelButton;
    LevelS  : array of array[1..3] of string;
    MofLevel : Integer;

    Select,
    SListLevel,
    EListLevel : Integer;

    constructor Create;
    procedure Draw;
    function  Work : Boolean;
    procedure List(D : Integer);
    procedure Load;
    procedure Save;
    function  Add : Integer;
  end;

implementation
uses
  u_Records, uConst, zglHeader, uMaterials, u_Menu;
var
  STX     : array[1..2] of Integer;

procedure T_LevelButton.Draw;
begin
  Text_DrawEx(mFont_1, STX[1], Y + mFont_1.MaxHeight div 2, ifS, 0, AnsiToUtf8(Text[1]), 255, MBtnColor[ST]);
  Text_DrawEx(mFont_1, STX[2], Y + mFont_1.MaxHeight div 2, ifS, 0, AnsiToUtf8(Text[2]), 255, MBtnColor[ST]);
end;

const
  c_Btn : array[1..3] of string = ('Вверх', 'Вниз', 'Закрыть');
  x_Btn : array[1..3] of Integer =(- 100 * 3, -100, 100);
  c_Name = 'Выбор уровня';

constructor T_M_Level.Create;
var
  I : Integer;
begin
  Sz.X := 620;
  Sz.Y := 512;

  P.X := trunc((WinW - Sz.X) / 2);
  P.Y := trunc((WinH - Sz.Y) / 2);

 // NP.X := trunc(WinW - Text_GetWidth(mFont_1, 'Level Change')) div 2;
  NP.Y := P.Y + 32;

  STX[1] := P.X + 40;
  STX[2] := trunc(P.X + Sz.X - 225);

  for I := 1 to c_List do
    LB[I] := T_LevelButton.Create('', I, STX[1], trunc(P.Y + 86 + 32 * I), Sz.X - 100, 28, 0.8);

  for I := 1 to 3 do
    Button[I] := T_Button.Create(c_Btn[I], 0, P.X + Sz.X div 2 + x_Btn[I] + 20,
                                            P.Y + Sz.Y - 54, 170, 46, 0.8);
  if FileEx('Map\Maps.TXT') then
    Load
  else
  begin
    SetLength(LevelS, 1);
    Save;
  end;
  List(1); 
end;

procedure T_M_Level.Draw;
var
  I : Integer;
begin
 // Pr2D_Rect(P.X, P.Y,     Sz.X, Sz.Y, $DE99AA, $E5, PR2D_FILL);
  ssprite2d_Draw(mBgMenu, P.X, P.Y, Sz.X, Sz.Y, 0, $E5);
  Text_DrawEx(mFont_1, WinW div 2, NP.Y, 1, 0, AnsiToUtf8(c_Name),  225, $250000, TEXT_HALIGN_CENTER);

  Text_DrawEx(mFont_1, STX[1] + 50, NP.Y + 48, 0.8, 0, AnsiToUtf8('Названиие'), 225, $772222);
  Text_DrawEx(mFont_1, STX[2] + 50, NP.Y + 48, 0.8, 0, AnsiToUtf8('Рекорд'), 225, $772222);

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

function T_M_Level.Work : Boolean;
var
  I : Integer;
begin
  Result := False;
  Select := -1;

  for I := 1 to c_List do
  if  (LB[I].LevelN > 0)
  and (LB[I].Work) then
  begin
    Select := LB[I].LevelN;
    Result := True;
  end;

  for I := 1 to 3 do
  if Button[I].Work then
  case I of
    1 : if SListLevel > 1 then
          List(SListLevel - 1);
    2 : List(SListLevel + 1);
    3 : Menu.S := 0;
  end;

end;

procedure T_M_Level.List(D : Integer);
var
  I, L : Integer;
begin
  SListLevel := D;
  EListLevel := SListLevel + c_List - 1;
  if EListLevel > MofLevel then
    EListLevel := MofLevel;

  L := 1;
  for I := SListLevel to EListLevel do
  begin
    LB[L].Text[1] := Int2S(I) + ') ' +LevelS[I, 1];
    LB[L].Text[2] := LevelS[I, 3];
    LB[L].LevelN := I;
    Inc(L);
  end;

  for I := L to c_List do
  with LB[I] do
  begin
   Text[1] := '';
   Text[2] := '';
   LevelN  := -1;
  end;
end;

procedure T_M_Level.Load;
var
  F : TextFile;
  I : Integer;
  S, S2 : string;
begin
  AssignFile(F, 'Map\Maps.TXT');
  Reset(F);
  SetLength(LevelS, 1);
  while not EoF(F) do
  begin
    Readln(F, S);
    Readln(F, S2);
    {if FileEx(S2) then
    begin    }
      I := Length(LevelS);
      SetLength(LevelS, I + 1);
      LevelS[I, 1] := S;
      LevelS[I, 2] := S2;
      Rec.LoadTRec(LevelS[I, 2]);
      LevelS[I, 3] := Int2S(Rec.RecTable.R[1].P ) + '  (' +
                            Rec.RecTable.R[1].N + ')';
    {end; }
  end;
  CloseFile(F);
  MofLevel := Length(LevelS) - 1;
end;

procedure T_M_Level.Save;
var
  F : TextFile;
  I : Integer;
begin
  AssignFile(F, 'Map\Maps.TXT');
  Rewrite(F);
  for I := 1 to MofLevel do
  begin
    Writeln(F, LevelS[I, 1]);
    Writeln(F, LevelS[I, 2]);
  end;
  CloseFile(F);
  
end;

function T_M_Level.Add : Integer;
var
  I : Integer;
begin
  I := Length(LevelS);
  SetLength(LevelS, I + 1);
  LevelS[I, 1] := 'Уровень ' + Int2S(I);
  LevelS[I, 2] := Int2S(I) + '.map';
  MofLevel := I;
  Save;
  Result := I;
  List(1);
end;

end.
