/*
 * mousemate plugin for VDR
 *
 * setup.c - settings and setup menu
 *
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 */

#include <linux/input.h>

#include "setup.h"
#include "MouseMate.h"

const char * strContexts[kContexts] =
{
    "Normal",
    "Menu",
    "Replay",
    "Replay DVD",
    "Replay MP3",
    "Replay CDDA",
    "Replay IMAGE",
    "Normal alternative",
    "Menu alternative",
    "Replay alternative",
    "Replay DVD alternative",
    "Replay MP3 alternative",
    "Replay CDDA alternative",
    "Replay IMAGE alternative"
};

const char * strEvents[kEvents] =
{
    "TurnLeft",
    "TurnRight",
    "PushedTurnLeft",
    "PushedTurnRight",
    "LeftClick",
    "LeftDoubleClick",
    "MiddleClick",
    "MiddleDoubleClick",
    "RightClick",
    "RightDoubleClick",
    "SideClick",
    "SideDoubleClick",
    "ExtraClick",
    "ExtraDoubleClick",
    "ForwardClick",
    "ForwardDoubleClick",
    "BackClick",
    "BackDoubleClick",
    "TaskClick",
    "TaskDoubleClick"
};


cMouseMateSetup::cMouseMateSetup()
    :sensitivity(1),
    doubleClickTime(300),
    toggleClickTime(1000),
    led(0),
    autotoggleoff(0),
    toggleOffTime(3000)
{
    for (int c = 0; c < kContexts; c++)
        for (int e = 0; e < kEvents; e++)
            keys[c][e] = kNone;
}


bool cMouseMateSetup::Parse(const char *Name, const char *Value)
{
    // Parse your own setup parameters and store their values.

    if (!strcasecmp(Name, "Sensitivity")) sensitivity = atoi(Value);
    else if (!strcasecmp(Name, "DoubleClickTime")) doubleClickTime = atoi(Value);
    else if (!strcasecmp(Name, "ToggleClickTime")) toggleClickTime = atoi(Value);
    else if (!strcasecmp(Name, "LED")) led = atoi(Value);
    else if (!strcasecmp(Name, "AutoToggleOff")) autotoggleoff = atoi(Value);
    else if (!strcasecmp(Name, "ToggleOffTime")) toggleOffTime = atoi(Value);
    else if (strstr(Name, "Key") == Name)
    {
        for (int c = 0; c < kContexts; c++)
        {
            for (int e = 0; e < kEvents; e++)
            {
                char str[256];
                sprintf(str, "Key%s%s", strContexts[c], strEvents[e]);
                if (!strcasecmp(Name, str))
                {
                    keys[c][e] = eKeys(atoi(Value));
                    return true;
                }
            }
        }
        return false;
    }
    else
        return false;
    return true;
}

static tKey keyTable[] = 
{ 
		    { kNone,          "None"       },
                    { kUp,            "Up"         },
                    { kDown,          "Down"       },
                    { kMenu,          "Menu"       },
                    { kOk,            "Ok"         },
                    { kBack,          "Back"       },
                    { kLeft,          "Left"       },
                    { kRight,         "Right"      },
                    { kRed,           "Red"        },
                    { kGreen,         "Green"      },
                    { kYellow,        "Yellow"     },
                    { kBlue,          "Blue"       },
                    { k0,             "0"          },
                    { k1,             "1"          },
                    { k2,             "2"          },
                    { k3,             "3"          },
                    { k4,             "4"          },
                    { k5,             "5"          },
                    { k6,             "6"          },
                    { k7,             "7"          },
                    { k8,             "8"          },
                    { k9,             "9"          },
                    { kInfo,          "Info"       },
                    { kPlay,          "Play"       },
                    { kPause,         "Pause"      },
                    { kStop,          "Stop"       },
                    { kRecord,        "Record"     },
                    { kFastFwd,       "FastFwd"    },
                    { kFastRew,       "FastRew"    },
                    { kNext,          "Next"       },
                    { kPrev,          "Prev"       },
                    { kPower,         "Power"      },
                    { kChanUp,        "Channel+"   },
                    { kChanDn,        "Channel-"   },
                    { kChanPrev,      "PrevChannel"},
                    { kVolUp,         "Volume+"    },
                    { kVolDn,         "Volume-"    },
                    { kMute,          "Mute"       },
                    { kAudio,         "Audio"      },
                    { kSchedule,      "Schedule"   },
                    { kChannels,      "Channels"   },
                    { kTimers,        "Timers"     },
                    { kRecordings,    "Recordings" },
                    { kSetup,         "Setup"      },
                    { kCommands,      "Commands"   },
                    { kUser1,         "User1"      },
                    { kUser2,         "User2"      },
                    { kUser3,         "User3"      },
                    { kUser4,         "User4"      },
                    { kUser5,         "User5"      },
                    { kUser6,         "User6"      },
                    { kUser7,         "User7"      },
                    { kUser8,         "User8"      },
                    { kUser9,         "User9"      },
                    { kNone,          NULL         },
                  };


