#include "skin.h"

// --- cSkinEm84xxDisplayChannel ----------------------------------------------

class cSkinEm84xxDisplayChannel: public cSkinDisplayChannel {
private:
   cOsd *mOsd;
   int lineHeight;
   int timeWidth;
   bool mMessage;
public:
   cSkinEm84xxDisplayChannel(bool WithInfo);
   virtual ~cSkinEm84xxDisplayChannel();
   virtual void SetChannel(const cChannel *Channel, int Number);
   virtual void SetEvents(const cEvent *Present, const cEvent *Following);
   virtual void SetMessage(eMessageType Type, const char *Text);
   virtual void Flush(void);
};

cSkinEm84xxDisplayChannel::cSkinEm84xxDisplayChannel(bool WithInfo)
{
   const cFont *font = cFont::GetFont(fontOsd);
   int lh = font->Height();

   mOsd = cOsdProvider::NewOsd(Setup.OSDLeft, Setup.OSDTop + Setup.OSDHeight - (3 * lh + 4));
   mMessage = false;

   tArea areas[] = { { 0, 0, 380, 3 * lh + 8, 8}};
   mOsd->SetAreas(areas, sizeof(areas) / sizeof(tArea));
   mOsd->DrawRectangle(0, 0, mOsd->Width() - 1, mOsd->Height() - 1, clrBlue);
   mOsd->DrawRectangle(2, 2, mOsd->Width() - 3, mOsd->Height() - 3, clrGray50);
   mOsd->DrawRectangle(0, 2 + lh, mOsd->Width() - 1, 3 + lh, clrBlue);
}

cSkinEm84xxDisplayChannel::~cSkinEm84xxDisplayChannel()
{
   delete mOsd;
}

void cSkinEm84xxDisplayChannel::SetChannel(const cChannel *Channel, int Number)
{
   const cFont *font = cFont::GetFont(fontOsd);
   mOsd->DrawText(2, 2, *ChannelString(Channel, Number), clrWhite, clrGray50, font, mOsd->Width() - 4);
}

void cSkinEm84xxDisplayChannel::SetEvents(const cEvent *Present, const cEvent *Following)
{
   if (Present != NULL) {
      const cFont *fontosd = cFont::GetFont(fontOsd);
      const cFont *fontsml = cFont::GetFont(fontSml);
      int lh = fontosd->Height();
      int w = fontosd->Width("00:00 ");
      mOsd->DrawText(2, 4 + lh, *Present->GetTimeString(), clrWhite, clrGray50, fontosd);
      mOsd->DrawText(2 + w, 4 + lh, Present->Title(), clrWhite, clrGray50, fontosd, mOsd->Width() - w - 4);
      mOsd->DrawText(2 + w, 4 + lh * 2, Present->ShortText(), clrMagenta, clrGray50, fontsml, mOsd->Width() - w - 4);
   }
}

void cSkinEm84xxDisplayChannel::SetMessage(eMessageType Type, const char *Text)
{
   if (Text != NULL) {
      const cFont *font = cFont::GetFont(fontOsd);
      int lh = font->Height();
      tColor color = clrWhite;
      switch (Type) {
         case mtStatus:  color = clrWhite; break;
         case mtInfo:    color = clrGreen; break;
         case mtWarning: color = clrYellow; break;
         case mtError:   color = clrRed; break;
      }
      mOsd->SaveRegion(0, 0, mOsd->Width() - 1, mOsd->Height() - 1);
      mOsd->DrawRectangle(0, mOsd->Height() - lh - 2, mOsd->Width() - 1, mOsd->Height() - 1, clrBlue);
      mOsd->DrawRectangle(2, mOsd->Height() - lh, mOsd->Width() - 3, mOsd->Height() - 3, clrGray50);
      mOsd->DrawText(2, mOsd->Height() - lh - 2, Text, color, clrGray50, font, mOsd->Width() - 4, 0, taCenter);
   }
   else {
      mOsd->RestoreRegion();
   }
}

