#include "bttv.hpp"

#include "audio_s.hpp"

using std::string;

Bttv* Bttv::instance = 0;

Bttv* Bttv::get_instance() 
{
  if (instance == 0) {
    instance =  new Bttv;
  }
  return instance;
}

Bttv::Bttv() : playing(false), _mute(false), paused(false) 
{
  conf = Config::get_instance();
}

void Bttv::volup()
{
  system((conf->p_bttvvolctl_path() + ' ' + 
	  conf->p_bttvvolupopt()).c_str());
}
 
void Bttv::voldown()
{
  system((conf->p_bttvvolctl_path() + ' ' + 
	  conf->p_bttvvoldownopt()).c_str());
}
 
void Bttv::mute()
{
  if (playing) {    
    if (_mute) {
      play(freq);
      _mute = false;
    } else {
      stop();
      _mute = true;
      playing = true;
    }
  }  
}   
    
void Bttv::stop()
{
  Audio_s *audio_state = Audio_s::get_instance();

  audio_state->set_playing(false);

  system((conf->p_bttvradio_path() + ' ' + 
	  conf->p_bttvradiooptsmute()).c_str());

  playing = false;
}
 
void Bttv::play(const string& frequency)
{
  freq = frequency;
  system((conf->p_bttvradio_path() + ' ' + 
	  conf->p_bttvradiooptsplay() + ' ' + freq).c_str());
  
  playing = true;
}

void Bttv::pause()
{
  if (paused) {
    play(freq);
    paused = false;
  } else {
    stop();
    paused = true;
  }
}
