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

#include <vdr/plugin.h>
#include <vdr/osd.h>
#include "connect_donkey.h"
#include <string.h>
#include <iostream.h>
#include <getopt.h>
#include <stdlib.h>

static const char *VERSION        = "0.0.4a";
static const char *DESCRIPTION    = "Display download Status of mldonkey Client";
static const char *MAINMENUENTRY  = "Mldonkey";

class display : public cOsdObject {
	private:
		cOsd *osd;
		tColor color;
		int line; // the marked line
		int port;
		char server[200];
		char username[250];
		char password[250];

	public:
		display(char *s,int p,char *user, char *pass);
		~display(void);
		virtual void Show(void);
		virtual eOSState ProcessKey(eKeys Key);
};

display::display(char *s, int p,char *user, char *pass) {
	osd = NULL;
	color = clrRed;
	strcpy(server,s);
	port=p;
	strcpy(username,user);
	strcpy(password,pass);
}

display::~display() {
		delete osd;
}

void display::Show(void) {
	
	connect_donkey d;
	int err=d.get(server,port,username,password);
	if (err == -1) {
		cout << "DEBUG(vdr-mldonkey): "<< d.geterror() <<endl;
		osd = cOsdProvider::NewOsd(60,100);
		const cFont *font;
		font=cFont::GetFont(fontSml);
		if (osd) {
			tArea Area = {0, 0, 599, 350, 2};
			if (osd->CanHandleAreas(&Area,1) == oeOk) {
				osd->SetAreas(&Area,1);
			} else {
				tArea Areas[] = { {0,0,99,350,2},
                                                  {100,0,199,350,2},
                                                  {200,0,299,350,2},
                                                  {300,0,399,350,2},
                                                  {400,0,499,350,2},
                                                  {500,0,599,350,2}

                                                 };
                                osd->SetAreas(Areas,sizeof(Areas)/sizeof(tArea));
				
			}
			osd->DrawRectangle(0,0,599,350,clrGray50);
			osd->DrawText(0,0,d.geterror(),clrWhite,clrTransparent,font,450,20,taCenter | taTop);		
			osd->Flush();
		}	
	} else {
	char	text[4048];
	strcpy(text,"");
	int count;
	count = d.getcount();
	
	cout << "DEBUG(vdr-mldonkey): Anzahl Files" << count << endl;
	
	osd = cOsdProvider::NewOsd(60,100);
	const cFont *font;
	font=cFont::GetFont(fontSml);
	
	if (osd) {
		tArea Area = {0, 0, 599, 350, 2};
		if (osd->CanHandleAreas(&Area,1) == oeOk) {
				osd->SetAreas(&Area,1);
			} else {
				tArea Areas[] = { {0,0,99,350,2},
						  {100,0,199,350,2},
						  {200,0,299,350,2},
						  {300,0,399,350,2},
						  {400,0,499,350,2},
						  {500,0,599,350,2}
						  
						 };
				osd->SetAreas(Areas,sizeof(Areas)/sizeof(tArea));
			}
		
		osd->DrawRectangle(0,0,599,350,clrGray50);
		osd->DrawText(0,0,"Dateiname",clrWhite,clrTransparent,font,450,20,taCenter | taTop);
		osd->DrawText(480,0,"%",clrWhite,clrTransparent,font,49,20,taTop|taCenter);
		osd->DrawText(540,0,"kb/s",clrWhite,clrTransparent,font,49,20,taTop|taCenter);
		
		for(int i=1;i<=count;i++) {
			
			osd->DrawText(0,(i+1)*20,d.getfile(i),clrWhite,clrTransparent,font,450,20,taDefault);
			osd->DrawText(480,(i+1)*20,d.getpct(i),clrWhite,clrTransparent,font,49,20,taTop|taRight);
			osd->DrawText(540,(i+1)*20,d.getrate(i),clrWhite,clrTransparent,font,49,20,taTop|taRight);
			

		}
		
		
		osd->Flush();
	}
	}
}

eOSState display::ProcessKey(eKeys Key) {
	eOSState state = cOsdObject::ProcessKey(Key);
	if (state == osUnknown) {
		switch (Key & ~k_Repeat) {
			case kOk: return osEnd;
			case kBack: return osEnd;
			default: return state;
		}
		state = osContinue;
	}
	return state;
}


class cPluginMldonkey : public cPlugin {
private:
  char *mldonkey_server; // ip of the mldonkey client
  char *mldonkey_user;   // username to log onto with
  char *mldonkey_pass;	 // password for the user
  int mldonkey_port;	// port where the mldonkey client listens
 
  
public:
  cPluginMldonkey(void);
  virtual ~cPluginMldonkey();
  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);
  };

cPluginMldonkey::cPluginMldonkey(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!
  mldonkey_port=4000;
  mldonkey_user=strdup("none");
  mldonkey_pass=strdup("none");
  
}

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

const char *cPluginMldonkey::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return "mldonkey -h <server ip> -p <port number> -u <username> -k <password>";
  
}

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

static struct option long_options[] = {
       { "host",      required_argument, NULL, 'h' },
       { "port",      required_argument,       NULL, 'p' },
       { "user",      required_argument,       NULL, 'u' },
       { "password",      required_argument,       NULL, 'k' },
       { NULL }
     };

  int c;
  while ((c = getopt_long(argc, argv, "h:p:u:k:", long_options, NULL)) != -1) {
        switch (c) {
          case 'h': mldonkey_server = optarg;
                    break;
	  case 'p': mldonkey_port = atoi(optarg);
		    break;
	  case 'u': mldonkey_user = optarg;
		    break;
	  case 'k': mldonkey_pass = optarg;
		    break;
          default:  return false;
          }
        }
  return true;


}

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

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

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

cOsdObject *cPluginMldonkey::MainMenuAction(void)
{ 
  return new display(mldonkey_server,mldonkey_port,mldonkey_user,mldonkey_pass);
}

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

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

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