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

#include "common.h"

cRMTuneMenu::cRMTuneMenu(cRMChannelMenu* _parent, cRMChannel* _channel):
    cOsdMenu(tr("Fine Tune"), 33)
{
    channel=_channel;
    parent=_parent;

    oldBG=Skins.Current()->Theme()->Color(0);
    Skins.Current()->Theme()->AddColor("clrBackground", 0x00000000);

    DisplayMenu();
    SetCurrent(Get(1));
}

cRMTuneMenu::~cRMTuneMenu()
{
    Skins.Current()->Theme()->AddColor("clrBackground", oldBG);
}

void cRMTuneMenu::DisplayMenu() {
    cPvrUsb2Plugin::LogDebug("Display Tune Menu.");
    
    Clear();

    if(channel) {
        Add(new cMenuEditStrItem(tr("Channel name"),
                channel->NameReference(),
                sizeof(tEditableTextBuffer), tr(FileNameChars)));
        Add(cRMMenuItems::FrequencyItem(tr("Frequency"), channel));
        if(channel->StartFrequency()!=channel->Frequency()) {
            Add(cRMMenuItems::Label(tr("Start frequency"),
                    channel->StartFrequency()));
        }
        if(channel->EndFrequency()!=channel->Frequency()) {
            Add(cRMMenuItems::Label(tr("End frequency"),
                    channel->EndFrequency()));
        }
        if(channel->VdrChannelNumber()>=0) {
            Add(cRMMenuItems::Label(tr("VDR channel number"),
                    channel->VdrChannelNumber()));
        }
    }

    Display();
}

eOSState cRMTuneMenu::ProcessKey(eKeys _key)
{
    eOSState state=cOsdMenu::ProcessKey(_key);
    switch(state) {
        case osContinue:
            switch(_key) {
                case kLeft:
                case kRight: {
                    if(parent) {
                        parent->Tune2CurrentChannel();
                    }
                    break;
                }
                default:
                    return state;
            }
            break;
        case osUnknown:
            switch(_key) {
                case kNone: 
                    SetStatus(NULL);
                    break;
                case kOk: {
                    if(!channel->IsModified()) {
                        return osBack;
                    }
                    channel->Write2VdrChannel();
                    if(channel->HaveVdrChannel()) {
                        Interface->Confirm(tr("Channel updated"), 2);
                    }
                    return osBack;
                }
                default:
                    break;
            }
            break;
        default:
            return state;
    }
    return osContinue;
}