void cSkinEm84xxDisplayChannel::Flush(void)
{
   mOsd->Flush();
}

// --- cSkinEm84xxDisplayMenu -----------------------------------------------

class cSkinEm84xxDisplayMenu : public cSkinDisplayMenu {
private:
   cOsd *mOsd;
   int x0, x1;
   int y0, y1, y2, y3, y4, y5;
   int lineHeight;
   void SetScrollbar(void);
public:
   cSkinEm84xxDisplayMenu(void);
   virtual ~cSkinEm84xxDisplayMenu();
   virtual void Scroll(bool Up, bool Page);
   virtual int MaxItems(void);
   virtual void Clear(void);
   virtual void SetTitle(const char *Title);
   virtual void SetButtons(const char *Red, const char *Green = NULL, const char *Yellow = NULL, const char *Blue = NULL);
   virtual void SetMessage(eMessageType Type, const char *Text);
   virtual void SetItem(const char *Text, int Index, bool Current, bool Selectable);
   virtual void SetEvent(const cEvent *Event);
   virtual void SetRecording(const cRecording *Recording);
   virtual void SetText(const char *Text, bool FixedFont);
   virtual void Flush(void);
};

cSkinEm84xxDisplayMenu::cSkinEm84xxDisplayMenu(void)
{
   const cFont *font = cFont::GetFont(fontSml);
   int lh = font->Height();
   int top = Setup.OSDTop + (Setup.OSDHeight - lh * 5) / 2;
   int left = Setup.OSDLeft + (Setup.OSDWidth - 328) / 2;

   mOsd = cOsdProvider::NewOsd(top, left);

   tArea areas[] = { { 0, 0, 327, lh * 5 + 5, 4}};
   mOsd->SetAreas(areas, sizeof(areas) / sizeof(tArea));
}

cSkinEm84xxDisplayMenu::~cSkinEm84xxDisplayMenu()
{
   delete mOsd;
}

void cSkinEm84xxDisplayMenu::SetScrollbar(void)
{
}

void cSkinEm84xxDisplayMenu::Scroll(bool Up, bool Page)
{
}

int cSkinEm84xxDisplayMenu::MaxItems(void)
{
   return 3;
}

void cSkinEm84xxDisplayMenu::Clear(void)
{
   const cFont *font = cFont::GetFont(fontSml);
   int lh = font->Height();
   mOsd->DrawRectangle(0, 0, mOsd->Width() - 1, mOsd->Height() - 1, clrTransparent);
   mOsd->DrawRectangle(0, 0, mOsd->Width() - 1, mOsd->Height() - lh - 2, clrBlue);
   mOsd->DrawRectangle(2, 2, mOsd->Width() - 3, mOsd->Height() - lh - 4, clrGray50);
   mOsd->DrawRectangle(0, 2 + lh, mOsd->Width() - 1, 3 + lh, clrBlue);
}

void cSkinEm84xxDisplayMenu::SetTitle(const char *Title)
{
   const cFont *font = cFont::GetFont(fontSml);
   mOsd->DrawText(2, 2, Title, clrWhite, clrGray50, font, mOsd->Width() - 4, 0, taCenter);
}

