/*
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * Author:
 *         Johannes Franz
 *
 * $Id: dvdrsched.cpp,v 1.10 2006/02/05 15:40:24 kutzner Exp $
 *
 */


/*
 * sched.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: dvdrsched.cpp,v 1.10 2006/02/05 15:40:24 kutzner Exp $
 */

#include <plugin.h>
#include <status.h>
#include <timers.h>
#include <tools.h>
#include "sched.h"
#include <iostream>

using namespace std;


static const char *VERSION        = "0.0.2";
static const char *DESCRIPTION    = "Schedules recordings over a network";
static const char *MAINMENUENTRY  = "Sched";





class cMyStatusMonitor : public cStatus {
    protected:
	virtual void ChannelSwitch(const cDevice *Device, int ChannelNumber);
	virtual void Recording(const cDevice *Device, const char *Name);
	virtual void Replaying(const cControl *Control, const char *Name);
	virtual void SetVolume(int Volume, bool Absolute);
	virtual void OsdClear(void);
	virtual void OsdTitle(const char *Title);
	virtual void OsdStatusMessage(const char *Message);
	virtual void OsdHelpKeys(const char *Red, const char *Green, const char *Yellow, const char *Blue);
	virtual void OsdCurrentItem(const char *Text);
	virtual void OsdTextItem(const char *Text, bool Scroll);
	virtual void OsdChannel(const char *Text);
	virtual void OsdProgramme(time_t PresentTime, const char *PresentTitle, const char *PresentSubtitle, time_t FollowingTime, const char *FollowingTitle, const char *FollowingSubtitle);
};

void cMyStatusMonitor::ChannelSwitch(const cDevice *Device, int ChannelNumber)
{
    //  dsyslog("status: cMyStatusMonitor::ChannelSwitch  %d %d", Device->CardIndex(), ChannelNumber);
    printf("channel switch\n");
}

void cMyStatusMonitor::Recording(const cDevice *Device, const char *Name)
{
    printf("status: cMyStatusMonitor::Recording  %d %s\n", Device->CardIndex(), Name);

}

void cMyStatusMonitor::Replaying(const cControl *Control, const char *Name)
{
    printf("status: cMyStatusMonitor::Replaying  %s\n", Name);
}

void cMyStatusMonitor::SetVolume(int Volume, bool Absolute)
{
    printf("status: cMyStatusMonitor::SetVolume  %d %d\n", Volume, Absolute);
}

void cMyStatusMonitor::OsdClear(void)
{
    printf("status: cMyStatusMonitor::OsdClear\n");
}

void cMyStatusMonitor::OsdTitle(const char *Title)
{
    printf("status: cMyStatusMonitor::OsdTitle '%s'\n", Title);
}

void cMyStatusMonitor::OsdStatusMessage(const char *Message)
{
    printf("status: cMyStatusMonitor::OsdStatusMessage '%s'\n", Message);
}

void cMyStatusMonitor::OsdHelpKeys(const char *Red, const char *Green, const char *Yellow, const char *Blue)
{
    printf("status: cMyStatusMonitor::OsdHelpKeys %s - %s - %s - %s\n", Red, Green, Yellow, Blue);
}

void cMyStatusMonitor::OsdCurrentItem(const char *Text)
{
    printf("status: cMyStatusMonitor::OsdCurrentItem %s\n", Text);
}

void cMyStatusMonitor::OsdTextItem(const char *Text, bool Scroll)
{
    printf("status: cMyStatusMonitor::OsdTextItem %s %d\n", Text, Scroll);
}

void cMyStatusMonitor::OsdChannel(const char *Text)
{
    printf("status: cMyStatusMonitor::OsdChannel %s\n", Text);
}

void cMyStatusMonitor::OsdProgramme(time_t PresentTime, const char *PresentTitle, const char *PresentSubtitle, time_t FollowingTime, const char *FollowingTitle, const char *FollowingSubtitle)
{
    char buffer[25];
    struct tm tm_r;
    printf("status: cMyStatusMonitor::OsdProgramme\n");
    strftime(buffer, sizeof(buffer), "%R\n", localtime_r(&PresentTime, &tm_r));
    printf("%5s %s\n", buffer, PresentTitle);
    printf("%5s %s\n", "", PresentSubtitle);
    strftime(buffer, sizeof(buffer), "%R\n", localtime_r(&FollowingTime, &tm_r));
    printf("%5s %s\n", buffer, FollowingTitle);
    printf("%5s %s\n", "", FollowingSubtitle);
}



class cSchedOsd : public cOsdMenu {
    private:
	virtual void Set(void);
    public:
	cSchedOsd(); 
	~cSchedOsd(); 
//	virtual void Show(void);
	virtual eOSState ProcessKey(eKeys Key);
};

cSchedOsd::cSchedOsd() :  cOsdMenu("Title") {
    Set();
}


cSchedOsd::~cSchedOsd()
{
}

void cSchedOsd::Set(){
	Clear();
	SetTitle("welcome to the submenu");
	Add(new cOsdItem("parse videofiles", osUser1));
	Add(new cOsdItem("start server", osUser2));
	Add(new cOsdItem("start client", osUser3));
	Add(new cOsdItem("recurse", osUser4));
}

