/**
  ******************************************************************************
  * @file           : TimeWorker.cpp
  * @author         : xin
  * @brief          : None
  * @attention      : None
  * @date           : 2023/4/11
  ******************************************************************************
  */

//
// Created by xin on 2023/4/11.
//

#include "TimeWorker.h"
#include "logout.h"
#include "fstream"
#include "CommanSetting.h"
TimeWorker::TimeWorker() {
    m_Timer=new QTimer();
    m_StartTime=QTime(8,0,0);
    m_EndTime=QTime(20,0,0);
    connect(m_Timer,SIGNAL(timeout()),this,SLOT(onTimerTrigger()));



}

void TimeWorker::SetTask(TaskToExec task) {
    //m_Task=task;
    m_TaskList.append(task);
    isinit=true;

}

void TimeWorker::SetIntervalTime(long time) {
    if (time>0)
        m_IntervalTime=time;
    else
        m_IntervalTime=60000;



}

void TimeWorker::Start() {

    m_Timer->setInterval(m_IntervalTime);
    m_Timer->start();
    onTimerTrigger();
}

void TimeWorker::Stop() {

    m_Timer->stop();
}

void TimeWorker::onTimerTrigger() {

    if (isinit)
    {
        QTime now=QTime::currentTime();

        if (now<m_StartTime||now>m_EndTime)
        {

            logout("TimeWorker",ERRORSTR+"Time is not in work time"+COLORRESET,6);
            logout("TimeWorker",ERRORSTR+"now is"+now.toString("hh:mm:ss")+ " StartTime is"+m_StartTime.toString("hh:mm:ss")+" EndTime is"+m_EndTime.toString("hh:mm:ss")+COLORRESET);
            return;
        }
        if (m_isWork)
        {
            logout("TimeWorker","Task is working please waite",6);
            return;
        }
        m_isWork=true;
       // logout("TimeWorker","Task is working");
        int taskid=0;
        foreach (auto task, m_TaskList) {
            task();
                //logout("TimeWorker","Task "+QString::number(taskid)+" is finished",6);
                taskid++;
        }

       // logout("TimeWorker","Task is finished");
        m_isWork=false;
    }
    else
    {
        logout("TimeWorker","Task is not init",6);
    }

}

void TimeWorker::SetWorkTime(QTime start, QTime end) {
    m_StartTime=start;
    m_EndTime=end;


}

void TimeWorker::SetWorkTime(QString start, QString end) {
    try
    {
        m_StartTime=QTime::fromString(start,"hh:mm:ss");
        m_EndTime=QTime::fromString(end,"hh:mm:ss");
    }
    catch (...)
    {
        logout("TimeWorker","SetWorkTime error",6);
    }



}

void TimeWorker::ReadjsonConfig(QString path) {
try
    {
        json j;
        std::ifstream i(path.toStdString());
        i >> j;
        if (j.contains("StartTime")&&j["StartTime"].is_string())
            m_StartTime=QTime::fromString(QString::fromStdString(j["StartTime"]),"hh:mm:ss");
        if(j.contains("EndTime")&&j["EndTime"].is_string())
            m_EndTime=QTime::fromString(QString::fromStdString(j["EndTime"]),"hh:mm:ss");
        if (j.contains("IntervalTime")&&j["IntervalTime"].is_number())
            SetIntervalTime(j["IntervalTime"].get<int>()*1000);
        logout("TimeWorker","ReadjsonConfig success: StartTime:"+m_StartTime.toString("hh:mm:ss")+" EndTime:"+m_EndTime.toString("hh:mm:ss")+" IntervalTime:"+QString::number(m_IntervalTime)+"ms",6);

    }
    catch (...)
    {
        logout("TimeWorker","ReadjsonConfig error",6);
    }

}

void TimeWorker::ReadConfig(json j) {
    if (j.contains("StartTime")&&j["StartTime"].is_string())
        m_StartTime=QTime::fromString(QString::fromStdString(j["StartTime"]),"hh:mm:ss");
    if(j.contains("EndTime")&&j["EndTime"].is_string())
        m_EndTime=QTime::fromString(QString::fromStdString(j["EndTime"]),"hh:mm:ss");
    if (j.contains("IntervalTime")&&j["IntervalTime"].is_number())
        SetIntervalTime(j["IntervalTime"].get<int>()*1000);
    logout("TimeWorker","ReadjsonConfig success: StartTime:"+m_StartTime.toString("hh:mm:ss")+" EndTime:"+m_EndTime.toString("hh:mm:ss")+" IntervalTime:"+QString::number(m_IntervalTime)+"ms",6);
}
