#include "WndTitlebar.h"
#include "vdricqosd.h"
#include "icqhook.h"
#include "WndStatusbar.h"
#include "WndMessage.h"


// ----------- cTitlebarWnd -------------------------------------------------------------

cTitlebarWnd::cTitlebarWnd(int iLeft, int iTop, int iWidth, int iHeight, int iBpP, cOsd *pOsd, 
                           cVdrIcqOsd *pVdrIcqOsd, SimpleClient  *pIcqClient)
{
  tArea Area    = { iLeft, iTop, iLeft + iWidth - 1, iTop + iHeight - 1, iBpP };
  m_Area1       = Area;
  m_Area2       = Area;
  m_Area1.x2    = 471;
  m_Area2.x1    = 472;
  m_pOsd        = pOsd;
  m_pVdrIcqOsd  = pVdrIcqOsd;
  m_pIcqClient  = pIcqClient;

  m_TitleColor    = clrWhite;
  m_TitleBGColor  = clrBlack;

  m_TitleText     = "";
  m_eStatus       = ICQ2000::STATUS_OFFLINE;
  m_SelectedStatusPage = 1;
  m_bTakt         = false;
  m_bInfoVisible  = false;
}

cTitlebarWnd::~cTitlebarWnd()
{
}

tArea cTitlebarWnd::GetArea1(void)
{
  return m_Area1;
}

tArea cTitlebarWnd::GetArea2(void)
{
  return m_Area2;
}

void cTitlebarWnd::Draw(void)
{
  if (m_pOsd->GetBitmap(0) == NULL)
    return; 
  
  { // start Mutex section
    cMutexLock lock(&cVdrIcqOsd::OsdMutex);

    m_pOsd->DrawRectangle(m_Area1.x1, m_Area1.y1, m_Area1.Width(), m_Area1.Height(), m_TitleBGColor);
    m_pOsd->DrawText(m_Area1.x1 + 5, m_Area1.y1, m_TitleText.c_str(), m_TitleColor, m_TitleBGColor, font);

    string StatusText;
    tColor StatusColor;
    tColor StatusBGColor;

    if ((m_eStatus != ICQ2000::STATUS_OFFLINE) && (m_pIcqClient->GetSelfContact()->isInvisible()))
    {
      StatusText    = "Invisible";
      StatusColor   = clrWhite;
      StatusBGColor = clrRed;
    }
    else
    {
      switch (m_eStatus)
      {
      case ICQ2000::STATUS_ONLINE:
        StatusText    = "Online";
        StatusColor   = clrBlack;
        StatusBGColor = clrGreen;
        break;
      case ICQ2000::STATUS_AWAY:
        StatusText = "Away";
        StatusColor   = clrWhite;
        StatusBGColor = clrBlue;
        break;
      case ICQ2000::STATUS_NA:
        StatusText = "NA";
        StatusColor   = clrWhite;
        StatusBGColor = clrBlue;
        break;
      case ICQ2000::STATUS_OCCUPIED:
        StatusText = "Occupied";
        StatusColor   = clrWhite;
        StatusBGColor = clrBlue;
        break;
      case ICQ2000::STATUS_DND:
        StatusText = "DND";
        StatusColor   = clrWhite;
        StatusBGColor = clrBlue;
        break;
      case ICQ2000::STATUS_FREEFORCHAT:
        StatusText = "Free For Chat";
        StatusColor   = clrWhite;
        StatusBGColor = clrGreen;
        break;
      case ICQ2000::STATUS_OFFLINE:
        StatusText = "Offline";
        StatusColor   = clrWhite;
        StatusBGColor = clrRed;
        break;
      default:
        StatusText = "Unknown";
        StatusColor   = clrWhite;
        StatusBGColor = clrRed;
        break;
      }
    }

    m_pOsd->DrawRectangle(m_Area2.x1, m_Area2.y1, m_Area2.Width(), m_Area2.Height(), StatusBGColor);
    m_pOsd->DrawText(m_Area2.x1, m_Area2.y1, StatusText.c_str(), StatusColor, StatusBGColor, font, 
      m_Area2.Width(), m_Area2.Height(), taCenter);

    m_pOsd->Flush();
  } // end Mutex section

  SetHelpKeys();
}

