PouringWindow::PouringWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::PouringWindow)
{
    ui->setupUi(this);
    setStyleSheet(App::CssStyle);

    QObject::connect(FlowMeterManager::Instance, SIGNAL(PourStarted()), this, SLOT(onPourStarted()));
    QObject::connect(FlowMeterManager::Instance, SIGNAL(PourFinished()), this, SLOT(onPourFinished()));
    QObject::connect(FlowMeterManager::Instance, SIGNAL(FlowMeterTicked()), this, SLOT(onFlowMeterTick()));

    connect(&updateTimer, SIGNAL(timeout()), this, SLOT(updateUI()));
    connect(this, SIGNAL(updateCamImageSignal()), this, SLOT(updateCamImageSlot()));

    ticksPerLiter = Settings::GetDouble("ticksPerLiter");
    logPours = Settings::GetBool("logPours");
    photoFrequency = Settings::GetInt("pourPhotoFrequency");

    currentUser = User::UnknownUser;
    currentPour = NULL;

    ui->usersComboBox->view()->setItemDelegate(new CustomComboBoxItem(this));
    vector<User*>::iterator iter;
    for (iter = User::UsersList.begin(); iter != User::UsersList.end(); ++iter)
    {
        User* user = (*iter);
        ui->usersComboBox->addItem(QString(user->Name.c_str()));
    }
    connect(ui->usersComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(userSelectedSlot(int)));
}
void FlowMeterManager::onFlowMeterData()
{
    QString out = QString(gpioProc->readAllStandardOutput());

    double now = time(NULL);
    // If it's been a couple seconds since the last tick, and we're not pouring, reset ticks
    if (IsPouring == false && now > LastTickAt)
        Ticks = 0;

    // Count the tick
    Ticks++;
    LastTickAt = now;

    // If we're still under the threshold, bail
    if (Ticks < tickThreshold)
        return;

    BeerTapSide tapSide = RIGHT_TAP;
    if (out.startsWith(leftTickString))
        tapSide = LEFT_TAP;
    if (out.startsWith(centerTickString))
        tapSide = CENTER_TAP;

    // If we just crossed the threshold, lets start the pour
    if (Ticks == tickThreshold)
    {
        beginPour(tapSide);
        return;
    }

    // Check for the other beer
    if (tapSide != CurrentTapSide)
        ConflictingTicks = true;

    emit FlowMeterTicked();
}