#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include "bot.h"

// this is a simple example for the PB guys
// please understand this is very prealpha right now :)

// These 2 variables are passed to the behavior from the bot in the
// BOTCMD_Init, which is always the first command a behavior will get.

HWND hwnd;  // handle of the main bot window. This is used when
            // SendMessage is called to pass a message to the bot.

int ID;     // This is your assigned behavior ID, you need to pass
            // this when sending the bot core messages


void SendPriv(int uid, char *str);
void SendPub(char *str);

// this is the function the bot calls for every event
extern "C" __declspec (dllexport) void CALLBACK BOT_Cmd(BOTCMD *bc) {
  BOTMSG bm;
  char str[500];
  static int enabled;

  switch (bc->Cmd) {
    case BOTCMD_Init: 
      // always include these 2 lines, you need this data for sending
      // messages back to the bot
      hwnd=bc->hwnd;
      ID=bc->BehaviorID;

      // this is how you register a bot command, set OpOnly to 0 if the
      // command is public, else 1. CmdID should be unique for
      // every command you register (unless you want 2 commands to
      // do the same thing).

      bm.Msg=BOTMSG_Register;
      strcpy(str,"!enable");
      bm.CmdID=1;
      bm.Cmd=str;
      bm.OpOnly=1;
      bm.HelpFile=NULL;
      SendMessage(hwnd,WM_BOT,ID,(int)(&bm));

      bm.Msg=BOTMSG_Register;
      strcpy(str,"!disable");
      bm.CmdID=2;
      bm.Cmd=str;
      bm.OpOnly=1;
      bm.HelpFile=NULL;
      SendMessage(hwnd,WM_BOT,ID,(int)(&bm));

      // that was also an example of how to send the bot a message
      // NEVER use PostMessage to send a message to the bot

      enabled=0;

      break;
    case BOTCMD_Command:
      // this processes !commands you have registered
      if (bc->CmdID==1) { // !enable
        enabled=1;
      } else if (bc->CmdID==2) { // !disable
        enabled=0;
      }
      break;
    case BOTCMD_Entered:
      if (enabled)
        SendPriv(bc->UserData->UID,"Welcome to Powerball!");
      break;
    case BOTCMD_Line:
      if (enabled)
        if (bc->LineType==LINETYPE_Pub)
          if (!strcmp(bc->Line,"Powerball"))
            SendPub("rules!");
      break;
  }
}


void SendPub(char *str) {
  BOTMSG bm;

  bm.Msg=BOTMSG_Line;
  bm.uid=0;
  bm.Line=str;
  bm.LineType=LINETYPE_Pub;
  SendMessage(hwnd,WM_BOT,ID,(int)(&bm));
}


void SendPriv(int uid, char *str) {
  BOTMSG bm;

  bm.Msg=BOTMSG_Line;
  bm.uid=uid;
  bm.Line=str;
  bm.LineType=LINETYPE_Private;
  SendMessage(hwnd,WM_BOT,ID,(int)(&bm));
}