cMouseMateSetupPage::cMouseMateSetupPage()
{
    context = kContextNormal;
    contextOld = kContextNormal;
    sensitivity = MouseMateSetup.sensitivity;
    doubleClickTime = MouseMateSetup.doubleClickTime;
    toggleClickTime = MouseMateSetup.toggleClickTime;
    led=MouseMateSetup.led;
    autotoggleoff=MouseMateSetup.autotoggleoff;
    toggleOffTime = MouseMateSetup.toggleOffTime;
    for (numKeys = 0; keyTable[numKeys].name; numKeys++)
    {
        for (int c = 0; c < kContexts; c++)
            for (int e = 0; e < kEvents; e++)
                if (keyTable[numKeys].type == MouseMateSetup.keys[c][e])
                    keys[c][e] = numKeys;
        strKeys[numKeys] = new char[strlen(keyTable[numKeys].name) + 1];
        strcpy(strKeys[numKeys], keyTable[numKeys].name);
    }
    for (int e = 0; e < kEvents; e++)
        setKeys[e] = keys[context][e];

    Add(new cMenuEditIntItem(tr("Sensitivity"), &sensitivity, 1, 10));
    Add(new cMenuEditIntItem(tr("Double click time (ms)"), &doubleClickTime, 100, 1000));
    Add(new cMenuEditIntItem(tr("Toggle click time (ms)"), &toggleClickTime, 100, 5000));
    Add(new cMenuEditBoolItem(tr("Show toggle with scroll lock led"), &led,tr("no"), tr("yes")));
    Add(itemToggle = new cMenuEditBoolItem(tr("Toggle automatic off"), &autotoggleoff,tr("no"), tr("yes")));
//    if (autotoggleoff) //TODO
//    {
    Add(new cMenuEditIntItem(tr("Toggle off time (ms)"), &toggleOffTime, 1000, 30000));
//    }

    Add(itemContext = new cMenuEditStraItem(tr("Set keys for context"), &context, kContexts, strContexts));
    Add(new cMenuEditStraItem(tr("Turn Left"), &setKeys[kEventTurnLeft], numKeys, strKeys));
    Add(new cMenuEditStraItem(tr("Turn Right"), &setKeys[kEventTurnRight], numKeys, strKeys));
    Add(new cMenuEditStraItem(tr("Pushed Turn Left"), &setKeys[kEventPushedTurnLeft], numKeys, strKeys));
    Add(new cMenuEditStraItem(tr("Pushed Turn Right"), &setKeys[kEventPushedTurnRight], numKeys, strKeys));

    Add(new cMenuEditStraItem(tr("Left click"), &setKeys[kEventLeftClick], numKeys, strKeys));
    Add(new cMenuEditStraItem(tr("Left double-click"), &setKeys[kEventLeftDoubleClick], numKeys, strKeys));
    Add(new cMenuEditStraItem(tr("Middle click"), &setKeys[kEventMiddleClick], numKeys, strKeys));
    Add(new cMenuEditStraItem(tr("Middle double-click"), &setKeys[kEventMiddleDoubleClick], numKeys, strKeys));
    Add(new cMenuEditStraItem(tr("Right click"), &setKeys[kEventRightClick], numKeys, strKeys));
    Add(new cMenuEditStraItem(tr("Right double-click"), &setKeys[kEventRightDoubleClick], numKeys, strKeys));

    if (MouseMate->IsButtonUsed(BTN_SIDE))
    {
        Add(new cMenuEditStraItem(tr("Side click"), &setKeys[kEventSideClick], numKeys, strKeys));
        Add(new cMenuEditStraItem(tr("Side double-click"), &setKeys[kEventSideDoubleClick], numKeys, strKeys));
    }
    if (MouseMate->IsButtonUsed(BTN_EXTRA))
    {
        Add(new cMenuEditStraItem(tr("Extra click"), &setKeys[kEventExtraClick], numKeys, strKeys));
        Add(new cMenuEditStraItem(tr("Extra double-click"), &setKeys[kEventExtraDoubleClick], numKeys, strKeys));
    }
    if (MouseMate->IsButtonUsed(BTN_FORWARD))
    {
        Add(new cMenuEditStraItem(tr("Forward click"), &setKeys[kEventForwardClick], numKeys, strKeys));
        Add(new cMenuEditStraItem(tr("Forward double-click"), &setKeys[kEventForwardDoubleClick], numKeys, strKeys));
    }
    if (MouseMate->IsButtonUsed(BTN_BACK))
    {
        Add(new cMenuEditStraItem(tr("Back click"), &setKeys[kEventBackClick], numKeys, strKeys));
        Add(new cMenuEditStraItem(tr("Back double-click"), &setKeys[kEventBackDoubleClick], numKeys, strKeys));
    }
    if (MouseMate->IsButtonUsed(BTN_TASK))
    {
        Add(new cMenuEditStraItem(tr("Task click"), &setKeys[kEventTaskClick], numKeys, strKeys));
        Add(new cMenuEditStraItem(tr("Task double-click"), &setKeys[kEventTaskDoubleClick], numKeys, strKeys));
    }
}


