unit UWIZOperationSelected;

{ Copyright (c) 2018 by Sphere 10 Software <http://www.sphere10.com/>

  Distributed under the MIT software license, see the accompanying file LICENSE
  or visit http://www.opensource.org/licenses/mit-license.php.

  This unit is a part of the PascalCoin Project, an infinitely scalable
  cryptocurrency. Find us here:
  Web: https://pascalcoin.org
  Source: https://github.com/PascalCoin/PascalCoin

  Acknowledgements:
  - Ugochukwu Mmaduekwe - main developer
  - Herman Schoenfeld - designer

  THIS LICENSE HEADER MUST NOT BE REMOVED.
}

{$mode delphi}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, UVisualGrid, UCommon.Data, UCellRenderers,
  UWizard, UWIZOperation, UCoreObjects;

type

  { TWIZOperationSelected }

  TWIZOperationSelected = class(TWizardForm<TWIZOperationsModel>)
    gpSelectedAccount: TGroupBox;
    lblTotalBalances: TLabel;
    lblTotalBalanceValue: TLabel;
    paGrid: TPanel;
  private
    FSelectedAccountsGrid: TVisualGrid;
  public
    procedure OnPresent; override;
    function Validate(out message: ansistring): boolean; override;
  end;



implementation

{$R *.lfm}

uses UAccounts, UCoreUtils, UDataSources, UCommon, UCommon.UI, Generics.Collections;

type

  { TOperationSelectedDataSource }

  TOperationSelectedDataSource = class(TAccountsDataSourceBase)
  private
    FModel: TWIZOperationsModel;
  public
    property Model: TWIZOperationsModel read FModel write FModel;
    procedure FetchAll(const AContainer: TList<TAccount>); override;
  end;

{ TWIZOperationSelected }

procedure TWIZOperationSelected.OnPresent;
var
  LData: TOperationSelectedDataSource;
  LIdx: integer;
  LAccount: TAccount;
  LTotalBalance: int64;
  LCaption: String;
begin
  FSelectedAccountsGrid := TVisualGrid.Create(Self);
  FSelectedAccountsGrid.CanSearch := False;
  FSelectedAccountsGrid.SortMode := smMultiColumn;
  FSelectedAccountsGrid.FetchDataInThread := False;
  FSelectedAccountsGrid.AutoPageSize := True;
  FSelectedAccountsGrid.SelectionType := stNone;
  FSelectedAccountsGrid.Options := [vgoColAutoFill, vgoColSizing, vgoSortDirectionAllowNone, vgoAutoHidePaging];
  LCaption := 'Selected Account';
  gpSelectedAccount.Caption := IIF(Length(Model.Account.SelectedAccounts) > 1, Format('%ss', [LCaption]), Format('%s', [LCaption]));
  with FSelectedAccountsGrid.AddColumn('Account') do
  begin
    StretchedToFill := True;
    Binding := 'AccountNumber';
    SortBinding := 'AccountNumber';
    DisplayBinding := 'Account';
    Width := 100;
    HeaderFontStyles := [fsBold];
    DataFontStyles := [fsBold];
    Filters := SORTABLE_NUMERIC_FILTER;
  end;
  with FSelectedAccountsGrid.AddColumn('Balance') do
  begin
    Binding := 'BalanceDecimal';
    SortBinding := 'Balance';
    DisplayBinding := 'Balance';
    Width := 100;
    HeaderAlignment := taRightJustify;
    DataAlignment := taRightJustify;
    Renderer := TCellRenderers.PASC;
    Filters := SORTABLE_NUMERIC_FILTER;
  end;
  LData := TOperationSelectedDataSource.Create(FSelectedAccountsGrid);
  LData.Model := Model;
  FSelectedAccountsGrid.DataSource := LData;
  paGrid.AddControlDockCenter(FSelectedAccountsGrid);

  LTotalBalance := 0;
  for LIdx := Low(Model.Account.SelectedAccounts) to High(Model.Account.SelectedAccounts) do
  begin
    LAccount := Model.Account.SelectedAccounts[LIdx];
    LTotalBalance := LTotalBalance + LAccount.balance;
  end;

  lblTotalBalanceValue.Caption :=
    Format('%s PASC', [TAccountComp.FormatMoney(LTotalBalance)]);
end;

function TWIZOperationSelected.Validate(out message: ansistring): boolean;
var
  LIdx: integer;
  LAccount: TAccount;
begin
  Result := True;

  if Model.ExecuteOperationType = omtEnlistAccountForSale then
    for LIdx := Low(model.Account.SelectedAccounts) to High(model.Account.SelectedAccounts) do
    begin
      LAccount := model.Account.SelectedAccounts[LIdx];
      if TAccountComp.IsAccountForSale(LAccount.accountInfo) then
      begin
        Result := False;
        message := Format('Account "%s" Is Already Enlisted For Sale', [LAccount.AccountString]);
        Exit;
      end;
    end;

  // get signer accounts from selected accounts
  Model.Signer.SignerCandidates := TCoreTool.GetSignerCandidates(Length(Model.Account.SelectedAccounts), Model.Fee.SingleOperationFee, Model.Account.SelectedAccounts);

  if Length(Model.Signer.SignerCandidates) < 1 then
  begin
    Result := False;
    message := 'No Valid Signer Account Was Found.';
  end;

end;

{ TOperationSelectedDataSource }

procedure TOperationSelectedDataSource.FetchAll(const AContainer: TList<TAccount>);
var
  LIdx: integer;
begin
  for LIdx := Low(Model.Account.SelectedAccounts) to High(Model.Account.SelectedAccounts) do
    AContainer.Add(Model.Account.SelectedAccounts[LIdx]);

end;

end.
