unit AidChatplugin;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  ComCtrls, RichEdit2, ExRichEdit, SysUtils, Graphics, classes,
  ComObj, ActiveX, ANYQAid_TLB, StdVcl, ANYQCOM_TLB;

type
  TAidChatPlugin = class(TAutoObject, IANYQChatPlugin)
  private
    _chat: IANYQChat;
    _msgout: TExRichEdit;
    Procedure ProcCommand(cmd: String);
  protected
    function onAfterMessage(var Body: WideString): WideString; safecall;
    procedure onAfterRecvMessage(const Body, xml: WideString); safecall;
    procedure onBeforeMessage(var Body: WideString); safecall;
    procedure onClose; safecall;
    procedure onContextMenu(const ID: WideString); safecall;
    procedure onKeyPress(const Key: WideString); safecall;
    procedure onMenu(const ID: WideString); safecall;
    procedure onNewWindow(HWND: Integer); safecall;
    procedure onRecvMessage(const Body, xml: WideString); safecall;
  public
    reg_id: integer;
    constructor Create(chat_controller: IANYQChat);
  end;

implementation

uses ComServ, uAidData, forms;

constructor TAidChatPlugin.Create(chat_controller: IANYQChat);
begin
  inherited Create();
  _chat := chat_controller;
end;

function TAidChatPlugin.onAfterMessage(var Body: WideString): WideString;
begin

end;

procedure TAidChatPlugin.onAfterRecvMessage(const Body, xml: WideString);
var
  adr: integer;
begin
  if (_MsgOut = nil) then
  begin
    _MsgOut := TExRichEdit.Create(nil);
    _MsgOut.Visible := false;
//    _MsgOut.ParentWindow := Application.Handle;
    adr := _chat.getMagicInt(Ptr_MsgInput);
    _MsgOut.DefAttributes.Name := TExRichEdit(Pointer(adr)^).DefAttributes.Name;
    _MsgOut.DefAttributes.Size := TExRichEdit(Pointer(adr)^).DefAttributes.Size;
    _MsgOut.DefAttributes.Color := TExRichEdit(Pointer(adr)^).DefAttributes.Color;
    _MsgOut.DefAttributes.Style := TExRichEdit(Pointer(adr)^).DefAttributes.Style;
    _MsgOut.ParentWindow := TExRichEdit(Pointer(adr)^).Handle;
    _MsgOut.Clear;
  end;
  if length(Body)<=0 then exit; 
  if Body[1] <> '.' then exit;
  adr := _chat.getMagicInt(Ptr_MsgInput);
  _MsgOut.SelAttributes.Name := TExRichEdit(Pointer(adr)^).SelAttributes.Name;
  _MsgOut.SelAttributes.Size := TExRichEdit(Pointer(adr)^).SelAttributes.Size;
  _MsgOut.SelAttributes.Color := TExRichEdit(Pointer(adr)^).SelAttributes.Color;
  _MsgOut.SelAttributes.Style := TExRichEdit(Pointer(adr)^).SelAttributes.Style;
  ProcCommand(Body);
  _MsgOut.Clear;  
end;

procedure TAidChatPlugin.onBeforeMessage(var Body: WideString);
begin

end;

procedure TAidChatPlugin.onClose;
begin
  if assigned(_MsgOut) then
    FreeAndNil(_MsgOut);
  _chat.UnRegister(reg_id);
end;

procedure TAidChatPlugin.onContextMenu(const ID: WideString);
begin

end;

procedure TAidChatPlugin.onKeyPress(const Key: WideString);
begin

end;

procedure TAidChatPlugin.onMenu(const ID: WideString);
begin

end;

procedure TAidChatPlugin.onNewWindow(HWND: Integer);
begin
  _MsgOut := nil;
end;

procedure TAidChatPlugin.onRecvMessage(const Body, xml: WideString);
begin

end;

procedure TAidChatPlugin.ProcCommand(cmd: String);
var
  i: integer;
  Pcmd: PChar;
  Key: string;
  param: string;
  Data: string;
  Childs: TStringlist;
  Oldrtf: string;
begin
  if cmd[1] = '.' then
    Delete(cmd, 1, 1);
  Pcmd := Pchar(cmd);
  Pcmd := GetParamStr(Pcmd, Key);
  Pcmd := GetParamStr(Pcmd, param);
  if length(Pcmd)>0 then exit;
  Data := AidData.GetData(key,param);
  Childs := TstringList.Create;
  if param='' then
    AidData.GetChildRecs(key, Childs);

//  _MsgOut.SelectAll;
//  Oldrtf := _MsgOut.RTFSelText;
  _MsgOut.WideSelText := Data;
  if Childs.Count>0 then
    _MsgOut.WideSelText := #13+#10+'您可以使用下列参数和命令: '+#13+#10;

  for i := 0 to Childs.Count-1 do
  begin
    if Childs.Names[i][1]=' ' then
      _MsgOut.WideSelText := '参数:'+Childs.Names[i]+' '+StringOfChar('-',20-length(Childs.Names[i]))+'> '+Childs.ValueFromIndex[i]+#13+#10
    else
      _MsgOut.WideSelText := '命令: .'+Childs.Names[i]+' '+StringOfChar('-',18-length(Childs.Names[i]))+'> '+Childs.ValueFromIndex[i]+#13+#10;
  end;

//  _MsgOut.WideSelText := 'No help here!';
  _MsgOut.SelectAll;
  _MsgOut.Paragraph.FirstIndent := 9;
  _MsgOut.Paragraph.LeftIndent := 0;
  _chat.SendMsg(_MsgOut.WideLines.Text, pchar(_MsgOut.RTFSelText));
//  _MsgOut.RTFSelText := Oldrtf;
  Childs.Free;
end;

initialization
  TAutoObjectFactory.Create(ComServer, TAidChatPlugin, Class_AidChatPlugin,
    ciMultiInstance, tmApartment);
end.
