#include "WndMessage.h"
#include "vdricqosd.h"
#include "icqhook.h"
#include "WndMessagelist.h"
#include "WndStatusbar.h"
#include "vdricqsetup.h"

#define MESAGELENGHT 256



// ----------- cMessageWnd --------------------------------------------------------------

cMessageWnd::cMessageWnd(int iLeft, int iTop, int iWidth, int iHeight, int iBpP, cOsd *pOsd, 
                         cVdrIcqOsd *pVdrIcqOsd, SimpleClient  *pIcqClient, 
                         cMessagelistWnd *pMessagelistWnd, cStatusbarWnd *pStatusbarWnd)
                         : cEditField(iLeft, iTop, iWidth, iHeight, MESAGELENGHT, this, pOsd, pStatusbarWnd,
                         VdrIcqSetup.TextInputmode)
{
  tArea Area = { iLeft, iTop, iLeft + iWidth - 1, iTop + iHeight - 1, iBpP };
  m_Area = Area;
  m_pOsd = pOsd;
  m_pVdrIcqOsd      = pVdrIcqOsd;
  m_pIcqClient      = pIcqClient;
  m_pMessagelistWnd = pMessagelistWnd;
  m_pStatusbarWnd   = pStatusbarWnd;
  SetStyle(clrBlue, clrRed, clrGray50, clrWhite, (cFont*)font);
}

cMessageWnd::~cMessageWnd()
{
}

tArea cMessageWnd::GetArea(void)
{
  return m_Area;
}

bool cMessageWnd::OnTextChanged(string& sText)
{
  ICQ2000::ContactRef Contact = m_pIcqClient->GetActiveContact();
  ICQ2000::ContactRef MyContact = m_pIcqClient->GetSelfContact();
  if (Contact->getUIN() != MyContact->getUIN())
  {
    m_pIcqClient->SendMessage(Contact, sText, false);
    m_pMessagelistWnd->Draw();
    return true;
  }
  else
  {
    m_pStatusbarWnd->Message(mtError, "Select a Contact first");
    return false;
  }
}

void cMessageWnd::SetHelpKeys(void)
{
  if (!m_pIcqClient->IsLostEventHistoryEmpty())
    m_pStatusbarWnd->SetButtons(NULL, NULL, NULL, tr("lost Message"));
  else
    m_pStatusbarWnd->ShowDefaultButtons();
}

void cMessageWnd::Draw(void)
{
  if (m_pOsd->GetBitmap(0) == NULL)
    return; 

  SetFocused(m_pVdrIcqOsd->GetSelectedWindow() == swMessage);

  if ((m_pVdrIcqOsd->GetSelectedWindow() == swMessage) && (!cEditField::Editing()))
    SetHelpKeys();
}

eOSState cMessageWnd::ProcessKey(eKeys Key)
{
  eOSState state = osUnknown;
  if (cEditField::Editing() || ((Key != kRed) && (Key != kGreen) && (Key != kYellow) && (Key != kBlue)))
    state = cEditField::ProcessKey(Key);
  if (state != osUnknown)
    return state;

  switch (Key) 
  {
  case kBlue:
    if (m_pIcqClient->IsLostEventHistoryEmpty())
      m_pStatusbarWnd->Message(mtError, tr("Incomming Messages not found"));
    else
      m_pIcqClient->ReadLostMessage();
    break;

  case kBack:
    m_pStatusbarWnd->ShowDefaultButtons();
    return osContinue;
    break;

  default:
    break;
  }

  return osContinue;
}
