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

#include "common.h"

/* ----------- cRMMenuEditFrequencyItem ------------ */

class cRMMenuEditFrequencyItem:
    public cOsdItem
{
    private:
        char* title;
        cRMChannel* channel;
        int increment;

    protected:
        virtual void Set();

    public:
        cRMMenuEditFrequencyItem(const char* _title,
                cRMChannel* _channel, int _increment);
        virtual ~cRMMenuEditFrequencyItem();
       
        virtual eOSState ProcessKey(eKeys _key);       
};
    
cRMMenuEditFrequencyItem::cRMMenuEditFrequencyItem(const char* _title,
        cRMChannel* _channel, int _increment)
{
    title=strdup(_title);
    increment=_increment;
    channel=_channel;

    Set();
}

cRMMenuEditFrequencyItem::~cRMMenuEditFrequencyItem()
{
    free(title);
}

void cRMMenuEditFrequencyItem::Set(void)
{
    char tmp[255];
    snprintf(tmp, sizeof(tmp), "%s:\t%'.2f%+'.2f MHz",
            title,
            1.0*channel->Frequency()/1000,
            1.0*channel->Offset()/1000);
    SetText(tmp);
}

eOSState cRMMenuEditFrequencyItem::ProcessKey(eKeys _key)
{
    eOSState state=cOsdItem::ProcessKey(_key);
    
    if(state!=osUnknown) {
        return state;
    }
    switch(_key) {
        case kLeft:
            channel->SetOffset(channel->Offset()-increment);
            break;
        case kRight:
            channel->SetOffset(channel->Offset()+increment);
            break;
        default:
            return state;
    }
    Set();
    return osContinue;
}

/* ----------- cRMMenuEditChannelItem ------------ */

class cRMMenuEditChannelItem:
    public cOsdItem
{
    private:
        cRMChannel* channel;
        int increment;

    protected:
        virtual void Set();

    public:
        cRMMenuEditChannelItem(cRMChannel* _channel, int _increment);
       
        virtual eOSState ProcessKey(eKeys _key);       
};
    
cRMMenuEditChannelItem::cRMMenuEditChannelItem(cRMChannel* _channel,
        int _increment)
{
    increment=_increment;
    channel=_channel;

    Set();
}

void cRMMenuEditChannelItem::Set(void)
{
    char tmp[255];
    snprintf(tmp, sizeof(tmp), "%s\t%'.2f%+'.2f MHz",
            channel->Name(),
            1.0*channel->Frequency()/1000,
            1.0*channel->Offset()/1000);
    SetText(tmp);
}

eOSState cRMMenuEditChannelItem::ProcessKey(eKeys _key)
{
    eOSState state=cOsdItem::ProcessKey(_key);
    
    if(state!=osUnknown) {
        return state;
    }
    switch(_key) {
        case kLeft:
            channel->SetOffset(channel->Offset()-increment);
            break;
        case kRight:
            channel->SetOffset(channel->Offset()+increment);
            break;
        default:
            return state;
    }
    Set();
    return osContinue;
}

/* ----------- Factory ------------ */

cOsdItem* cRMMenuItems::Label(const char* _text)
{
    cOsdItem* item=new cOsdItem(_text);
    item->SetSelectable(false);
    return item;
}

cOsdItem* cRMMenuItems::Label(const char* _text, const char* _value)
{
    char tmp[255];
    snprintf(tmp, sizeof(tmp), "%s:\t%s", _text, _value);
    return Label(tmp);
}

cOsdItem* cRMMenuItems::Label(const char* _text, int _value)
{
    char tmp[255];
    snprintf(tmp, sizeof(tmp), "%s:\t%d", _text, _value);
    return Label(tmp);
}

cOsdItem* cRMMenuItems::ChannelItem(cRMChannel* _channel,
        int _increment)
{
    return new cRMMenuEditChannelItem(_channel, _increment);
}

cOsdItem* cRMMenuItems::FrequencyItem(const char* _title, cRMChannel* _channel,
        int _increment)
{
    return new cRMMenuEditFrequencyItem(_title, _channel, _increment);
}

    