void cSkinEm84xxDisplayMenu::SetButtons(const char *Red, const char *Green, const char *Yellow, const char *Blue)
{
   const cFont *font = cFont::GetFont(fontSml);
   int lh = font->Height();
   int w = mOsd->Width() / 4;
   if (Red != NULL) {
      mOsd->DrawRectangle(0, mOsd->Height() - lh - 1, w - 1, mOsd->Height() - 1, clrRed);
      mOsd->DrawRectangle(2, mOsd->Height() - lh - 1, w - 3, mOsd->Height() - 3, clrGray50);
      mOsd->DrawText(2, mOsd->Height() - lh, Red, clrWhite, 0, font, w - 4, 0, taCenter);
   }
   if (Green != NULL) {
      mOsd->DrawRectangle(w, mOsd->Height() - lh - 1, 2 * w - 1, mOsd->Height() - 1, clrGreen);
      mOsd->DrawRectangle(w + 2, mOsd->Height() - lh - 1, 2 * w - 3, mOsd->Height() - 3, clrGray50);
      mOsd->DrawText(w + 2, mOsd->Height() - lh, Green, clrWhite, 0, font, w - 4, 0, taCenter);
   }
   if (Yellow != NULL) {
      mOsd->DrawRectangle(2 * w, mOsd->Height() - lh - 1, 3 * w - 1, mOsd->Height() - 1, clrYellow);
      mOsd->DrawRectangle(2 * w + 2, mOsd->Height() - lh - 1, 3 * w - 3, mOsd->Height() - 3, clrGray50);
      mOsd->DrawText(2 * w + 2, mOsd->Height() - lh, Yellow, clrWhite, 0, font, w - 4, 0, taCenter);
   }
   if (Blue != NULL) {
      mOsd->DrawRectangle(3 * w, mOsd->Height() - lh - 1, 4 * w - 1, mOsd->Height() - 1, clrBlue);
      mOsd->DrawRectangle(3 * w + 2, mOsd->Height() - lh - 1, 4 * w - 3, mOsd->Height() - 3, clrGray50);
      mOsd->DrawText(3 * w + 2, mOsd->Height() - lh, Blue, clrWhite, 0, font, w - 4, 0, taCenter);
   }
}

void cSkinEm84xxDisplayMenu::SetMessage(eMessageType Type, const char *Text)
{
}

void cSkinEm84xxDisplayMenu::SetItem(const char *Text, int Index, bool Current, bool Selectable)
{
   const cFont *font = cFont::GetFont(fontSml);
   int lh = font->Height();
   int y = 4 + lh + Index * lh;
   tColor fg, bg;
   if (Current) {
      fg = clrBlack;
      bg = clrCyan;
   }
   else {
      fg = clrWhite;
      bg = clrGray50;
   }

   for (int i = 0; i < MaxTabs; i++) {
      const char *s = GetTabbedText(Text, i);
      if (s)
         mOsd->DrawText(2 + Tab(i) / 2, y, s, fg, bg, font, mOsd->Width() - Tab(i) / 2);
      if (!Tab(i + 1))
         break;
   }
   SetEditableWidth(mOsd->Width() - Tab(1) / 2 - 4);
}

void cSkinEm84xxDisplayMenu::SetEvent(const cEvent *Event)
{
}

void cSkinEm84xxDisplayMenu::SetRecording(const cRecording *Recording)
{
}

void cSkinEm84xxDisplayMenu::SetText(const char *Text, bool FixedFont)
{
}

void cSkinEm84xxDisplayMenu::Flush(void)
{
   mOsd->Flush();
}

// --- cSkinEm84xxDisplayReplay ---------------------------------------------

class cSkinEm84xxDisplayReplay : public cSkinDisplayReplay {
private:
   cOsd *osd;
   int x0, x1;
   int y0, y1, y2, y3;
   int lastCurrentWidth;
   bool message;
public:
   cSkinEm84xxDisplayReplay(bool ModeOnly);
   virtual ~cSkinEm84xxDisplayReplay();
   virtual void SetTitle(const char *Title);
   virtual void SetMode(bool Play, bool Forward, int Speed);
   virtual void SetProgress(int Current, int Total);
   virtual void SetCurrent(const char *Current);
   virtual void SetTotal(const char *Total);
   virtual void SetJump(const char *Jump);
   virtual void SetMessage(eMessageType Type, const char *Text);
   virtual void Flush(void);
};

cSkinEm84xxDisplayReplay::cSkinEm84xxDisplayReplay(bool ModeOnly)
{
}

