/*
 * mainmenu.c
 */

#include "mainmenu.h"
#include "browse.h"
#include "dispatcher.h"

#include <sstream>


using namespace std;

/* --- cMainmenu ------------------------------------------------------------ */

cMainmenu::cMainmenu(cDispatcher *_dispatcher)
:cOsdMenu("Audiorecorder, Mainmenu")
{
        dispatcher = _dispatcher;
        active = dispatcher->is_active();
        
        Clear();
        
        Add(new cOsdItem("Browse tracks", osUser1));
        Add(new cOsdItem(active ? "Stop receiving" : "Start receiving",
                        osUser2));
        Add(new cOsdItem("Start rebuilding cache", osUser3));
        
        Display();
}


eOSState cMainmenu::ProcessKey(eKeys key)
{
        eOSState state = cOsdMenu::ProcessKey(key);
        
        if (HasSubMenu())
                return osContinue;
        
        switch (state) {
        case osBack:
                state = osEnd;
                break;
        case osUser1:
                if (Cache.is_rebuilding()) {
                        Skins.Message(mtStatus, "Rebuilding cache, try later");
                        break;
                }
                
                AddSubMenu(new cBrowse());
                
                break;
        case osUser2:
                if (active) {
                        Add(new cOsdItem("Start receiving", osUser2), false,
                                Get(Current()));
                        dispatcher->stop();
                        active = false;
                }
                else {
                        Add(new cOsdItem("Stop receiving", osUser2), false,
                                Get(Current()));                        
                        dispatcher->start();
                        active = true;
                }
                Del(Current());
                Display();
                
                break;
        case osUser3:
                Cache.rebuild();
                Skins.Message(mtStatus, "Rebuilding cache");
                
                break;
        default:
                break;
        }
        
        return state;
}
