#include <vdr/tools.h>

#include "osdproxy.h"

cOsdProxy::cOsdProxy(OSDCREATEFUNC osdMenuCreateFunction, KEYHANDLERFUNC keyHandler, 
                     RELEASEFUNC releaseFunc,const char* title, cOsdProxyData* data,
                     eKeys osdKey, bool directShow)
{
  mOsdMenuCreateFunc = osdMenuCreateFunction;
  mData = data;
  mTitle = strdup(title);
  mOsdMenu = NULL;
  mOsdKey = osdKey;
  mKeyHandlerFunc = keyHandler;
  mReleaseFunc = releaseFunc;

  if (directShow)
    ShowOsd(true);
}

cOsdProxy::~cOsdProxy()
{
  if (mOsdMenu)
    ShowOsd(false);
  if (mTitle)
    free(mTitle);

  if (mReleaseFunc)
    mReleaseFunc();
}

void cOsdProxy::Show(void)
{
  if (mOsdMenu)
    mOsdMenu->Display();
}

void cOsdProxy::ShowOsd(bool show)
{
  if ((!show) && (mOsdMenu))
    DELETENULL(mOsdMenu);
  else if ((show) && (!mOsdMenu))
    mOsdMenu = mOsdMenuCreateFunc(mTitle,mData);
}

eOSState cOsdProxy::ProcessKey(eKeys Key)
{
  if (mOsdMenu)
  {
    eOSState retState = mOsdMenu->ProcessKey(Key);
    if (retState == osEnd)
    {
      ShowOsd(false);
      return osContinue;
    }
    return retState;
  }
  else if (Key == mOsdKey)
  {
    ShowOsd(true);
    return osContinue;
  }
  else if (Key == kBack)
    return osEnd;
  else if (mKeyHandlerFunc)
    return mKeyHandlerFunc(Key);
  else
    return osContinue;
}
