#include <vdr/status.h>
#include <vdr/interface.h>
#include "WndStatusbar.h"
#include "vdricqosd.h"



// ----------- cStatusbarWnd --------------------------------------------------------------

cStatusbarWnd::cStatusbarWnd(int iLeft, int iTop, int iWidth, int iHeight, int iBpP, cOsd *pOsd, 
                             cVdrIcqOsd *pVdrIcqOsd)
{
  tArea Area    = { iLeft, iTop, iLeft + iWidth - 1, iTop + iHeight - 1, iBpP };
  m_Area1       = Area;
  m_Area2       = Area;
  m_Area1.x2    = Area.x1 + Area.Width() / 2 - 1;
  m_Area2.x1    = Area.x1 + Area.Width() / 2;
  m_pOsd        = pOsd;
  m_pVdrIcqOsd  = pVdrIcqOsd;
  m_bMessage    = false;
  m_bInAction   = false;
  m_pRed        = NULL;
  m_pGreen      = NULL;
  m_pYellow     = NULL;
  m_pBlue       = NULL;
  m_bDefaulMenu = false;
  m_bWaitForConfirming = false;
}

cStatusbarWnd::~cStatusbarWnd()
{
  m_bMessage = false;
  while (m_bInAction)
  {
    usleep(100);
  }
}

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

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


void cStatusbarWnd::ShowDefaultButtons(void)
{
  if (m_pVdrIcqOsd->GetSelectedWindow() == swTitlebar)
    m_pVdrIcqOsd->SetLastSelectedWindow();

  SetButtons(tr("Message"), tr("Messagelist"), tr("Contact"), tr("Change Status"));

  m_bDefaulMenu = true;
}

void cStatusbarWnd::SetButtons(const char *pRed, const char *pGreen /*=NULL*/, 
                               const char *pYellow /*=NULL*/, const char *pBlue /*=NULL*/)
{
  m_bDefaulMenu = false;
  m_pRed        = pRed;
  m_pGreen      = pGreen;
  m_pYellow     = pYellow;
  m_pBlue       = pBlue;

  if (!m_bMessage)
    DrawButtons();
}

void cStatusbarWnd::DrawButtons(void)
{
  if (m_pOsd->GetBitmap(0) == NULL)
    return; 

  cMutexLock lock(&cVdrIcqOsd::OsdMutex);
  m_pOsd->GetBitmap(5)->Reset();
  m_pOsd->GetBitmap(6)->Reset();

  if (m_pRed != NULL)
  {
    m_pOsd->DrawText(m_Area1.x1, m_Area1.y1, m_pRed, clrWhite, clrRed, font, 
      m_Area1.Width() / 2, m_Area1.Height(), taCenter);
  }
  else
  {
    m_pOsd->DrawRectangle(m_Area1.x1, m_Area1.y1, 
      m_Area1.x1 + m_Area1.Width() / 2, m_Area1.y2, clrTransparent);
  }


  if (m_pGreen != NULL)
  {
    m_pOsd->DrawText(m_Area1.x1 + m_Area1.Width() / 2, m_Area1.y1, m_pGreen, clrBlack, clrGreen, 
      font, m_Area1.Width() / 2, m_Area1.Height(), taCenter);
  }
  else
  {
    m_pOsd->DrawRectangle(m_Area1.x1 + m_Area1.Width() / 2, m_Area1.y1, 
      m_Area1.x2, m_Area1.y2, clrTransparent);
  }

  if (m_pYellow != NULL)
  {
    m_pOsd->DrawText(m_Area2.x1, m_Area2.y1, m_pYellow, clrBlack, clrYellow, 
      font, m_Area2.Width() / 2, m_Area2.Height(), taCenter);
  }
  else
  {
    m_pOsd->DrawRectangle(m_Area2.x1, m_Area2.y1, 
      m_Area2.x1 + m_Area2.Width() / 2, m_Area2.y2, clrTransparent);
  }

  if (m_pBlue != NULL)
  {
    m_pOsd->DrawText(m_Area2.x1 + m_Area2.Width() / 2, m_Area2.y1, m_pBlue, clrWhite, clrBlue, 
      font, m_Area2.Width() / 2, m_Area2.Height(), taCenter);
  }
  else
  {
    m_pOsd->DrawRectangle(m_Area2.x1 + m_Area2.Width() / 2, m_Area2.y1,
      m_Area2.x2, m_Area2.y2, clrTransparent);
  }

  m_pOsd->Flush();
}

void cStatusbarWnd::Message(eMessageType MessageType, const char *pMessage, int iSeconds /*=5*/)
{
  if (m_pOsd->GetBitmap(0) == NULL)
    return; 

  if (pMessage != NULL)
  {
    cMutexLock lock(&cVdrIcqOsd::OsdMutex);

    dsyslog(pMessage);
    switch (MessageType)
    {
    case mtStatus:
      m_pOsd->DrawText(m_Area1.x1, m_Area1.y1, pMessage, clrBlack, clrCyan, font, 
        m_Area1.Width() + m_Area2.Width(), m_Area1.Height(), taCenter);
      break;
    case mtInfo:
      m_pOsd->DrawText(m_Area1.x1, m_Area1.y1, pMessage, clrBlack, clrGreen, font, 
        m_Area1.Width() + m_Area2.Width(), m_Area1.Height(), taCenter);
      break;
    case mtWarning:
      m_pOsd->DrawText(m_Area1.x1, m_Area1.y1, pMessage, clrBlack, clrYellow, font, 
        m_Area1.Width() + m_Area2.Width(), m_Area1.Height(), taCenter);
      break;
    case mtError:
      m_pOsd->DrawText(m_Area1.x1, m_Area1.y1, pMessage, clrWhite, clrRed, font, 
        m_Area1.Width() + m_Area2.Width(), m_Area1.Height(), taCenter);
      break;
    }
    m_bInAction = true;
    m_Timeout = time(NULL) + iSeconds;
    if (!m_bMessage)
    {
      m_bMessage = true;
      Start();
    }
    
    m_pOsd->Flush();
  }
  else
  {
    m_bMessage = false;
    while (m_bInAction)
      usleep(100);
  }
}

bool cStatusbarWnd::Confirm(const char *pMessage, int iSeconds /*=10*/, bool WaitForTimeout /*=false*/)
{
  m_bWaitForConfirming = true;
  isyslog("vdricq: confirm: %s", pMessage);
  Message(mtWarning, pMessage, iSeconds + 5);
  cStatus::MsgOsdStatusMessage(pMessage);
  eKeys Key = Interface->Wait(iSeconds);
  bool result = WaitForTimeout ? Key == kNone : Key == kOk;
  Message(mtWarning, NULL);
  cStatus::MsgOsdStatusMessage(NULL);
  isyslog("vdricq: %sconfirmed", result ? "" : "not ");
  m_bWaitForConfirming = false;

  return result;
}

void cStatusbarWnd::Action(void)
{
  while ((time(NULL) < m_Timeout) && (m_bMessage))
  {
    usleep(100);
  }

  DrawButtons();

  m_bInAction = false;
  m_bMessage = false;
}
