/*
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "common.h"

class cMenuItem
{
    public:
        eSetupItem item;
        const char* title;
};

static const cMenuItem menuItems[]=
{
    { eSetupResolution, "Resolution" },
    { eSetupVolume, "Volume" },
    { eSetupAudioBitrate, "Audio bitrate" },
    { eSetupBrightness, "Brightness" },
    { eSetupContrast, "Contrast" },
    { eSetupHue, "Hue" },
    { eSetupSaturation, "Saturation" },
    { eSetupVideoBitrateAverage, "Average video bitrate" },
    { eSetupVideoBitratePeak, "Peak video bitrate" },
    { eSetupVbr, "Enable VBR mode" },
    { eSetupVideoDevice0, "Video device 1" },
    { eSetupVideoDevice1, "Video device 2" },
    { eSetupVideoDevice2, "Video device 3" },
    { eSetupVideoDevice3, "Video device 4" },
    { eSetupVideoStandard, "Video standard" },
};

cRMSetupMenu::cRMSetupMenu()
{
    newSetup=PvrUsb2->setup;
    vbrItem=NULL;

    DisplayMenu();
}

cRMSetupMenu::~cRMSetupMenu()
{
    if(PvrUsb2) {
        PvrUsb2->SetupMenuDestroyed();
    }
}

void cRMSetupMenu::Clear()
{
    cMenuSetupPage::Clear();
    vbrItem=NULL;
}

void cRMSetupMenu::Store()
{
    for(int i=0; i<newSetup.NumItems(); i++) {
        eSetupItem item=newSetup.Item(i);
        SetupStore(newSetup.ItemName(item), newSetup.Value(item));
    }

    PvrUsb2->UpdateSetup(&newSetup);
}

const char* cRMSetupMenu::ItemTitle(eSetupItem _item)
{
    for(int i=0; i<ENTRIES(menuItems, cMenuItem); i++) {
        if(menuItems[i].item==_item) {
            return menuItems[i].title;
        }
    }
    return NULL;
}

cMenuEditItem* cRMSetupMenu::CreateMenuItem(eSetupItem _item)
{
    cMenuEditItem* menuItem=NULL;
    switch(_item) {
        case eSetupResolution:
        case eSetupVolume:
        case eSetupAudioBitrate:
        case eSetupBrightness:
        case eSetupContrast:
        case eSetupHue:
        case eSetupSaturation:
        case eSetupVideoBitrateAverage:
        case eSetupVideoBitratePeak:
        case eSetupVideoStandard:
        case eSetupVideoDevice0:
        case eSetupVideoDevice1:
        case eSetupVideoDevice2:
        case eSetupVideoDevice3:
            menuItem=NewEditStraItem(_item, newSetup.IndexReference(_item));
            break;
        case eSetupVbr:
            menuItem=NewEditBoolItem(_item, newSetup.IndexReference(_item));
            break;
        default:
            cPvrUsb2Plugin::LogError("Invalid item index.");
            break;
    }
    if(menuItem) {
        Add(menuItem);
    }
    return menuItem;
}

cMenuEditStraItem* cRMSetupMenu::NewEditStraItem(eSetupItem _item,
        int* _index)
{
    *_index=newSetup.ClipIndex(_item, *_index);
    return new cMenuEditStraItem(tr(ItemTitle(_item)), _index,
            NumIndices(_item), Values(_item));
}

cMenuEditBoolItem* cRMSetupMenu::NewEditBoolItem(eSetupItem _item,
        int* _index)
{
    *_index=newSetup.ClipIndex(_item, *_index);
    return new cMenuEditBoolItem(tr(ItemTitle(_item)), _index);
}

const char* const* cRMSetupMenu::Values(eSetupItem _item)
{
    return cRMSetupTools::Values(newSetup.ItemType(_item));
}

int cRMSetupMenu::NumIndices(eSetupItem _item)
{
    return cRMSetupTools::NumIndices(newSetup.ItemType(_item));
}

void cRMSetupMenu::DisplayMenu() {
    cPvrUsb2Plugin::LogDebug("Display Setup.");

    int current=Current();
    Clear();

    SetHelp(NULL, NULL, NULL, tr("Channels"));

    CreateMenuItem(eSetupResolution);
    CreateMenuItem(eSetupVolume);
    CreateMenuItem(eSetupBrightness);
    CreateMenuItem(eSetupContrast);
    CreateMenuItem(eSetupHue);
    CreateMenuItem(eSetupSaturation);
    vbrItem=CreateMenuItem(eSetupVbr);
    CreateMenuItem(eSetupVideoBitrateAverage);
    if(newSetup.Value(eSetupVbr)) {
        CreateMenuItem(eSetupVideoBitratePeak);
    }
    CreateMenuItem(eSetupAudioBitrate);
    CreateMenuItem(eSetupVideoStandard);
    if(PvrUsb2->Device(0)) {
        CreateMenuItem(eSetupVideoDevice0);
    }
    if(PvrUsb2->Device(1)) {
        CreateMenuItem(eSetupVideoDevice1);
    }
    if(PvrUsb2->Device(2)) {
        CreateMenuItem(eSetupVideoDevice2);
    }
    if(PvrUsb2->Device(3)) {
        CreateMenuItem(eSetupVideoDevice3);
    }

    SetCurrent(Get(current));
    Display();
}

eOSState cRMSetupMenu::ProcessKey(eKeys _key)
{
    eOSState state=cMenuSetupPage::ProcessKey(_key);

    switch(state) {
        case osContinue:
            switch(_key) {
                case kLeft:
                case kRight:
                    if(Get(Current())==vbrItem) {
                        DisplayMenu();
                    }
                    break;
                default:
                    return state;
            }
            break;
        case osUnknown:
            switch(_key) {
                case kBlue:
                    return AddSubMenu(new cRMChannelMenu());
                default:
                    return state;
            }
            break;
        default:
            return state;
    }
    return osContinue;
}