//void cSchedOsd::Show(void)
//{
//    osd = cOsdProvider::NewOsd(100, 50);
//    if (osd) {
//	tArea Area = { 0, 0, 99, 199,  4 };
//	osd->SetAreas(&Area, 1);
//	osd->DrawRectangle(0, 0, 99, 199, clrGray50);
//	osd->Flush();
//    }
//}

eOSState cSchedOsd::ProcessKey(eKeys Key)
{
    //cout << "ProcessKey " << endl;
    eOSState state = cOsdMenu::ProcessKey(Key);
    cout << "state: " << state << endl;
	switch (state){
		case osUser1:
			return osContinue;
		case osUser2:
			return osContinue;
		case osUser3:
			return osContinue;
		case osUser4:
			return osContinue;
		default:
			return state;
	}
//	if (state == osUnknown) {
//	    switch (Key & ~k_Repeat) {
//	    case kUp:     cout << "kUp" << endl; break;
//	    case kDown:   cout << "kDown" << endl; break;
//	    case kLeft:   cout << "kLeft" << endl; break;
//	    case kRight:  cout << "kRight" << endl; break;
//	    case kRed:    cout << "kRed" << endl; break;
//	    case kGreen:  cout << "kGreen" << endl; break;
//	    case kYellow: cout << "kYellow" << endl; break;
//	    case kBack:   cout << "kBlue" << endl; return osBack;
//	    case kOk:     cout << "kOK" << endl; return osChannels;
//			  // Enter
//	    default: return state;
//	    }
//	    state = osContinue;
//	}
    //Display();
    return osContinue;
}


class cPluginSched : public cPlugin {
    private:
	// Add any member variables or functions you may need here.
	cMyStatusMonitor *statusMonitor;  

	//	cSchedMsgQueue *_q;
	//	cIgorRecv * _receiverthread;
	//    cTimerSearch * timersearch;
	cMainThread mt;
    public:
	cPluginSched(void);
	virtual ~cPluginSched();
	virtual const char *Version(void) { return VERSION; }
	virtual const char *Description(void) { return DESCRIPTION; }
	virtual const char *CommandLineHelp(void);
	virtual bool ProcessArgs(int argc, char *argv[]);
	virtual bool Initialize(void);
	virtual bool Start(void);
	virtual void Housekeeping(void);
	virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
	virtual cOsdObject *MainMenuAction(void);
	virtual cMenuSetupPage *SetupMenu(void);
	virtual bool SetupParse(const char *Name, const char *Value);

	//	void handlemsg(cSchedMsg *m);
};

cPluginSched::cPluginSched(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!


    statusMonitor = NULL;
    //	_q = NULL;
    //	_receiverthread = NULL;

    //    timersearch = NULL;
}

cPluginSched::~cPluginSched()
{
    // Clean up after yourself!
    delete statusMonitor;

    /*  
	if (_receiverthread){
	if (_receiverthread->Running(){
	_receiverthread->Stop();
	}
	delete _receiverthread; // is this immediatedly allowed?
	}
	if (_q){
	delete _q;
	}
	*/
    printf("Ende\n");
}

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

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

bool cPluginSched::Initialize(void)
{
    // Initialize any background activities the plugin shall perform.
    //    char *buf = "hello\n";
    //    write(STDOUT_FILENO, buf, 6);


    return true;
}

bool cPluginSched::Start(void)
{
    // Start any background activities the plugin shall perform.
    //    printf("Starting2_2\n");

    //  statusMonitor = new cMyStatusMonitor;
    /*
       int socketd;
       int error;
       char *myaddress = "blablasdf";

       char *myapptypestr = "test";
       char *msg = "hallo";

       sa_igor mysa_igor;
       igor_apptype myapptype;

       mysa_igor.address = myaddress;
       mysa_igor.len = strlen(myaddress);
       mysa_igor.mustbehashed = 1;

       myapptype.apptype = "TEST";
       myapptype.len = strlen(myapptype.apptype);
       myapptype.mustbehashed = 1;

       if ( (socketd = igor_socket()) < 0) {
       fprintf(stderr, "error igor_socket()\n");
       }

#ifdef DEBUGSCHED
cout << "socketd = " << socketd << endl;
#endif

if ( (error = igor_bind(socketd, &myapptype))  < 0) {
fprintf(stderr, "error igor_bind()\n");
}

#ifdef DEBUGSCHED
cout << "igor_bind returned: " << error << endl;
#endif


*/

//  igor_sendto(socketd, &msg, sizeof(msg), 1, &mysa_igor);

//    Channels.SwitchTo(20);



// the igor socket is created; start the receiving thread

cout << "starting main thread " << endl;
mt.Start();



/*
   timersearch = new cTimerSearch();
   timersearch->Start();
   */


return true;
}

void cPluginSched::Housekeeping(void)
{
    // Perform any cleanup or other regular tasks.
    /*
    // handle all message in the queue
    bool msgready = false;
    _q.lock(); msgready = _q.empty(); _q.unlock();
    while (msgready){
    cSchedMsg *m;
    _q.lock(); m =  _q.get(); _q.unlock();
    handlemsg(m);
    _q.lock(); msgready = _q.empty(); _q.unlock();
    }
    */

}

cOsdObject *cPluginSched::MainMenuAction(void)
{
    // Perform the action when selected from the main VDR menu.


    //    printf("%s\n", ConfigDirectory());
    // return NULL;
    return new cSchedOsd;
}

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

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


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