void cTitlebarWnd::SetHelpKeys(void)
{
  if (m_pVdrIcqOsd->GetSelectedWindow() == swTitlebar)
  {
    if (m_SelectedStatusPage == 1)
      m_pStatusbarWnd->SetButtons(tr("Online"), tr("Away"), tr("Invisble"), tr("Offline"));
    else
      m_pStatusbarWnd->SetButtons(tr("Extended Away"), tr("Free for Chat"), "Do not Disturb", tr("Occupied"));
  }
}

eOSState cTitlebarWnd::ProcessKey(eKeys Key)
{
  switch (Key) 
  {
  case kLeft:
    m_SelectedStatusPage = 1;
    break;

  case kRight:
    m_SelectedStatusPage = 2;
    break;

  case kRed:
    if (m_SelectedStatusPage == 1)
      m_pIcqClient->SetStatus(ICQ2000::STATUS_ONLINE);
    else
      m_pIcqClient->SetStatus(ICQ2000::STATUS_NA);
    m_pStatusbarWnd->ShowDefaultButtons();
    break;
  
  case kGreen:
    if (m_SelectedStatusPage == 1)
      m_pIcqClient->SetStatus(ICQ2000::STATUS_AWAY);
    else
      m_pIcqClient->SetStatus(ICQ2000::STATUS_FREEFORCHAT);
    m_pStatusbarWnd->ShowDefaultButtons();
    break;
  
  case kYellow:
    if (m_SelectedStatusPage == 1)
      m_pIcqClient->SetStatus(ICQ2000::STATUS_ONLINE, true);
    else
      m_pIcqClient->SetStatus(ICQ2000::STATUS_DND);
    m_pStatusbarWnd->ShowDefaultButtons();
    break;
  
  case kBlue:
    if (m_SelectedStatusPage == 1)
      m_pIcqClient->SetStatus(ICQ2000::STATUS_OFFLINE);
    else
      m_pIcqClient->SetStatus(ICQ2000::STATUS_OCCUPIED);
    m_pStatusbarWnd->ShowDefaultButtons();
    break;

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

  default:
    break;
  }

  SetHelpKeys();

  return osContinue;
}

void cTitlebarWnd::SetTitleText(string Text)
{
  m_TitleText = Text;
}

void cTitlebarWnd::SetStatus(ICQ2000::Status eStatus)
{
  m_eStatus = eStatus;
  Draw();
}

void cTitlebarWnd::SetTitleColors(tColor TitleColor, tColor TitleBGColor)
{
  m_TitleColor   = TitleColor;
  m_TitleBGColor = TitleBGColor;
}

void cTitlebarWnd::Takt(void)
{
  if (!m_pIcqClient->IsLostEventHistoryEmpty())
  { 
    { // start Mutex section
      cMutexLock lock(&cVdrIcqOsd::OsdMutex);

      m_bTakt = !m_bTakt;
      if (m_bTakt)
        m_pOsd->DrawText(m_Area1.x2 - 35, m_Area1.y1, "!", m_TitleBGColor, m_TitleColor, font, 27, 27, taCenter);
      else
        m_pOsd->DrawText(m_Area1.x2 - 35, m_Area1.y1, "!", m_TitleColor, m_TitleBGColor, font, 27, 27, taCenter);

      m_pOsd->Flush();
    } // end Mutex section

    if (!m_bInfoVisible)
    {
      if (m_pVdrIcqOsd->GetSelectedWindow() == swMessage)
        m_pVdrIcqOsd->GetMessageWnd()->SetHelpKeys();
      m_bInfoVisible = true;
    }
  }
  else
  {
    if (m_bInfoVisible)
    {
      Draw();
      if (m_pVdrIcqOsd->GetSelectedWindow() == swMessage)
        m_pVdrIcqOsd->GetMessageWnd()->SetHelpKeys();
      m_bInfoVisible = false;
    }
  }
}
