unit frmResultUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CommonDataUnit;

type
  TfrmResult = class(TForm)
    ListBox1: TListBox;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FormResize(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  private
    { Private declarations }
    fResultList : TQueryResultList;
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;

    procedure SetResult(const aData : String);
    procedure ClearData;
  end;

var
  frmResult: TfrmResult;

implementation

{$R *.dfm}

uses
  FMCUtilsWrapper
  , SuperObject
  ;

{ TfrmResult }

constructor TfrmResult.Create(AOwner: TComponent);
var
  Icon : TIcon;
begin
  inherited;
  fResultList := TQueryResultList.Create;

  Self.Color := clWhite;

  icon := TIcon.Create;
  try
    Icon.Handle := LoadIcon(hInstance, 'ActiveIcon');
    PostMessage(Self.Handle, WM_SETICON, ICON_SMALL, icon.Handle);
  finally
    Icon.free;
  end;
end;

procedure TfrmResult.SetResult(const aData: String);
var
  SuperObj : ISuperObject;
  DetailObj : ISuperObject;
  aRecResult : TRecQueryResult;
  aAESKey : String;
  i : Integer;
  pDetailItem : PRecResultDetail;
  JSONArrIter : TSuperObjectIter;
begin
  ClearData;
  SuperObj := SO(aData);
  aAESKey := g_FacilityID + 'scn2019x';

  with aRecResult do
  begin
    // Request 응답의 code 항목이 FAIL이면 조회 실패 메시지를 준비시키고
    // 그 외의 경우는 data, name 값을 AES128Decoding 하여 디스플레이 준비시킨다.
    if (SuperObj['code'] = nil) or (UpperCase(SuperObj['code'].AsString) = 'FAIL') then
    begin
      New(pDetailItem);
      with pDetailItem^ do
      begin
        Key := '조회 실패';
        if (SuperObj['msg'] <> nil) then
          Value := SuperObj['msg'].AsString
        else
          Value := '';
      end;
      fResultList.Add(pDetailItem);
      ListBox1.Items.Add('');
    end else
    begin
      code := SuperObj['code'].AsString;    
      name := AES128DecodeString(SuperObj['name'].AsString, aAESKey);
      data := AES128DecodeString(SuperObj['data'].AsString, aAESKey);

      DetailObj := SO(data);

      for i:=0 to DetailObj.AsArray.Length-1 do
      begin
        if ObjectFindFirst(DetailObj.AsArray.O[i], JSONArrIter) then
        repeat
          New(pDetailItem);
          with pDetailItem^ do
          begin
            Key := JSONArrIter.key;
            Value := JSONArrIter.val.AsString;
          end;
          fResultList.Add(pDetailItem);
          ListBox1.Items.Add('');
        until not ObjectFindNext(JSONArrIter);
        ObjectFindClose(JSONArrIter);
      end;
    end;
    Self.Caption := name;
  end;
end;

procedure TfrmResult.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #27 then
    Close;
end;

destructor TfrmResult.Destroy;
begin
  FreeAndNil(fResultList);
  inherited;
end;

procedure TfrmResult.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  KeyStr, ValueStr : String;
begin
  inherited;
  if Index < 0 then exit;
  if Index > fResultList.Count-1 then exit;

  KeyStr := PRecResultDetail(fResultList.Items[Index])^.Key;
  ValueStr := PRecResultDetail(fResultList.Items[Index])^.Value;

  with TListbox(Control).Canvas do
  begin        
    if odSelected in State then
    begin
      Brush.Color := $00238AFF;
      Pen.Color   := $00238AFF;
    end
    else begin
      Brush.Color := clWhite;
      Pen.Color := clWhite;
    end;
    FillRect(Rect);
    Rectangle(Rect);

    Font.Style  := [fsBold];
    DrawTextEx(Handle, PChar(KeyStr), Length(KeyStr), Rect, DT_VCENTER or DT_SINGLELINE or DT_LEFT, nil);
    DrawTextEx(Handle, PChar(ValueStr), Length(ValueStr), Rect, DT_VCENTER or DT_SINGLELINE or DT_RIGHT, nil);
  end;
end;

procedure TfrmResult.ClearData;
begin
  fResultList.Clear;
  ListBox1.Items.Clear;
end;

procedure TfrmResult.FormResize(Sender: TObject);
begin
  ListBox1.Left := 5;
  ListBox1.Width := Self.Width - 17;
  ListBox1.Top := 5;
  ListBox1.Height := Self.Height - 10;
end;

procedure TfrmResult.FormShow(Sender: TObject);
begin
  Self.Height := 683;
  Self.Left := Screen.Width - Self.Width;
  Self.Top := Screen.Height - Self.Height - 45;
end;

procedure TfrmResult.FormActivate(Sender: TObject);
begin
  SetWindowLong(Handle, GWL_EXSTYLE,
                GetWindowLong(Handle, GWL_EXSTYLE) or
                WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
  ShowWindow(Application.Handle, SW_HIDE);

end;

procedure TfrmResult.CreateParams(var Params: TCreateParams);
begin
  inherited;
end;

end.