cSkinEm84xxDisplayReplay::~cSkinEm84xxDisplayReplay()
{
}

void cSkinEm84xxDisplayReplay::SetTitle(const char *Title)
{
}

void cSkinEm84xxDisplayReplay::SetMode(bool Play, bool Forward, int Speed)
{
}

void cSkinEm84xxDisplayReplay::SetProgress(int Current, int Total)
{
}

void cSkinEm84xxDisplayReplay::SetCurrent(const char *Current)
{
}

void cSkinEm84xxDisplayReplay::SetTotal(const char *Total)
{
}

void cSkinEm84xxDisplayReplay::SetJump(const char *Jump)
{
}

void cSkinEm84xxDisplayReplay::SetMessage(eMessageType Type, const char *Text)
{
}

void cSkinEm84xxDisplayReplay::Flush(void)
{
}

// --- cSkinEm84xxDisplayVolume ---------------------------------------------

class cSkinEm84xxDisplayVolume : public cSkinDisplayVolume {
private:
   cOsd *mOsd;
public:
   cSkinEm84xxDisplayVolume(void);
   virtual ~cSkinEm84xxDisplayVolume();
   virtual void SetVolume(int Current, int Total, bool Mute);
   virtual void Flush(void);
};

cSkinEm84xxDisplayVolume::cSkinEm84xxDisplayVolume(void)
{
   const cFont *font = cFont::GetFont(fontOsd);
   int lh = font->Height();
   int w = font->Width("000");

   mOsd = cOsdProvider::NewOsd(Setup.OSDLeft, Setup.OSDTop + Setup.OSDHeight - (lh + 11));

   tArea areas[] = { { 0, 0, 380, lh + 20, 8}};
   mOsd->SetAreas(areas, sizeof(areas) / sizeof(tArea));
   mOsd->DrawRectangle(0, 0, mOsd->Width() - 1, mOsd->Height() - 1, clrTransparent);
   mOsd->DrawRectangle(0, 0, w + 40, 2 + lh - 1, clrBlue);
   mOsd->DrawRectangle(2, 2, w + 38, 2 + lh - 1, clrGray50);
   mOsd->DrawRectangle(2, 2 + lh, mOsd->Width() - 1, mOsd->Height() - 1, clrBlue);
   mOsd->DrawRectangle(2, 4 + lh, mOsd->Width() - 3, mOsd->Height() - 3, clrGray50);
   mOsd->DrawRectangle(4 + w, 0, 5 + w, 2 + lh - 1, clrBlue);
}

cSkinEm84xxDisplayVolume::~cSkinEm84xxDisplayVolume()
{
   delete mOsd;
}

void cSkinEm84xxDisplayVolume::SetVolume(int Current, int Total, bool Mute)
{
   const cFont *font = cFont::GetFont(fontOsd);
   int w = font->Width("000");
   mOsd->DrawText(2, 2, *itoa(Current), clrWhite, clrGray50, font, w, 0, taRight);
   int wp = (mOsd->Width() - 8) * Current / Total;
   mOsd->DrawRectangle(4, mOsd->Height() - 14, mOsd->Width() - 5, mOsd->Height() - 5, clrGray50);
   mOsd->DrawRectangle(4, mOsd->Height() - 14, 4 + wp - 1, mOsd->Height() - 5, clrCyan);
}

void cSkinEm84xxDisplayVolume::Flush(void)
{
   mOsd->Flush();
}

// --- cSkinEm84xxDisplayTracks ---------------------------------------------

class cSkinEm84xxDisplayTracks : public cSkinDisplayTracks {
private:
   cOsd *osd;
   int x0, x1;
   int y0, y1, y2;
   int lineHeight;
   int currentIndex;
   void SetItem(const char *Text, int Index, bool Current);
public:
   cSkinEm84xxDisplayTracks(const char *Title, int NumTracks, const char * const *Tracks);
   virtual ~cSkinEm84xxDisplayTracks();
   virtual void SetTrack(int Index, const char * const *Tracks);
   virtual void SetAudioChannel(int AudioChannel) {}
   virtual void Flush(void);
};

