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

#include <vdr/plugin.h>
#include <getopt.h>

#include "i18n.h"
#include "setup.h"
#include "IcetftSwitch.h"

static const char *VERSION        = "0.0.1";
static const char *DESCRIPTION    = "Switching ICE-TFT on and off";
static const char *MAINMENUENTRY  = "ICE-TFT";

class cPluginIcetft : public cPlugin
{
    private:
        // nothing yet
    public:
        cPluginIcetft(void);
        virtual ~cPluginIcetft();
        virtual const char *Version(void) { return VERSION; }
        virtual const char *Description(void) { return tr(DESCRIPTION); }
        virtual const char *CommandLineHelp(void);
        virtual bool ProcessArgs(int argc, char *argv[]);
        virtual bool Initialize(void);
        virtual bool Start(void);
        virtual void Stop(void);
        virtual void Housekeeping(void);
        virtual void MainThreadHook(void);
        virtual cString Active(void);
        virtual const char *MainMenuEntry(void);
        virtual cOsdObject *MainMenuAction(void);
        virtual cMenuSetupPage *SetupMenu(void);
        virtual bool SetupParse(const char *Name, const char *Value);
        virtual bool Service(const char *Id, void *Data = NULL);
        virtual const char **SVDRPHelpPages(void);
        virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
};

cPluginIcetft::cPluginIcetft(void)
{
    // Initialize any member variables here.
}


cPluginIcetft::~cPluginIcetft()
{
    // Clean up after yourself!
    delete IcetftSwitch;
}


const char *cPluginIcetft::CommandLineHelp(void)
{
    // Return a string that describes all known command line options.
    return "  -d DEV,   --device=DEV use DEV as serial device (default /dev/ttyS0)\n";;
}


bool cPluginIcetft::ProcessArgs(int argc, char *argv[])
{
    // Implement command line argument processing here if applicable.
    static struct option long_options[] =
    {
        { "device", required_argument, NULL, 'd' },
        { NULL }
    };
    int c;

    strcpy(IcetftSetup.device, "/dev/ttyS0");
    while ((c = getopt_long(argc, argv, "d:", long_options, NULL)) != -1)
    {
        switch (c)
        {
            case 'd':
                strcpy(IcetftSetup.device, optarg);
                break;
            default:
                return false;
        }
    }
    return true;
}


bool cPluginIcetft::Initialize(void)
{
    // IcetftSwitch
    IcetftSwitch=new cIcetftSwitch();
    return true;
}


bool cPluginIcetft::Start(void)
{
    // Start any background activities the plugin shall perform.
    RegisterI18n(Phrases);

    if (IcetftSetup.stateAtStartup)
    {
        IcetftSwitch->setAction(on);
    }
    else
    {
        IcetftSwitch->setAction(off);
    }
    return true;
}


void cPluginIcetft::Stop(void)
{
    // Stop any background activities the plugin shall perform.
}


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


void cPluginIcetft::MainThreadHook(void)
{
    // Perform actions in the context of the main program thread.
    // WARNING: Use with great care - see PLUGINS.html!
}


cString cPluginIcetft::Active(void)
{
    // Return a message string if shutdown should be postponed
    return NULL;
}


const char *cPluginIcetft::MainMenuEntry(void)
{
    return IcetftSetup.hidemenu ? 0 : tr(MAINMENUENTRY);
}


cOsdObject *cPluginIcetft::MainMenuAction(void)
{
    IcetftSwitch->setAction(toggle);

    tState tftstate=IcetftSwitch->getTftState();
    if (IcetftSetup.message)
    {
        if (tftstate==on)
            Skins.Message(mtInfo, tr("TFT on"), 1);
        else if (tftstate==off)
            Skins.Message(mtInfo, tr("TFT off"), 1);
    }
    return NULL;
}


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


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


bool cPluginIcetft::Service(const char *Id, void *Data)
{
   // only experimental for now
    if (strcmp(Id,"Icetft-On-v1.0") == 0)
    {
        if (Data)
        {
            IcetftSwitch->setAction(on);
        }
        return true;
    }

    if (strcmp(Id,"Icetft-Off-v1.0") == 0)
    {
        if (Data)
        {
            IcetftSwitch->setAction(off);
        }
        return true;
    }

    if (strcmp(Id,"Icetft-Toggle-v1.0") == 0)
    {
        if (Data)
        {
            IcetftSwitch->setAction(toggle);
        }
        return true;
    }
    /* todo
        if (strcmp(Id,"Icetft-Status-v1.0") == 0)
        {
            if (Data)
            {
                char *data = (char*)Data;
                if (IcetftSwitch->getTftState()==on)
                   strcpy(data,"on");
               else
                   strcpy(data,"off");
            }
    return true;
    }
    */
    return false;
}


const char **cPluginIcetft::SVDRPHelpPages(void)
{
    // Return help text for SVDRP commands this plugin implements
    static const char *HelpPages[] =
    {
        "ON\n"
        "    Switch TFT on.",
        "OFF\n"
        "    Switch TFT off.",
        "TOGGLE\n"
        "    Toggle TFT status (on->off, off->on).",
        "STATUS\n"
        "    Return status of TFT (on/off).",
        NULL
    };
    return HelpPages;
}


cString cPluginIcetft::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{

    // Process SVDRP commands this plugin implements
    if (strcasecmp(Command, "ON") == 0)
    {
        IcetftSwitch->setAction(on);
        if(IcetftSwitch->getTftState()==on)
            return cString("on");
        else if (IcetftSwitch->getTftState()==error)
            return cString("error");
    }
    else if (strcasecmp(Command, "OFF") == 0)
    {
        IcetftSwitch->setAction(off);
        if(IcetftSwitch->getTftState()==off)
            return cString("off");
        else if (IcetftSwitch->getTftState()==error)
            return cString("error");
    }
    else if (strcasecmp(Command, "TOGGLE") == 0)
    {
        IcetftSwitch->setAction(toggle);
        if (IcetftSwitch->getTftState()==on)
            return cString("on");
        else if(IcetftSwitch->getTftState()==off)
            return cString("off");
        else if (IcetftSwitch->getTftState()==error)
            return cString("error");
    }
    else if (strcasecmp(Command, "STATUS") == 0)
    {
        if (IcetftSwitch->getTftState()==on)
            return cString("on");
        else if(IcetftSwitch->getTftState()==off)
            return cString("off");
        else if (IcetftSwitch->getTftState()==error)
            return cString("error");
    }
    return NULL;
}


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