cMouseMateSetupPage::~cMouseMateSetupPage()
{
    for (int i = 0; i < numKeys; i++)
    {
        delete[] strKeys[i];
    }
}


void cMouseMateSetupPage::Store()
{
    SetupStore("Sensitivity", MouseMateSetup.sensitivity = sensitivity);
    SetupStore("DoubleClickTime", MouseMateSetup.doubleClickTime = doubleClickTime);
    SetupStore("ToggleClickTime", MouseMateSetup.toggleClickTime = toggleClickTime);
    SetupStore("LED", MouseMateSetup.led = led);
    SetupStore("AutoToggleOff", MouseMateSetup.autotoggleoff = autotoggleoff);
    SetupStore("ToggleOffTime", MouseMateSetup.toggleOffTime = toggleOffTime);
    for (int e = 0; e < kEvents; e++)
        keys[contextOld][e] = setKeys[e];
    for (int c = 0; c < kContexts; c++)
    {
        for (int e = 0; e < kEvents; e++)
        {
            char str[256];
            MouseMateSetup.keys[c][e] = keyTable[keys[c][e]].type;
            sprintf(str, "Key%s%s", strContexts[c], strEvents[e]);
            SetupStore(str, MouseMateSetup.keys[c][e]);
        }
    }

}


eOSState cMouseMateSetupPage::ProcessKey(eKeys key)
{
    eOSState state = cMenuSetupPage::ProcessKey(key);
    //calculating number of used events
    short int eventCount=MouseMate->Get_btnCount()*2+4;
    if (state == osContinue)
    {
        if (key == kLeft || key == kRight)
        {
            cOsdItem * item = Get(Current());
            if (item == itemContext)
            {
                for (int e = 0; e < eventCount; e++)
                {
                    keys[contextOld][e] = setKeys[e];
                    setKeys[e] = keys[context][e];
                    item = Get(Current() + e + 1);
                    item->Set();
                }
                contextOld = context;
                Display();
            }
            if (item == itemToggle)
            {
                Display();
 	    }

        }
    }
    return state;
}


cMouseMateSetup MouseMateSetup;