cSkinEm84xxDisplayTracks::cSkinEm84xxDisplayTracks(const char *Title, int NumTracks, const char * const *Tracks)
{
}

cSkinEm84xxDisplayTracks::~cSkinEm84xxDisplayTracks()
{
}

void cSkinEm84xxDisplayTracks::SetItem(const char *Text, int Index, bool Current)
{
}

void cSkinEm84xxDisplayTracks::SetTrack(int Index, const char * const *Tracks)
{
}

void cSkinEm84xxDisplayTracks::Flush(void)
{
}

// --- cSkinEm84xxDisplayMessage --------------------------------------------

class cSkinEm84xxDisplayMessage : public cSkinDisplayMessage {
private:
   cOsd *mOsd;
public:
   cSkinEm84xxDisplayMessage(void);
   virtual ~cSkinEm84xxDisplayMessage();
   virtual void SetMessage(eMessageType Type, const char *Text);
   virtual void Flush(void);
};

cSkinEm84xxDisplayMessage::cSkinEm84xxDisplayMessage(void)
{
   const cFont *font = cFont::GetFont(fontOsd);
   int lh = font->Height();

   mOsd = cOsdProvider::NewOsd(Setup.OSDLeft, Setup.OSDTop + Setup.OSDHeight - lh - 4);

   tArea Areas[] = { { 0, 0, Setup.OSDWidth - 1, lh + 3, 8}};
   mOsd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
   mOsd->DrawRectangle(0, 0, mOsd->Width() - 1, mOsd->Height() - 1, clrBlue);
   mOsd->DrawRectangle(2, 2, mOsd->Width() - 3, mOsd->Height() - 3, clrGray50);
}

cSkinEm84xxDisplayMessage::~cSkinEm84xxDisplayMessage()
{
   delete mOsd;
}

void cSkinEm84xxDisplayMessage::SetMessage(eMessageType Type, const char *Text)
{
   const cFont *font = cFont::GetFont(fontOsd);
   tColor color = clrWhite;
   switch (Type) {
      case mtStatus:  color = clrWhite; break;
      case mtInfo:    color = clrGreen; break;
      case mtWarning: color = clrYellow; break;
      case mtError:   color = clrRed; break;
   }
   mOsd->DrawText(2, 2, Text, color, 0, font, mOsd->Width() - 4, 0, taCenter);
}

void cSkinEm84xxDisplayMessage::Flush(void)
{
   mOsd->Flush();
}

// --- cSkinEm84xx ------------------------------------------------------------

cSkinEm84xx::cSkinEm84xx(void):
cSkin("em84xx")
{
}

cSkinEm84xx::~cSkinEm84xx()
{
}

cSkinDisplayChannel *cSkinEm84xx::DisplayChannel(bool WithInfo)
{
   return new cSkinEm84xxDisplayChannel(WithInfo);
}

cSkinDisplayMenu *cSkinEm84xx::DisplayMenu(void)
{
   return new cSkinEm84xxDisplayMenu();
}

cSkinDisplayReplay *cSkinEm84xx::DisplayReplay(bool ModeOnly)
{
   return new cSkinEm84xxDisplayReplay(ModeOnly);
}

cSkinDisplayVolume *cSkinEm84xx::DisplayVolume(void)
{
   return new cSkinEm84xxDisplayVolume();
}

cSkinDisplayTracks *cSkinEm84xx::DisplayTracks(const char *Title, int NumTracks, const char * const *Tracks)
{
   return new cSkinEm84xxDisplayTracks(Title, NumTracks, Tracks);
}

cSkinDisplayMessage *cSkinEm84xx::DisplayMessage(void)
{
   return new cSkinEm84xxDisplayMessage();
}

