#include "InteractiveWork.h"

#include "RunAbstract.h"
#include "RunDataCacheRefresh.h"
#include "RunMaterialInfoListRefresh.h"
#include "RunMaterialReplaceRefreshListModel.h"
#include "RunMaterialManualReplaceRefreshListModel.h"
#include "RunMaterialManualReplaceSubmit.h"
#include "RunMaterialExceptionRefreshListModel.h"

#include "RunHttpGet_exception_type.h"
#include "RunHttpGet_severity_level.h"
#include "RunHttpGet_getInfo.h"
#include "RunHttpGet_getCabinetHomePage.h"
#include "RunHttpGet_getInstructionsList.h"

#include "RunHttpGet_getIsMaterialsLoanExceptionPage.h"
#include "RunHttpGet_getSysUserCharacteristicPage.h"
#include "RunHttpGet_getIsMaterialsPage.h"
#include "RunHttpGet_getLastCheckPlanByCabinetCode.h"
#include "RunHttpGet_selectIsMaterialsCabinetByCode.h"

#include "RunHttpPost_startCheckPlan.h"
#include "RunHttpPost_deleteSysUserCharacteristicByRecordIds.h"
#include "RunHttpPost_insertCheckRecord.h"
#include "RunHttpPost_insertSubmitPlan.h"
#include "RunHttpPost_insertUserFace.h"
#include "RunHttpPost_insertUserFingerprintDat.h"
#include "RunHttpPost_loginByFace.h"
#include "RunHttpPost_loginByFingerprintDat.h"
#include "RunHttpPost_insertIsException.h"

#include "RunViewWait.h"
#include "RunViewRefresh_AbnormalReport.h"

InteractiveWork::InteractiveWork(QObject *parent)
    : QThread(parent)
{
    m_bThread = true;
}

InteractiveWork::~InteractiveWork()
{
    m_bThread = false;
}

void InteractiveWork::appendTask(InteractiveTask *task)
{
    QMutexLocker locker(&m_mutex);
    task->stat = InteractiveTask::StatWaitStart;
    m_listTask.append(task);
}

