#include "timingoftrainee.h"

TimingOfTrainee::TimingOfTrainee(int trainee_id):
    timing_id(0),
    trainee_id(trainee_id)
{
    //QDateTime dataTimeCurr = QDateTime::currentDateTime();
    dataTimeNull = QDateTime::fromString("2000-01-01 00:00:00.000", "yyyy-MM-dd hh:mm:ss.zzz");
    setOperatingTime(QTime::fromString("00:00:00"));
    setEntryTime(dataTimeNull);
    setExitTime(dataTimeNull);
}


void TimingOfTrainee::setEntryTimeS(QString entry_time)
{
    entryTime_str = entry_time;
    entryTime = QDateTime::fromString(entry_time);
}

void TimingOfTrainee::setEntryTime(QDateTime entry_time)
{
    entryTime = entry_time;
    entryTime_str = entry_time.toString("yyyy-MM-dd hh:mm:ss.zzz");
}


void TimingOfTrainee::setExitTimeS(QString exit_time)
{
    exitTime_str = exit_time;
    exitTime = QDateTime::fromString(exit_time);
}

void TimingOfTrainee::setExitTime(QDateTime exit_time)
{
    exitTime = exit_time;
    exitTime_str = exit_time.toString("yyyy-MM-dd hh:mm:ss.zzz");
}


void TimingOfTrainee::setOperatingTimeS(QString operating_time)
{
    operatingTime_str = operating_time;
    operatingTime = QTime::fromString(operating_time);
}

void TimingOfTrainee::setOperatingTime(QTime operating_time)
{
    operatingTime = operating_time;
    operatingTime_str = operating_time.toString();
}

void TimingOfTrainee::fixEntry()
{
    QDateTime dataTimeCurr = QDateTime::currentDateTime();
    setEntryTime(dataTimeCurr);
}

void TimingOfTrainee::fixExit()
{
    QDateTime dataTimeCurr = QDateTime::currentDateTime();
    setExitTime(dataTimeCurr);
    if(entryTime != dataTimeNull)
        calculateOperatingTime();
}

void TimingOfTrainee::calculateOperatingTime()
{
    int cntSec = entryTime.secsTo(exitTime);
    setOperatingTime(getOperatingTime().addSecs(cntSec));
}
