unit FormNormalize;

{$mode Delphi}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, RegistryEx,
  FormEx;

type

  { TFormNormalize }

  TFormNormalize = class(TFormEx)
    ButtonCancel: TButton;
    ButtonProcess: TButton;
    CheckBoxAddPhonePrefix: TCheckBox;
    CheckBoxRemoveExactDuplicates: TCheckBox;
    CheckBoxRemovePhotos: TCheckBox;
    CheckBoxRemovePhoneSpaces: TCheckBox;
    CheckBoxConvertInternationaCallPrefixToCountryCode: TCheckBox;
    EditPhoneCountryCode: TEdit;
    EditPhoneInternationalCallPrefix: TEdit;
    ScrollBox1: TScrollBox;
    procedure ButtonProcessClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    procedure LoadConfig;
    procedure SaveConfig;
  end;

implementation

{$R *.lfm}

uses
  Core, VCardFile, VCardProcessor;

{ TFormNormalize }

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

procedure TFormNormalize.ButtonProcessClick(Sender: TObject);
var
  VCardProcessor: TVCardProcessor;
begin
  VCardProcessor := TVCardProcessor.Create(nil);
  with VCardProcessor do
  try
    RemovePhoneSpaces := CheckBoxRemovePhoneSpaces.Checked;
    AddDefaultPhoneCountryPrefix := CheckBoxAddPhonePrefix.Checked;
    DefaultPhoneCountryCode := Core.Core.DefaultPhoneCountryCode;
    ConvertInternationalCallPrefixToCountryCode := CheckBoxConvertInternationaCallPrefixToCountryCode.Checked;
    DefaultInternationalCallPrefix := Core.Core.DefaultInternationalCallPrefix;
    RemoveExactDuplicates := CheckBoxRemoveExactDuplicates.Checked;
    RemovePhotos := CheckBoxRemovePhotos.Checked;
    Process(TVCardFile(Core.Core.DataFile).VCard);
  finally
    Free;
  end;
end;

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

procedure TFormNormalize.LoadConfig;
begin
  EditPhoneCountryCode.Text := Core.Core.DefaultPhoneCountryCode;
  EditPhoneInternationalCallPrefix.Text := Core.Core.DefaultInternationalCallPrefix;
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    CheckBoxAddPhonePrefix.Checked := ReadBoolWithDefault('AddPhonePrefix', False);
    CheckBoxRemovePhoneSpaces.Checked := ReadBoolWithDefault('RemovePhoneSpaces', False);
    CheckBoxRemoveExactDuplicates.Checked := ReadBoolWithDefault('RemoveExactDuplicates', False);
    CheckBoxRemovePhotos.Checked := ReadBoolWithDefault('RemovePhotos', False);
    CheckBoxConvertInternationaCallPrefixToCountryCode.Checked := ReadBoolWithDefault('ConvertInternationaCallPrefixToCountryCode', False);
  finally
    Free;
  end;
end;

procedure TFormNormalize.SaveConfig;
begin
  Core.Core.DefaultPhoneCountryCode := EditPhoneCountryCode.Text;
  Core.Core.DefaultInternationalCallPrefix := EditPhoneInternationalCallPrefix.Text;
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    WriteBool('AddPhonePrefix', CheckBoxAddPhonePrefix.Checked);
    WriteBool('RemovePhoneSpaces', CheckBoxRemovePhoneSpaces.Checked);
    WriteBool('RemoveExactDuplicates', CheckBoxRemoveExactDuplicates.Checked);
    WriteBool('RemovePhotos', CheckBoxRemovePhotos.Checked);
    WriteBool('ConvertInternationaCallPrefixToCountryCode', CheckBoxConvertInternationaCallPrefixToCountryCode.Checked);
  finally
    Free;
  end;
end;


end.

