unit FormImport;

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
  ExtCtrls, Table, LazFileUtils, FormEx;

type

  { TFormImport }

  TFormImport = class(TFormEx)
    ButtonBrowse: TButton;
    ButtonCancel: TButton;
    ButtonImport: TButton;
    CheckBoxHumanReadableHeader: TCheckBox;
    ComboBoxInputFormat: TComboBox;
    EditInputFile: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    ListView1: TListView;
    OpenDialog1: TOpenDialog;
    ScrollBox1: TScrollBox;
    TimerRedraw: TTimer;
    procedure ButtonBrowseClick(Sender: TObject);
    procedure ButtonImportClick(Sender: TObject);
    procedure ComboBoxInputFormatChange(Sender: TObject);
    procedure EditInputFileChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TimerRedrawTimer(Sender: TObject);
  private
    Table: TTable;
    RedrawPending: Boolean;
    procedure ImportTable;
    procedure UpdateTableFormat;
    procedure LoadConfig;
    procedure SaveConfig;
  end;


implementation

{$R *.lfm}

uses
  Core, Common, RegistryEx, VCardFile, VCard;

{ TFormImport }

procedure TFormImport.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  SaveConfig;
end;

procedure TFormImport.ButtonImportClick(Sender: TObject);
var
  TableFormat: TTableFormat;
begin
  TableFormat := TTableFormat(ComboBoxInputFormat.Items.Objects[ComboBoxInputFormat.ItemIndex]);
  TVCardFile(Core.Core.DataFile).VCard.ImportFromFile(EditInputFile.Text, TableFormat,
    CheckBoxHumanReadableHeader.Checked);
end;

procedure TFormImport.ComboBoxInputFormatChange(Sender: TObject);
begin
  RedrawPending := True;
end;

procedure TFormImport.EditInputFileChange(Sender: TObject);
begin
  RedrawPending := True;
  UpdateTableFormat;
end;

procedure TFormImport.ButtonBrowseClick(Sender: TObject);
begin
  OpenDialog1.DefaultExt := '';
  OpenDialog1.InitialDir := ExtractFileDir(EditInputFile.Text);
  OpenDialog1.FileName := ExtractFileName(EditInputFile.Text);
  if OpenDialog1.Execute then begin
    EditInputFile.Text := OpenDialog1.FileName;
  end;
end;

procedure TFormImport.FormCreate(Sender: TObject);
var
  TableFormat: TTableFormat;
  TableFormats: TTableFormats;
begin
  Table := TTable.Create;

  TableFormats := Table.GetInputFormats;
  ComboBoxInputFormat.Items.BeginUpdate;
  try
    for TableFormat := Low(TTableFormat) to High(TTableFormat) do
    if TableFormat in TableFormats then
      ComboBoxInputFormat.Items.AddObject(TableFormatText[TableFormat], TObject(TableFormat));
    if (ComboBoxInputFormat.ItemIndex = -1) and (ComboBoxInputFormat.Items.Count > 0) then
      ComboBoxInputFormat.ItemIndex := 0;
  finally
    ComboBoxInputFormat.Items.EndUpdate;
  end;

  LoadConfig;
  UpdateTableFormat;
end;

procedure TFormImport.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Table);
end;

procedure TFormImport.FormShow(Sender: TObject);
begin
  RedrawPending := True;
end;

procedure TFormImport.TimerRedrawTimer(Sender: TObject);
begin
  if RedrawPending then begin
    ImportTable;
    Table.GetOutputListView(ListView1);
    RedrawPending := False;
  end;
end;

procedure TFormImport.ImportTable;
var
  TableFormat: TTableFormat;
begin
  TableFormat := TTableFormat(ComboBoxInputFormat.Items.Objects[ComboBoxInputFormat.ItemIndex]);
  if not FileExists(EditInputFile.Text) then Exit;

  try
    Table.SetInput(TableFormat, LoadFileToStr(EditInputFile.Text));
    Table.Title := ExtractFileNameWithoutExt(EditInputFile.Text);
  except
    // It may fail due to invalid format
  end;
end;

procedure TFormImport.UpdateTableFormat;
var
  FileExt: string;
begin
  FileExt := ExtractFileExt(EditInputFile.Text);
  if FileExt = '.json' then
    ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfJson))
  else if FileExt = '.csv' then
    ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfCsv))
  else if FileExt = '.txt' then
    ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfMediaWiki));
end;

procedure TFormImport.LoadConfig;
var
  TableFormat: TTableFormat;
begin
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    EditInputFile.Text := ReadStringWithDefault('ImportFileName', 'Import.txt');
    TableFormat := TTableFormat(ReadIntegerWithDefault('ImportTableFormat', Integer(tfCsv)));
    CheckBoxHumanReadableHeader.Checked := ReadBoolWithDefault('ImportHumanReadableColumns', CheckBoxHumanReadableHeader.Checked);
    ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(TableFormat));
  finally
    Free;
  end;
end;

procedure TFormImport.SaveConfig;
begin
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    WriteString('ImportFileName', EditInputFile.Text);
    WriteInteger('ImportTableFormat', Integer(ComboBoxInputFormat.Items.Objects[ComboBoxInputFormat.ItemIndex]));
    WriteBool('ImportHumanReadableColumns', CheckBoxHumanReadableHeader.Checked);
  finally
    Free;
  end;
end;

end.

