unit uBiosSet;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ComCtrls, StdCtrls, IniFiles, ExtCtrls, ImgList,
  GR32_Image;

type
  TFormBiosSet = class(TForm)
    ButtonOk: TButton;
    ButtonCancel: TButton;
    BiosNames: TListView;
    BiosSets: TListView;
    LabelDescription: TLabel;
    TopImage: TImage32;
    procedure ButtonCancelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BiosNamesSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure BiosSetsSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure BiosSetsCustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure ButtonOkClick(Sender: TObject);
  private
    { Private declarations }
    SelectedBios: String;
    procedure LoadBios;
    procedure LoadBiosSets(BiosName: String);
    procedure UpdateBiosSets;
  public
    { Public declarations }
  end;

var
  FormBiosSet: TFormBiosSet;

implementation

uses uMain, uCommon;

{$R *.dfm}

procedure TFormBiosSet.LoadBios;
var
  BiosSection: THashedStringList;
  BiosSetFile: TMemIniFile;
  Loop: Integer;
  TempString: String;
begin
  if not FileExists(FormMain.FrontendPath+'ini_files\biosset.ini') then
     Exit;

  BiosSetFile:= TMemIniFile.Create(FormMain.FrontendPath+'ini_files\biosset.ini');
  BiosSection:= THashedStringList.Create;
  BiosSetFile.ReadSectionValues('Descriptions', BiosSection);
  if BiosSection.Count > 0 then
     begin
       BiosNames.Items.BeginUpdate;
       for Loop:=0 to BiosSection.Count-1 do
       begin
         case BiosSetFile.ValueExists(BiosSection.Names[Loop], 'default_bios') of
           True:
             begin
               with BiosNames.Items.Add do
               begin
                 ImageIndex:= 65;
                 Caption:= BiosSection.ValueFromIndex[Loop];
                 SubItems.Add(BiosSection.Names[Loop]);
                 SubItems.Add(BiosSetFile.ReadString(BiosSection.Names[Loop], 'default_bios', ''));
                 TempString:= BiosSetFile.ReadString(BiosSection.Names[Loop], 'selected_bios', '');
                 if TempString = '' then
                    TempString:= SubItems[1];
                 SubItems.Add(TempString);
               end;
             end;
           //False:
           //  begin
           //    GenerateMessage('Warning', 'Bios "'+BiosSection.ValueFromIndex[Loop]+'" will not be '+
           //                    'added because entry "default_bios" is missing...', 2);
           //  end;
         end;
       end;
       BiosNames.SortType:= stBoth;
       BiosNames.Items.EndUpdate;
       if BiosNames.Items.Count = 0 then
          GenerateMessage('Warning', 'File "'+FormMain.FrontendPath+'ini_files\biosset.ini'+
                          '" is outdated. Entry "default_bios" is not found for any of the '+
                          'available bios sets. Please recreate your MAME  based games list or '+
                          'replace "biosset.ini" with an updated one.', 2);
     end;
  FreeAndNil(BiosSetFile);
  FreeAndNil(BiosSection);
end;

procedure TFormBiosSet.LoadBiosSets(BiosName: String);
var
  BiosList: THashedStringList;
  BiosSetFile: TMemIniFile;
  Loop: Integer;
begin
  BiosSets.Items.BeginUpdate;
  BiosSets.Items.Clear;
  BiosSets.Items.EndUpdate;
  if not FileExists(FormMain.FrontendPath+'ini_files\biosset.ini') then
     Exit;
  BiosSetFile:= TMemIniFile.Create(FormMain.FrontendPath+'ini_files\biosset.ini');
  BiosList:= THashedStringList.Create;
  BiosSetFile.ReadSectionValues(BiosName, BiosList);
  if BiosList.Count > 0 then
     begin
       BiosSets.Items.BeginUpdate;
       for Loop:=0 to BiosList.Count-1 do
       begin
         if (BiosList.Names[Loop] <> 'default_bios') and
            (BiosList.Names[Loop] <> 'selected_bios') then
            begin
              with BiosSets.Items.Add do
              begin
                ImageIndex:= 65;
                Caption:= BiosList.ValueFromIndex[Loop];
                SubItems.Add(BiosList.Names[Loop]);
                if SubItems[0] = BiosNames.Selected.SubItems[2] then
                   begin
                     Selected:= True;
                     Focused:= True;
                   end;
              end;
            end;
       end;
       BiosSets.Items.EndUpdate;
     end;
  FreeAndNil(BiosSetFile);
  FreeAndNil(BiosList);
end;

procedure TFormBiosSet.UpdateBiosSets;
var
  BiosSetFile: TMemIniFile;
  Loop: Integer;
begin
  case FileExists(FormMain.FrontendPath+'ini_files\biosset.ini') of
    True:
      begin
        BiosSetFile:= TMemIniFile.Create(FormMain.FrontendPath+'ini_files\biosset.ini');
        for Loop:=0 to BiosNames.Items.Count-1 do
        begin
          if BiosSetFile.SectionExists(BiosNames.Items[Loop].SubItems[0]) then
             begin
               if BiosNames.Items[Loop].SubItems[2] <> BiosNames.Items[Loop].SubItems[1] then
                  BiosSetFile.WriteString(BiosNames.Items[Loop].SubItems[0], 'selected_bios', BiosNames.Items[Loop].SubItems[2])
               else
                  BiosSetFile.WriteString(BiosNames.Items[Loop].SubItems[0], 'selected_bios', '');
             end
          else
             GenerateMessage('Error', 'Section ['+BiosNames.Items[Loop].SubItems[0]+'] not found! '+
                             'Cannot update "'+BiosNames.Items[Loop].Caption+'".'+#13#10+#13#10+
                             'Please make sure the file "'+FormMain.FrontendPath+'ini_files\biosset.ini'+
                             '" is not corrupted.', 2);
        end;
        BiosSetFile.UpdateFile;
        FreeAndNil(BiosSetFile);
      end;
    False: GenerateMessage('Error', 'File "'+FormMain.FrontendPath+'ini_files\biosset.ini" not found!', 2);
 end;
 Close;
end;

procedure TFormBiosSet.ButtonCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TFormBiosSet.FormShow(Sender: TObject);
begin
  FormMain.LoadTopBar(TopImage, 'bios.png');
  LoadBios;
end;

procedure TFormBiosSet.BiosNamesSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  if Selected then
     begin
       if SelectedBios <> Item.SubItems[0] then
          begin
           SelectedBios:= Item.SubItems[0];
           LoadBiosSets(SelectedBios);
          end;
     end;
end;

procedure TFormBiosSet.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  BiosSets.Clear;
  BiosNames.Clear;
end;

procedure TFormBiosSet.BiosSetsSelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
begin
  if Selected then
     begin
       if BiosNames.Selected.SubItems[2] <> Item.SubItems[0] then
          BiosNames.Selected.SubItems[2]:= Item.SubItems[0];
     end;
end;

procedure TFormBiosSet.BiosSetsCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if Item.SubItems[0] = BiosNames.Selected.SubItems[1] then
     Sender.Canvas.Font.Style:= [fsBold];
end;

procedure TFormBiosSet.ButtonOkClick(Sender: TObject);
begin
  UpdateBiosSets;
end;

end.
