unit frmMainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, XPStyleActnCtrls, ActnList, ActnMan, Menus,
  IniFiles,
  StdCtrls, CoolTrayIcon;

type
  TfrmMain = class(TForm)
    ActionManager1: TActionManager;
    actShowAboutForm: TAction;
    actShowConfigForm: TAction;
    actTerminateApp: TAction;
    CoolTrayIcon1: TCoolTrayIcon;
    PopupMenu1: TPopupMenu;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N9: TMenuItem;
    N10: TMenuItem;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure RxTrayIcon1DblClick(Sender: TObject);
    procedure actTerminateAppExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure actShowAboutFormExecute(Sender: TObject);
    procedure actShowConfigFormExecute(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  protected
    procedure ScannerBarcodeEvent(ASender: TObject; eventType: Smallint; var pscanData: WideString);
    procedure CreateParams(var Params: TCreateParams); override;
  private
    { Private declarations }
  private
    procedure CheckConfig;
    procedure SetTrayIcon(const aIsActive : Boolean);
    function ReqDCInfo(const aCardNo : String) : String;    // 행정감면정보 조회
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}
{$R resicon.res}

uses
  FMCUtilsWrapper
  , ZebraCtrlUnit
  , CommonDataUnit
  , SuperObject
  , SuperXmlParser
  , ActiveX, ComObj, DateUtils
  , frmAboutUnit, frmConfigUnit
  , frmResultUnit;


constructor TfrmMain.Create(AOwner: TComponent);
var
  Icon : TIcon;
begin
  inherited;
  // Scanner initialization
  Open_Zebra;
  Discover_Zebra;
  SetEvent_Zebra('1');           // 스캐너가 무조건 1개 이상 연결됐을 거라고 가정한다.
  SwitchHostMode_Zebra('1', uhmIBMHID);

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

  g_CoreScanner.OnBarcodeEvent := ScannerBarcodeEvent;
end;

destructor TfrmMain.Destroy;
begin
  Close_Zebra;
  CoUninitialize;
  inherited;
end;

procedure TfrmMain.RxTrayIcon1DblClick(Sender: TObject);
begin
  actTerminateApp.Execute;
end;

procedure TfrmMain.actTerminateAppExecute(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TfrmMain.FormShow(Sender: TObject);
begin
  CoolTrayIcon1.Enabled := false;
end;

procedure TfrmMain.FormHide(Sender: TObject);
begin
  CoolTrayIcon1.Enabled := true;
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caNone;
  Hide;
end;

procedure TfrmMain.actShowAboutFormExecute(Sender: TObject);
begin
  with TfrmAbout.Create(nil) do
  try
    ShowModal();
  finally
    Free;
  end;
end;

function TfrmMain.ReqDCInfo(const aCardNo: String): String;
var
  aRequest : Variant;
begin
  try
    aRequest := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    aRequest.Open('GET', g_SvrAddr + aCardNo, false);
    aRequest.SetRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0');
    aRequest.SetRequestHeader('FCLTY_ID', g_FacilityID);
    aRequest.Send;
    Result := aRequest.ResponseText;
  finally
    aRequest := Unassigned;
  end;
end;

procedure TfrmMain.CheckConfig;
begin
  SetTrayIcon(Length(g_FacilityID) > 0);

  if Length(g_FacilityID) < 1 then
    CoolTrayIcon1.ShowBalloonHint('ESB Agent', '시설코드가 올바르게 입력되지 않았습니다', bitError, 10);

  if Length(g_SvrAddr) < 1 then
    CoolTrayIcon1.ShowBalloonHint('ESB Agent', '서비스 서버 주소가 올바르지 않습니다', bitError, 10);
end;

procedure TfrmMain.actShowConfigFormExecute(Sender: TObject);
begin
  with TfrmConfig.Create(nil) do
  try
    if ShowModal() = mrOK then
    begin
      SaveIni;
      CheckConfig;
    end;
  finally
    Free;
  end;
end;

procedure TfrmMain.SetTrayIcon(const aIsActive: Boolean);
begin
  if aIsActive then
    CoolTrayIcon1.Icon.Handle := LoadIcon(HInstance, 'ActiveIcon')
  else
    CoolTrayIcon1.Icon.Handle := LoadIcon(HInstance, 'InActiveIcon');
end;

procedure TfrmMain.ScannerBarcodeEvent(ASender: TObject;
  eventType: Smallint; var pscanData: WideString);
var
  SuperObj : ISuperObject;
  aHexStr : String;
  aEncodeStr : String;
  aAESKey : String;
  RstStr : String;
begin
  SuperObj := XMLParseString(pscanData, true);
  aHexStr := SuperObj['arg-xml.scandata.datalabel'].AsString;
  aHexStr := StringReplace(aHexStr, ' 0x', '', [rfReplaceAll]);
  aHexStr := StringReplace(aHexStr, '0x', '', [rfReplaceAll]);

  // AESEncryption 암호키로 기관번호+scn2019x 문자열을 사용한다.
  aAESKey := g_FacilityID + 'scn2019x';

  aEncodeStr := AES128EncodeString(HexStrToStr(aHexStr), aAESKey);

  with frmResult do
  begin
    Show;
    RstStr := ReqDCInfo(aEncodeStr);
    SetResult(RstStr);    
  end;
end;

procedure TfrmMain.Button1Click(Sender: TObject);
var
  RstStr : String;
begin

  with frmResult do
  begin
    Show;
    RstStr := ReqDCInfo('aa');
    SetResult(RstStr);
  end;
end;

procedure TfrmMain.Button2Click(Sender: TObject);
var
  RstStr : String;
begin
  with frmResult do
  begin
    Show;
    RstStr := ReqDCInfo('VGuPMlIvv+qjao/YXKDe5A==');
    SetResult(RstStr);
  end;
end;

procedure TfrmMain.Button3Click(Sender: TObject);
var
  aAESKey : String;
  RstStr : String;
begin
  aAESKey := g_FacilityID + 'scn2019x';

  with frmResult do
  begin
    Show;
    RstStr := ReqDCInfo(AES128EncodeString('', aAESKey));
    SetResult(RstStr);
  end;
end;

procedure TfrmMain.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 TfrmMain.CreateParams(var Params: TCreateParams);
begin
  inherited;
end;

end.
