unit FormCompare;

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, DataFile,
  VCardFile, VCard, Common, RegistryEx, FormEx;

type

  { TFormCompare }

  TFormCompare = class(TFormEx)
    ButtonBrowse: TButton;
    ButtonCancel: TButton;
    ButtonCompare: TButton;
    CheckBoxNormalizePhoneNumbers: TCheckBox;
    CheckBoxRemoveExactDuplicates: TCheckBox;
    CheckBoxSortContacts: TCheckBox;
    CheckBoxWithoutPhotos: TCheckBox;
    EditAnotherFile: TEdit;
    Label1: TLabel;
    OpenDialog1: TOpenDialog;
    ScrollBox1: TScrollBox;
    procedure ButtonBrowseClick(Sender: TObject);
    procedure ButtonCompareClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    LeftVCard: TVCardFile;
    RightVCard: TVCardFile;
    procedure CompareInternal;
    procedure CompareExternal;
    procedure LoadConfig;
    procedure SaveConfig;
  end;


implementation

{$R *.lfm}

uses
  Core, FormCompareSideBySide, VCardProcessor;

resourcestring
  SCompareToolNotFound = 'Compare tool ''%s'' not found. Select valid compare tool in the application settings.';

{ TFormCompare }

procedure TFormCompare.FormCreate(Sender: TObject);
begin
  LeftVCard := TVCardFile.Create(nil);
  RightVCard := TVCardFile.Create(nil);
end;

procedure TFormCompare.FormDestroy(Sender: TObject);
begin
  FreeAndNil(LeftVCard);
  FreeAndNil(RightVCard);
end;

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

procedure TFormCompare.ButtonCompareClick(Sender: TObject);
var
  VCardProcessor: TVCardProcessor;
begin
  LeftVCard.Assign(TVCardFile(Core.Core.DataFile));
  RightVCard.LoadFromFile(EditAnotherFile.Text);

  VCardProcessor := TVCardProcessor.Create(nil);
  with VCardProcessor do
  try
    DefaultPhoneCountryCode := Core.Core.DefaultPhoneCountryCode;
    RemovePhotos := CheckBoxWithoutPhotos.Checked;
    RemovePhoneSpaces := CheckBoxNormalizePhoneNumbers.Checked;
    AddDefaultPhoneCountryPrefix := CheckBoxNormalizePhoneNumbers.Checked;
    RemoveExactDuplicates := CheckBoxRemoveExactDuplicates.Checked;
    Order := CheckBoxSortContacts.Checked;
  finally
    Free;
  end;

  LeftVCard.VCard.Contacts.Sort;
  RightVCard.VCard.Contacts.Sort;

  CompareExternal;
end;

procedure TFormCompare.ButtonBrowseClick(Sender: TObject);
var
  TempFile: TDataFile;
begin
  TempFile := Core.Core.DefaultDataFileClass.Create(nil);
  try
    OpenDialog1.Filter := TempFile.GetFileFilter;
  finally
    TempFile.Free;
  end;

  OpenDialog1.DefaultExt := '';
  OpenDialog1.InitialDir := ExtractFileDir(EditAnotherFile.Text);
  OpenDialog1.FileName := ExtractFileName(EditAnotherFile.Text);
  OpenDialog1.Options := OpenDialog1.Options - [ofAllowMultiSelect];
  if OpenDialog1.Execute then begin
    EditAnotherFile.Text := OpenDialog1.FileName;
  end;
end;

procedure TFormCompare.FormShow(Sender: TObject);
begin
  LoadConfig;
end;

procedure TFormCompare.CompareInternal;
var
  TempFileName: string;
begin
  with TFormCompareSideBySide.Create(nil) do
  try
    TempFileName := Core.Core.GetTempDir +
      DirectorySeparator + 'Compare' + VCardFileExt;
    ForceDirectories(ExtractFileDir(TempFileName));
    TVCardFile(Core.Core.DataFile).SaveToFile(TempFileName);
    LoadFileLeft(TempFileName);
    LoadFileRight(EditAnotherFile.Text);
    ShowModal;
  finally
    Free;
  end;
end;

procedure CompareText(TextLeft, TextRight: string; FileNameLeft: string = 'FileLeft.txt';
  FileNameRight: string = 'FileRight.txt');
var
  TempFileRight: string;
  TempFileLeft: string;
begin
  if not DirectoryExists(Core.Core.GetTempDir) then
    CreateDir(Core.Core.GetTempDir);
  TempFileLeft := Core.Core.GetTempDir + DirectorySeparator + FileNameLeft;
  TempFileRight := Core.Core.GetTempDir + DirectorySeparator + FileNameRight;
  SaveStringToFile(TextLeft, TempFileLeft);
  SaveStringToFile(TextRight, TempFileRight);
  if FileExists(Core.Core.CompareTool) then
    ExecuteProgram(Core.Core.CompareTool, [TempFileLeft, TempFileRight])
    else ShowMessage(Format(SCompareToolNotFound, [Core.Core.CompareTool]));
end;

procedure TFormCompare.CompareExternal;
begin
  CompareText(LeftVCard.VCard.AsString, RightVCard.VCard.AsString,
    ExtractFileName(LeftVCard.FileName), ExtractFileName(EditAnotherFile.Text));
end;

procedure TFormCompare.LoadConfig;
begin
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    EditAnotherFile.Text := ReadStringWithDefault('LastCompareFileName', '');
    CheckBoxWithoutPhotos.Checked := ReadBoolWithDefault('WithoutPhotos', True);
    CheckBoxSortContacts.Checked := ReadBoolWithDefault('SortContacts', True);
    CheckBoxNormalizePhoneNumbers.Checked := ReadBoolWithDefault('NormalizePhoneNumbers', True);
    CheckBoxRemoveExactDuplicates.Checked := ReadBoolWithDefault('RemoveExactDuplicates', True);
  finally
    Free;
  end;
end;

procedure TFormCompare.SaveConfig;
begin
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    WriteString('LastCompareFileName', EditAnotherFile.Text);
    WriteBool('WithoutPhotos', CheckBoxWithoutPhotos.Checked);
    WriteBool('SortContacts', CheckBoxSortContacts.Checked);
    WriteBool('NormalizePhoneNumbers', CheckBoxNormalizePhoneNumbers.Checked);
    WriteBool('RemoveExactDuplicates', CheckBoxRemoveExactDuplicates.Checked);
  finally
    Free;
  end;
end;

end.