void InteractiveWork::run()
{
    while (m_bThread)
    {
        QThread::msleep(10);

        QMutexLocker locker(&m_mutex);
        for (auto it = m_listTask.begin(); it != m_listTask.end();)
        {

            InteractiveTask *task = *it;
            // 任务等待开始
            if (task->stat == InteractiveTask::StatWaitStart)
            {
                RunAbstract *run = nullptr;

                switch(task->type)
                {
                case InteractiveTask::TypeDataCacheRefresh:
                    run = new RunDataCacheRefresh(task, &m_mutex);
                    break;

                case InteractiveTask::TypeMaterialInfoListRefresh:
                    run = new RunMaterialInfoListRefresh(task, &m_mutex);
                    break;

                case InteractiveTask::TypeMaterialReplaceRefreshListModel:
                    run = new RunMaterialReplaceRefreshListModel(task, &m_mutex);
                    break;

                case InteractiveTask::TypeMaterialManualReplaceRefreshListModel:
                    run = new RunMaterialManualReplaceRefreshListModel(task, &m_mutex);
                    break;

                case InteractiveTask::TypeMaterialManualReplaceSubmit:
                    run = new RunMaterialManualReplaceSubmit(task, &m_mutex);
                    break;

                // 刷新异常列表
                case InteractiveTask::TypeMaterialExceptionRefreshListModel:
                    run = new RunMaterialExceptionRefreshListModel(task, &m_mutex);
                    break;


                    // 获取异常类型字典值
                case InteractiveTask::TypeHttpGet_exception_type:
                    run = new RunHttpGet_exception_type(task, &m_mutex);
                    break;

                    // 获取严重等级字典值
                case InteractiveTask::TypeHttpGet_severity_level:
                    run = new RunHttpGet_severity_level(task, &m_mutex);
                    break;

                    // 获取用户信息
                case InteractiveTask::TypeHttpGet_getInfo:
                    run = new RunHttpGet_getInfo(task, &m_mutex);
                    break;

                    // 获取主界面信息
                case InteractiveTask::TypeHttpGet_getCabinetHomePage:
                    run = new RunHttpGet_getCabinetHomePage(task, &m_mutex);
                    break;

                    // 获取用户使用说明
                case InteractiveTask::TypeHttpGet_getInstructionsList:
                    run = new RunHttpGet_getInstructionsList(task, &m_mutex);
                    break;

                    // 获取异常信息
                case InteractiveTask::TypeHttpGet_getIsMaterialsLoanExceptionPage:
                    run = new RunHttpGet_getIsMaterialsLoanExceptionPage(task, &m_mutex);
                    break;

                    // 查询用户特征
                case InteractiveTask::TypeHttpGet_getSysUserCharacteristicPage:
                    run = new RunHttpGet_getSysUserCharacteristicPage(task, &m_mutex);
                    break;

                    // 查询当前机柜物资检查计划
                case InteractiveTask::TypeHttpGet_getLastCheckPlanByCabinetCode:
                    run = new RunHttpGet_getLastCheckPlanByCabinetCode(task, &m_mutex);
                    break;

                    // 查询物资信息
                case InteractiveTask::TypeHttpGet_getIsMaterialsPage:
                    run = new RunHttpGet_getIsMaterialsPage(task, &m_mutex);
                    break;

                // 获取物资柜ID
                case InteractiveTask::TypeHttpGet_selectIsMaterialsCabinetByCode:
                    run = new RunHttpGet_selectIsMaterialsCabinetByCode(task, &m_mutex);
                    break;


                    // 开始物资检查计划
                case InteractiveTask::TypeHttpPost_startCheckPlan:
                    run = new RunHttpPost_startCheckPlan(task, &m_mutex);
                    break;


                    // 物资检查计划保存
                case InteractiveTask::TypeHttpPost_insertCheckRecord:
                    run = new RunHttpPost_insertCheckRecord(task, &m_mutex);
                    break;

                    // 提交最终检查结果
                case InteractiveTask::TypeHttpPost_insertSubmitPlan:
                    run = new RunHttpPost_insertSubmitPlan(task, &m_mutex);
                    break;

                    // 删除人员特征
                case InteractiveTask::TypeHttpPost_deleteSysUserCharacteristicByRecordIds:
                    run = new RunHttpPost_deleteSysUserCharacteristicByRecordIds(task, &m_mutex);
                    break;

                    // 新增人脸记录
                case InteractiveTask::TypeHttpPost_insertUserFace:
                    run = new RunHttpPost_insertUserFace(task, &m_mutex);
                    break;

                    // 新增指纹记录
                case InteractiveTask::TypeHttpPost_insertUserFingerprintDat:
                    run = new RunHttpPost_insertUserFingerprintDat(task, &m_mutex);
                    break;

                    // 人脸登录
                case InteractiveTask::TypeHttpPost_loginByFace:
                    run = new RunHttpPost_loginByFace(task, &m_mutex);
                    break;

                    // 指纹登录
                case InteractiveTask::TypeHttpPost_loginByFingerprintDat:
                    run = new RunHttpPost_loginByFingerprintDat(task, &m_mutex);
                    break;

                    // 异常上报
                case InteractiveTask::TypeHttpPost_insertIsException:
                    run = new RunHttpPost_insertIsException(task, &m_mutex);
                    break;


                    // 视图等待
                case InteractiveTask::TypeViewWait:
                    run = new RunViewWait(task, &m_mutex);
                    break;

                    // 视图刷新-异常上报
                case InteractiveTask::TypeViewRefresh_AbnormalReport:
                    run = new RunViewRefresh_AbnormalReport(task, &m_mutex);
                    break;



                    // 丢弃无效任务
                default:
                    task->stat = InteractiveTask::StatFailStart;
                    break;
                }

                if (run != nullptr)
                {
                    task->stat = InteractiveTask::StatExecAfoot;
                    QThreadPool::globalInstance()->start(run);
                }
            }
            // 任务正在执行
            else if (task->stat == InteractiveTask::StatExecAfoot)
            {
                // 执行中...
            }
            // 任务执行完成
            else if (task->stat == InteractiveTask::StatCompleted)
            {
                emit signal_completed(task);
                it = m_listTask.erase(it);
                continue;
            }
            // 任务执行失败
            else if (task->stat == InteractiveTask::StatFailStart)
            {
                emit signal_failstart(task);
                it = m_listTask.erase(it);
                continue;
            }

            ++it;
        }

    }
}
