/* 
 * ffnetdev.c: Full Featured Network Device VDR-Plugin for Streaming 
 *
 * See the README file for copyright information and how to reach the author.
 *
 */
 
#include <getopt.h>
#include <stdlib.h>

#include "i18n.h"
#include "osdworker.h"
#include "tsworker.h"
#include "netosd.h"
#include "ffnetdev.h"
#include "streamdevice.h"
#include "remote.h"


const char *cPluginFFNetDev::VERSION = "0.0.4";
const char *cPluginFFNetDev::DESCRIPTION 		= "Full Featured Network Device for Streaming";
//const char *cOSDWorker::MAINMENUENTRY 		= "FFNetDev";
 
// --- cNetOSDProvider -----------------------------------------------

cNetOSDProvider::cNetOSDProvider(void)
{
	
}

cOsd * cNetOSDProvider::CreateOsd(int Left, int Top)
{
    osd = new cNetOSD(Left, Top);
    return osd;
}


// --- cPluginFFNetDev ----------------------------------------------------------

const char *cPluginFFNetDev::Version(void) {
		return tr(VERSION);
}

const char *cPluginFFNetDev::Description(void) {
		return tr(DESCRIPTION);
}

cPluginFFNetDev::cPluginFFNetDev(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!
  TSPort = STREAMPORT;
  OSDPort = OSDPORT;
  EnableRemote = false;
}

cPluginFFNetDev::~cPluginFFNetDev()
{
  cOSDWorker::Exit();
  cTSWorker::Exit();
}

const char *cPluginFFNetDev::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return 
  "  -t PORT, --tsport PORT      port number for sending TS to.\n"
  "  -o PORT, --osdport PORT     listen on this port for OSD connect.\n"
  "  -e 			 enable remote control over OSD connection.\n"
  "\n";
}

bool cPluginFFNetDev::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  fprintf (stderr, "[ffnetdev] processing args.\n");
  static struct option long_options[] = {
      { "tsport",    		required_argument	, NULL, 't' },
      { "osdport",   		required_argument	, NULL, 'o' },
      { "enable-remote",   	no_argument		, NULL, 'e' },
      { NULL }
  };

  int c;
  while ((c = getopt_long(argc, argv, "t:o:e", long_options, NULL)) != -1) {
        switch (c) {
          case 'e': EnableRemote = true;
          	    break;
          case 't': if (isnumber(optarg)) {
                       int n = atoi(optarg);
                       if (0 < n && n < 65536) {
                          TSPort = n;
                          fprintf(stderr, "[ffnetdev] TS Port: %d\n", n);
                          break;
                       }
                    }
                    fprintf(stderr, "[ffnetdev] invalid port number: %s\n", optarg);
                    return 2;
                    break;
          case 'o': if (isnumber(optarg)) {
                       int n = atoi(optarg);
                       if (0 < n && n < 65536) {
                          OSDPort = n;
                          fprintf(stderr, "[ffnetdev] OSD Port: %d\n", n);
                          break;
                       }
                    }
                    fprintf(stderr, "[ffnetdev] invalid port number: %s\n", optarg);
                    return 2;
                    break;
	  default : return 2;
	}
  }
  fprintf (stderr, "[ffnetdev] finished processing args.\n");
  return true;
}

bool cPluginFFNetDev::Active(void) {
	return (cOSDWorker::Active() || cTSWorker::Active());
}

bool cPluginFFNetDev::Start(void)
{
  // Start any background activities the plugin shall perform.
  RegisterI18n(Phrases);
  
  if(EnableRemote) {
	  m_Remote = new cMyRemote("ffnetdev");
	  fprintf(stderr, "[ffnetdev] remote control enabled.\n");
  }
  else {
	  fprintf(stderr, "[ffnetdev] remote control disabled.\n");
  	  m_Remote = NULL;
  }	  
  cOSDWorker::Init(m_Remote, OSDPort);
  cTSWorker::Init(m_StreamDevice, TSPort);
 
  return true;
}

bool cPluginFFNetDev::Initialize(void)
{
  // Start any background activities the plugin shall perform.
  fprintf(stderr,"[ffnetdev] initializing plugin.\n");
  m_StreamDevice = new cStreamDevice();
  return true;
}

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

cOsdObject *cPluginFFNetDev::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  fprintf (stderr, "[ffnetdev] MainMenuAction called.\n");
//  return new cMenuSetupSoftdevice;
  return NULL;
}

cMenuSetupPage *cPluginFFNetDev::SetupMenu(void)
{
  // Return our setup menu
//  return new cMenuSetupFFNetDev;
  return NULL;
}

bool cPluginFFNetDev::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  return true;
//  return setupStore.SetupParse(Name, Value);
}

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