/*
 * screenshot.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "screenshot.h"

#include <vdr/status.h>
#include <vdr/skins.h>
#include <vdr/lirc.h>

void cMenuSetupScreenshot::Store(void)
{
  strcpy(sPath, sNewPath);
  iFileformat = iNewFileformat;
  iQuality = iNewQuality;
  iNoOfPics = iNewNoOfPics;
  iHideMenuEntry = iNewHideMenuEntry;
  SetupStore("Path", sPath);
  SetupStore("Fileformat", iFileformat);
  SetupStore("Quality", iQuality);
  SetupStore("NoOfPics", iNoOfPics);
  SetupStore("HideMenuEntry", iHideMenuEntry);
  SetupStore("UserKey", iUserKey);
  SetupStore("HideOsd", iHideOsd);
}
  
cMenuSetupScreenshot::cMenuSetupScreenshot(void)
{
  strcpy(sNewPath, sPath);
  iNewFileformat = iFileformat;
  iNewQuality = iQuality;
  iNewNoOfPics = iNoOfPics;
  iNewHideMenuEntry = iHideMenuEntry;
  iNewUserKey = iUserKey;
  iNewHideOsd = iHideOsd;
  char allowedChars[100];
  sprintf(allowedChars, "%s/", tr(FileNameChars));
  Add(new cMenuEditStrItem  (tr("Image Directory"),         sNewPath, sizeof(sNewPath), allowedChars));
  Add(new cMenuEditStraItem (tr("Fileformat"),              &iNewFileformat, 2, FILEFORMATS));
  Add(new cMenuEditIntItem  (tr("Image Quality (1-100)"),   &iNewQuality, 1, 100));
  Add(new cMenuEditIntItem  (tr("No. of pictures to take"), &iNewNoOfPics, 1, 100));
  Add(new cMenuEditBoolItem (tr("Hide mainmenu entry"),     &iNewHideMenuEntry));
//  Add(new cMenuEditIntItem  (tr("User key to use"),         &iNewUserKey, 1, 9));
//  Add(new cMenuEditBoolItem (tr("Hide OSD"),                &iNewHideOsd));
}

cPluginScreenshot::cPluginScreenshot(void)
{
  // Initialize any member variables here.
  // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
  title[0] = 0;
}

cPluginScreenshot::~cPluginScreenshot()
{
  // Clean up after yourself!
}

const char *cPluginScreenshot::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return NULL;
}

bool cPluginScreenshot::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  return true;
}

bool cPluginScreenshot::Initialize(void)
{
  // Initialize any background activities the plugin shall perform.
  return true;
}

bool cPluginScreenshot::Start(void)
{
  // Start any background activities the plugin shall perform.
  RegisterI18n(Phrases);
  // Make dummy picture at startup, because the first screenshot using
  // GrabImage(...) only takes a gray image.
  cDevice::PrimaryDevice()->GrabImage("/dev/null", false);
  return true;
}

void cPluginScreenshot::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
}

cOsdObject *cPluginScreenshot::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  // clear any osd visible
  if(cControl::Control())
    cControl::Control()->Hide();
  //TODO how delete proper menu
  cStatus::MsgOsdClear();
  // take pictures
  char fileName[100];
  time_t t = time(NULL);
  struct tm *now = localtime(&t);
  int i = 0;
  int pnr = 0;
  bool isOk = true;
  while (isOk && i < iNoOfPics)
  {
    FILE *fp;
    do {
      pnr++;
      sprintf(fileName, "%s/%s-%d%02d%02d-%03d.%s", sPath, title, \
    	    now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, \
	    pnr, FILEFORMATS[iFileformat]);
      fp = fopen(fileName, "r");
      if (fp) fclose(fp);
    } while (fp != NULL);
    isOk = cDevice::PrimaryDevice()->GrabImage(fileName, iFileformat, iQuality);
    i++;
  }
  // show result
  if (isOk)
    isyslog("screenshot: %d image%s saved", iNoOfPics, (iNoOfPics > 1) ? "s" : "");
  else
    esyslog("screenshot: error calling GrabImage(...)");
  if (isOk)
    Skins.Message(mtInfo, tr("OK - Images saved."));
  else
    Skins.Message(mtError, tr("Error"));
  Skins.Flush();
  return NULL;
}

/*
void cPluginScreenshot::UserKeyPressed(eKeys Key)
{
  if (Key != kUser1 + iUserKey - 1)
    return;
    
  if (iHideOsd)
  {
    if(cControl::Control())
      cControl::Control()->Hide();
    //TODO how delete proper menu
    cStatus::MsgOsdClear();
  }
  // take pictures
  char fileName[100];
  time_t t = time(NULL);
  struct tm *now = localtime(&t);
  int i = 0;
  bool isOk = true;
  while (isOk && i < iNoOfPics)
  {
    sprintf(fileName, "%s/screenshot-%d%02d%02d-%02d%02d%02d-%02d.%s", sPath, \
    	    now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, \
            now->tm_hour, now->tm_min, now->tm_sec, \
	    i, FILEFORMATS[iFileformat]);
    isOk = cDevice::PrimaryDevice()->GrabImage(fileName, iFileformat, iQuality);
    i++;
  }
  // show result
  if (isOk)
    isyslog("screenshot: %d image%s saved", iNoOfPics, (iNoOfPics > 1) ? "s" : "");
  else
    esyslog("screenshot: error calling GrabImage(...)");
  if (isOk)
    Skins.Message(mtInfo, tr("OK"));
  else
    Skins.Message(mtError, tr("Error"));
  Skins.Flush();
  return;
}
*/

cMenuSetupPage *cPluginScreenshot::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.
  return new cMenuSetupScreenshot;
}

bool cPluginScreenshot::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  if       (!strcasecmp(Name, "Path"))          strcpy(sPath, Value);
  else if  (!strcasecmp(Name, "Quality"))       iQuality       = atoi(Value);
  else if  (!strcasecmp(Name, "Fileformat"))    iFileformat    = atoi(Value);
  else if  (!strcasecmp(Name, "NoOfPics"))      iNoOfPics      = atoi(Value);
  else if  (!strcasecmp(Name, "HideMenuEntry")) iHideMenuEntry = atoi(Value);
  else if  (!strcasecmp(Name, "UserKey"))       iUserKey       = atoi(Value);
  else if  (!strcasecmp(Name, "HideOsd"))       iHideOsd       = atoi(Value);
  else return false;
  return true;
}

void cPluginScreenshot::Replaying(const cControl *Control, const char *Name)
{
  if (Name)
    strncpy(title, Name, sizeof(title));
  else
    strcpy(title, "screenshot");
}

/*
void cPluginScreenshot::ChannelSwitch(const cDevice *Device, int ChannelNumber)
{
  if (ChannelNumber && Device->IsPrimaryDevice())
    strncpy(title, (Channels.GetByNumber(ChannelNumber))->Name(), sizeof(channelName));
}
*/

void cPluginScreenshot::OsdProgramme(time_t PresentTime, const char *PresentTitle, const char *PresentSubtitle, time_t FollowingTime, const char *FollowingTitle, const char *FollowingSubtitle)
{
  if (PresentTitle)
    strncpy(title, PresentTitle, sizeof(title));
  else
    strcpy(title, "screenshot");
}

VDRPLUGINCREATOR(cPluginScreenshot); // Don't touch this!
