#include "tank.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QByteArray>


//油气回收系统 外观检查项
static QString vrs_visual_inspection_items[VRS_VISUAL_INSPECTION_ITEMS_NUM] ={
        "油气回收耦合阀",
        "通气阀",
        "油气回收阀",
        "密封盖",
        "气动联锁阀",
        "连接管路",
        "控制管路",
        "多仓油气管路并联"
};


//底部装油系统 外观检查项
static QString bls_visual_inspection_items[BLS_VISUAL_INSPECTION_ITEMS_NUM] ={
        "卸油阀",
        "呼吸阀",
        "紧急切断阀",
        "连接管线",
        "防溢流系统"
};



Compartment::Compartment()
{
    m_result.delta_sys_pressure= 0.0;
    m_result.pass_sys_pressure = false;
    m_result.delta_valve_pressure = 0.0;
    m_result.pass_valve_pressure = false;
    m_testfinished=false;

}
Compartment::~Compartment()
{

}

int Compartment::get_addpressure_timeout()
{
    int timeout = 5*60;

    if(m_volume > 10000){
        timeout = 15*60;
    }else if(m_volume > 5000){
        timeout = 10*60;
    }else if(m_volume > 1000){
        timeout = 5*60;
    }

    return timeout;
}

int Compartment::get_adjust_timeout()
{
    int timeout = 0;

    if(m_volume > 9000){
        timeout = 5*60;
    }else if(m_volume > 7000){
        timeout = 5*60;
    }else if(m_volume > 5000){
        timeout = 5*60;
    }else if(m_volume > 3000){
        timeout = 5*60;
    }else if(m_volume > 1000){
        timeout = 5*60;
    }else{
        timeout = 5*60;
    }

    timeout = 15*60;

    return timeout;
}

void Compartment::clear_result()
{
    m_result.pass_sys_pressure = false;
    m_result.pass_valve_pressure = false;
    m_result.sys_test_finished = false;
    m_result.valve_test_finished = false;
    m_testfinished = false;
}

LeakageValue::LeakageValue()
{
    couplevalve = -100.0;
    manhole = -100.0;
}

LeakageValue::~LeakageValue()
{

}


Tanker::Tanker()
{
    for(int i =0; i< VRS_VISUAL_INSPECTION_ITEMS_NUM; i++){
        vrs_results[i] = RESULT_QUALIFIED;
    }

    for(int i=0; i< BLS_VISUAL_INSPECTION_ITEMS_NUM; i++){
        bls_results[i] = RESULT_QUALIFIED;
    }



}

Tanker::~Tanker()
{

}

Compartment& Tanker::get_compartment(int id)
{

   return m_comparts[id];
}

QJsonObject Tanker::get_vrs_items()
{
    QJsonObject jsonObj;
    jsonObj.insert("count", VRS_VISUAL_INSPECTION_ITEMS_NUM);

    QJsonArray itemsArray;

    for(int i=0; i < VRS_VISUAL_INSPECTION_ITEMS_NUM; i++){

        QJsonObject itemObj;
        //itemObj.insert("index", i);
        itemObj.insert("name", vrs_visual_inspection_items[i]);
        itemObj.insert("value", QString::number(vrs_results[i]));

        itemsArray.append(itemObj);
    }

    jsonObj.insert("item", itemsArray);

    return jsonObj;
}

QJsonObject Tanker::get_bls_items()
{
    QJsonObject jsonObj;
    jsonObj.insert("count", BLS_VISUAL_INSPECTION_ITEMS_NUM);

    QJsonArray itemsArray;

    for(int i=0; i < BLS_VISUAL_INSPECTION_ITEMS_NUM; i++){

        QJsonObject itemObj;
        //jsonObj.insert("index", i);
        itemObj.insert("name", bls_visual_inspection_items[i]);
        itemObj.insert("value", QString::number(bls_results[i]));

        itemsArray.append(itemObj);
    }

    jsonObj.insert("item", itemsArray);

    return jsonObj;
}

bool Tanker::set_item_result(const QString& jsonStr )
{
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonStr.toUtf8());
    QJsonObject jsonObj = jsonDoc.object();

    QString table_str;
    QString item_name_str;
    int value = RESULT_QUALIFIED;

    if(jsonObj.contains("table") && jsonObj.value("table").isString()){
        table_str = jsonObj.value("table").toString();
    }else{
        qDebug() << "set_item_result no table";
        return false;
    }

    if(jsonObj.contains("name") && jsonObj.value("name").isString()){
        item_name_str = jsonObj.value("name").toString();
    }else{
        qDebug() << "set_item_result no name";
        return false;
    }

    if(jsonObj.contains("value") && jsonObj.value("value").isString()){
        value = jsonObj.value("value").toString().toInt();
    }else{
        qDebug() << "set_item_result no value";
        return false;
    }

    qDebug() << "table:" << table_str;

    if(table_str == "vrs"){
        for(int i=0; i<VRS_VISUAL_INSPECTION_ITEMS_NUM; i++){
           if(item_name_str == vrs_visual_inspection_items[i]){
               if(value >= RESULT_QUALIFIED && value <= RESULT_NOITEM){
                   vrs_results[i] = value;
                   qDebug() << "item_name:" << item_name_str;
                   qDebug() << "value:" << value;
               }
           }
        }

    }else if(table_str == "bls"){
        for(int i=0; i<BLS_VISUAL_INSPECTION_ITEMS_NUM; i++){
           if(item_name_str == bls_visual_inspection_items[i]){
               if(value >= RESULT_QUALIFIED && value <= RESULT_NOITEM){
                   bls_results[i] = value;
                   qDebug() << "item_name:" << item_name_str;
                   qDebug() << "value:" << value;
               }
           }
        }
    }else{
        qDebug() << "set_item_result table_str invalid";
        return false;
    }


    return true;
}

bool Tanker::set_leakage_value(const QString& jsonStr)
{
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonStr.toUtf8());
    QJsonObject jsonObj = jsonDoc.object();

    LeakageValue lv;

    if(jsonObj.contains("couplevalve") && jsonObj.value("couplevalve").isDouble()){
        lv.couplevalve = jsonObj.value("couplevalve").toDouble();
    }else{
        qDebug() << "set_leakage_value no couplevalve";
        return false;
    }

    if(jsonObj.contains("manhole") && jsonObj.value("manhole").isDouble()){
        lv.manhole = jsonObj.value("manhole").toDouble();
    }else{
        qDebug() << "set_leakage_value no manhole";
        return false;
    }

    if(lv.couplevalve < 0 || lv.manhole < 0){
        return false;
    }

    m_leakagevalue = lv;


    return true;
}



