#include <vdr/plugin.h>
#include "setup.h"
#include "menu.h"
#include "serial.h"

cMenuLcdControl::cMenuLcdControl():cOsdMenu("Lcdcontrol") {
	Add(new cOsdItem("Acpect Ratio"));
 	Add(new cOsdItem("MainMenu"));
}

eOSState cMenuLcdControl::ProcessKey(eKeys Key) {
	
	eOSState state = cOsdMenu::ProcessKey(Key);

	if (HasSubMenu()) return osContinue;
    if (Key == kOk){
    	switch (cOsdMenu::Current ()) {
    		case 0: {
    			AddSubMenu(new cMenuLcdControlAspectRatio);
    			if ( LcdControlSetup.MenuUpDown != NULL){
          			return (osPlugin);
    			}
    			if ( LcdControlSetup.MenuUpDown == NULL && state == 1){
          			return (osEnd);
    			}
    			break;
    		}
    		case 1: {
    			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdMenu);
      			SerialPort.closePort();
      			LcdControlSetup.MenuMainMenu = new cMenuLcdControlMainMenu;
  				return (osPlugin);
      			break;
    		}
    		default: break;
		}
    }
	return state;
}

cMenuLcdControlAspectRatio::cMenuLcdControlAspectRatio():cOsdMenu("Acpect Ratio") {
	Add(new cOsdItem("16:9"));
 	Add(new cOsdItem("Auto"));
 	Add(new cOsdItem("4:3"));
 	Add(new cOsdItem("14:9"));
 	Add(new cOsdItem("Spektral"));
 	Add(new cOsdItem("Vollbild"));
 	Add(new cOsdItem("Zoom1"));
 	Add(new cOsdItem("Zoom2"));
}

eOSState cMenuLcdControlAspectRatio::ProcessKey(eKeys Key) {
	
	eOSState state = cOsdMenu::ProcessKey(Key);
	
    if (Key == kOk){
    	switch (cOsdMenu::Current ()) {
    		case 0: {
      			dsyslog("Switching TV to 16:9");
      			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdAspectRatio16by9);
      			SerialPort.closePort();
      			return (osBack);
      			break;
    		}
    		case 1: {
      			dsyslog("Switching TV to Auto");
      			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdAspectRatioAuto);
      			SerialPort.closePort();
      			return (osBack);
      			break;
    		}
    		case 2: {
      			dsyslog("Switching TV to 4:3");
      			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdAspectRatio4by3);
      			SerialPort.closePort();
      			return (osBack);
      			break;
    		}
    		case 3: {
      			dsyslog("Switching TV to 14:9");
      			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdAspectRatio14by9);
      			SerialPort.closePort();
  				LcdControlSetup.MenuUpDown = new cMenuLcdControlUpDown;
  				return (osBack);
      			break;
    		}
    		case 4: {
      			dsyslog("Switching TV to Spectral");
      			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdAspectRatioSpectral);
      			SerialPort.closePort();
      			LcdControlSetup.MenuUpDown = new cMenuLcdControlUpDown;
  				return (osBack);
      			break;
    		}
    		case 5: {
      			dsyslog("Switching TV to Fullscreen");
      			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdAspectRatioFull);
      			SerialPort.closePort();
      			LcdControlSetup.MenuUpDown = new cMenuLcdControlUpDown;
  				return (osBack);
      			break;
    		}
    		case 6: {
      			dsyslog("Switching TV to Zoom 1");
      			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdAspectRatioZoom1);
      			SerialPort.closePort();
      			LcdControlSetup.MenuUpDown = new cMenuLcdControlUpDown;
  				return (osBack);
      			break;
    		}
    		case 7: {
      			dsyslog("Switching TV to Zoom 2");
      			SerialPort.openPort(LcdControlSetup.serialPort);
      			SerialPort.writePort(LcdControlSetup.cmdAspectRatioZoom2);
      			SerialPort.closePort();
      			LcdControlSetup.MenuUpDown = new cMenuLcdControlUpDown;
  				return (osBack);
      			break;
    		}
    		default: break;
		}
    }
	return state;
}

cMenuLcdControlUpDown::cMenuLcdControlUpDown()
{
}

cMenuLcdControlUpDown::~cMenuLcdControlUpDown(){
	LcdControlSetup.MenuUpDown = NULL;
}

eOSState cMenuLcdControlUpDown::ProcessKey(eKeys Key)
{
  eOSState state = cOsdObject::ProcessKey(Key);
  if (Key == kUp) {
  	SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdUp);
    SerialPort.closePort();
  }
  if (Key == kDown) {
  	SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdDown);
    SerialPort.closePort();
  }
  if (Key == kBack){
  	 return osEnd;
  }
  if (Key == kOk){
  	 return osEnd;
  }
  if (Key == kMenu){
  	 return osEnd;
  }
  return state;
}

void cMenuLcdControlUpDown::Show(void)
{
}

cMenuLcdControlMainMenu::cMenuLcdControlMainMenu()
{
}

cMenuLcdControlMainMenu::~cMenuLcdControlMainMenu(){
	LcdControlSetup.MenuMainMenu = NULL;
}

eOSState cMenuLcdControlMainMenu::ProcessKey(eKeys Key)
{
  eOSState state = cOsdObject::ProcessKey(Key);
  if (Key == kUp) {
  	SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdUp);
    SerialPort.closePort();
  }
  if (Key == kDown) {
  	SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdDown);
    SerialPort.closePort();
  }
  if (Key == kLeft) {
  	SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdLeft);
    SerialPort.closePort();
  }
  if (Key == kRight) {
  	SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdRight);
    SerialPort.closePort();
  }
  if (Key == kBack){
  	SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdBack);
    SerialPort.closePort();
  }
  if (Key == kOk){
  	SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdOk);
    SerialPort.closePort();
  }
  if (Key == kMenu){
  	 return osEnd;
  }
  return state;
}

void cMenuLcdControlMainMenu::Show(void)
{
}
