#include "remotepackconfig.h"
#include "globalinfo.h"
#include <pqQtlib/utils/pqfileutils.h>
#include <QJsonDocument>

RemotePackConfig::RemotePackConfig(QObject *parent) : QObject(parent)
{
    connect(&_request,&HttpRequest::result,this,&RemotePackConfig::result);
    connect(&_WareRequest,&HttpRequest::result,this,&RemotePackConfig::WareResult);
    parsefUNS.insert("A1000100",[this](const QJsonObject & obj){this->_config.jitai = this->getConfigStringList(obj);});

    parsefUNS.insert("A1000101",[this](const QJsonObject & obj){this->_config.dangchegongbanci = this->getConfigStringList(obj);});
    parsefUNS.insert("A1000102",[this](const QJsonObject & obj){this->_config.daBaoGongBanCi = this->getConfigStringList(obj);});
    parsefUNS.insert("A1000103",[this](const QJsonObject & obj){this->_config.xiangzhong = this->getConfigStringDouble(obj);});
    parsefUNS.insert("A1000104",[this](const QJsonObject & obj){this->_config.tongZhong = this->getConfigStringDouble(obj);});
    parsefUNS.insert("A1000105",[this](const QJsonObject & obj){this->_config.guanse = this->getConfigStringList(obj);});
    parsefUNS.insert("A1000106",[this](const QJsonObject & obj){this->_config.nianxiang = this->getConfigStringList(obj);});
    parsefUNS.insert("A1000107",[this](const QJsonObject & obj){this->_config.zhixiang = this->getConfigStringList(obj);});
    parsefUNS.insert("A1000108",[this](const QJsonObject & obj){this->_config.mashu = this->getConfigStringInt(obj);});
//   parsefUNS.insert("A1000109",[this](const QJsonObject & obj){this->_config.cheZhong = this->getConfigStringList(obj);});
//   parsefUNS.insert("A1000110",[this](const QJsonObject & obj){this->_config.gangHao = this->getConfigStringList(obj);});
   parsefUNS.insert("A1000111",[this](const QJsonObject & obj){this->_config.xiangHaoXianShiGuiZe = this->getConfigInt(obj);});
   parsefUNS.insert("A1000112",[this](const QJsonObject & obj){this->_config.daBaoRuku = this->getConfigBool(obj);});
   parsefUNS.insert("A1000113",[this](const QJsonObject & obj){this->_config.daBaoRiQiXianShiFangShi = this->getConfigInt(obj);});
   parsefUNS.insert("A1000114",[this](const QJsonObject & obj){this->_config.pinZhong = this->getConfigStringList(obj);});

   parsefUNS.insert("A2000200",[this](const QJsonObject & obj){this->_config.ziDongDushu = this->getConfigBool(obj);});
   parsefUNS.insert("A2000201",[this](const QJsonObject & obj){this->_config.buDingZhongDaBaoXianzhi = this->getConfigBool(obj);});
   parsefUNS.insert("A2000202",[this](const QJsonObject & obj){this->_config.dingZhongDaBaoXianZhiKaiQi = this->getConfigBool(obj);});
   parsefUNS.insert("A2000203",[this](const QJsonObject & obj){this->_config.jiTaiBangDingMaDan = this->getConfigBool(obj);});
   parsefUNS.insert("A2000204",[this](const QJsonObject & obj){this->_config.morenTongZhongGUDing = this->getConfigBool(obj);});
   parsefUNS.insert("A2000205",[this](const QJsonObject & obj){this->_config.morenXiandhiMaoZhong = this->getConfigBool(obj);});


   parsefUNS.insert("A2000206",[this](const QJsonObject & obj){this->_config.morenDaYinRuKuMadan = this->getConfigBool(obj);});
   parsefUNS.insert("A2000207",[this](const QJsonObject & obj){this->_config.morenBudingZhongMaoZhongXianZhi = this->getConfigBool(obj);});
   parsefUNS.insert("A2000208",[this](const QJsonObject & obj){this->_config.daBaoZhuangLeiXingGuiZeKaiQi = this->getConfigBool(obj);});
   parsefUNS.insert("A2000209",[this](const QJsonObject & obj){this->_config.daBaoZhuangMaoZhongXianZhiKaiQi = this->getConfigBool(obj);});
   parsefUNS.insert("A2000210",[this](const QJsonObject & obj){this->_config.ziDongDaBaoKaiQi = this->getConfigBool(obj);});
   parsefUNS.insert("A2000211",[this](const QJsonObject & obj){this->_config.pingBanZiDongTongBuShuJu = this->getConfigBool(obj);});
   parsefUNS.insert("A2000212",[this](const QJsonObject & obj){
       auto jingdu  = this->daBaoJingDu(obj);
       this->_config.chengZhongJIngDu = jingdu.first;
       this->_config.chengZhongJingDuMode = jingdu.second;
   });

   parsefUNS.insert("A2000213",[this](const QJsonObject & obj){this->_config.buDingZhongTongShuFanWei = this->getConfigpAIR(obj);});
   parsefUNS.insert("A2000214",[this](const QJsonObject & obj){this->_config.dingZhongTongShuFanwei = this->getConfigpAIR(obj);});
   parsefUNS.insert("A2000215",[this](const QJsonObject & obj){this->_config.morenRuKuMaDanFenShu = this->getConfigInt(obj);});
   parsefUNS.insert("A2000216",[this](const QJsonObject & obj){this->_config.buDingZhongXianZhiFanWei = this->getConfigpAIR(obj);});
   parsefUNS.insert("A2000217",[this](const QJsonObject & obj){this->_config.daBaoZhuangDaYinXiangDanFenShu = this->getConfigInt(obj);});
   parsefUNS.insert("A2000218",[this](const QJsonObject & obj){this->_config.daBaoZhuangMaoZhongFanWei = this->getConfigInt(obj);});
   parsefUNS.insert("A2000219",[this](const QJsonObject & obj){this->_config.ziDongDaBaoXiangZhongFanWei = this->getConfigInt(obj);});
   parsefUNS.insert("A2000220",[this](const QJsonObject & obj){this->_config.zidongDaBaoMaoZhongFanWei = this->getConfigInt(obj);});
   parsefUNS.insert("A3000300",[this](const QJsonObject & obj){this->_config.morenDaYinHuaMaDan = this->getConfigInt(obj);});
   parsefUNS.insert("A3000301",[this](const QJsonObject & obj){this->_config.morenXiaoShouXianShiJinE = this->getConfigInt(obj);});
   parsefUNS.insert("A3000302",[this](const QJsonObject & obj){this->_config.morenDayinXiaoShouMinXidan = this->getConfigInt(obj);});
   parsefUNS.insert("A3000303",[this](const QJsonObject & obj){this->_config.morenDaYinXIaoShouDan = this->getConfigInt(obj);});
   parsefUNS.insert("A3000304",[this](const QJsonObject & obj){this->_config.morenXianShiQianKuan = this->getConfigInt(obj);});
   parsefUNS.insert("A3000305",[this](const QJsonObject & obj){this->_config.xiaoShouEJingDu = this->getConfigInt(obj);});
//   parsefUNS.insert("A4000400",[this](const QJsonObject & obj){this->_config.diys = this->getDiys(obj);});

  //TODO:http://doc.vanlai.net:3001/web/#/1?page_id=57 补全剩下
//http://erp.itwsw.cn/#/packageCon

}

QList<DIYPackConfig> RemotePackConfig::getDiys(const QJsonObject & data)
{
    // TODO:
    QJsonArray value_array = data["value"].toArray();
    DIYPackConfig DTYConfig;
    QList<DIYPackConfig> DTYConfigList;
    for(int i = 0;i<value_array.size();i++){
        QJsonObject obj = value_array.at(i).toObject();
        DTYConfig.type = obj["packaging_type"].toInt();
        DTYConfig.danDianXiaXian = obj["danding_lower_limit"].toInt();
        DTYConfig.danDianShangXian = obj["danding_upper_limit"].toInt();
        DTYConfig.jingZhongXiaXian = obj["net_weight_lower_limit"].toInt();
        DTYConfig.jingZhongShangXian = obj["net_weight_upper_limit"].toInt();
        DTYConfigList.append(DTYConfig);
    }
    return DTYConfigList;
}

QList<POYPackConfig> RemotePackConfig::getPoys(const QJsonObject & data)
{
        // TODO:
    QJsonArray value_array = data["value"].toArray();
    POYPackConfig POYConfig;
    QList<POYPackConfig> POYConfigList;
    for(int i = 0;i<value_array.size();i++){
        QJsonObject obj = value_array.at(i).toObject();
        POYConfig.type = obj["packaging_type"].toInt();
        POYConfig.jingZhongXiaXian = obj["net_weight_lower_limit"].toInt();
        POYConfig.jingZhongShangXian = obj["net_weight_upper_limit"].toInt();
        POYConfigList.append(POYConfig);
    }
    return POYConfigList;
}

void RemotePackConfig::refConfig()
{
    getDdengji = false;
    GlobalInfo * glo = GlobalInfo::this_();
    if(glo->isNoLine()){
        auto dt = PQ::CacheFile::readFile("config.cache");
        auto obj = QJsonDocument::fromJson(dt).object();
        result(200,obj);
        getDdengji = true;
        dt = PQ::CacheFile::readFile("dengjiList.cache");
        obj = QJsonDocument::fromJson(dt).object();
        result(200,obj);
    }
//    _aid = QString::number(body["user_id"].toInt());
//    _token = body["access_token"].toString();
    _request.setUserToken(QString::number( glo->user().accountId) ,glo->user().acessToken);
    QString url = glo->config()->baseUrl+"/v1/configuration";
    qDebug() << url;
    _request.get(url);
}

void RemotePackConfig::result(int code, const QJsonObject & body)
{
    int ecode = body.value("code").toInt(404);
    if(ecode != 200){
        emit configUpdateError(ecode,body.value("message").toString(""));
        return;
    }
    if(!getDdengji){
        PQ::CacheFile::writeFile("config.cache",QJsonDocument(body).toJson());
        QJsonArray object = body.value("data").toArray();
        for(int i = 0; i < object.size(); ++ i){
            QJsonObject obj = object.at(i).toObject();
            QString key = obj.value("key").toString();
            auto handle = parsefUNS.value(key,[](const QJsonObject &){});
            handle(obj);
        }
        emit configUpdate(_config);
        if(!GlobalInfo::this_()->isNoLine()){refDengJiList();}

    } else {
        // TODO： 解析等级列表
        PQ::CacheFile::writeFile("dengjiList.cache",QJsonDocument(body).toJson());
        QJsonArray object = body.value("data").toArray();
        DengJiConfigItem dengJiConfig;
        for(int i = 0; i<object.size();i++){
            QJsonObject obj = object.at(i).toObject();
            dengJiConfig.id = obj.value("level_id").toInt();
            dengJiConfig.value = obj.value("name").toString();
            dengJiConfig.isEnable = obj.value("is_disable").toBool();
            dengji.append(dengJiConfig);
        }
        emit dengJIConfigUp(dengji);
        refWarehouse();
    }
}

void RemotePackConfig::refWarehouse()
{
    QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/warehouse/list?is_disable=0&page=0&limit=999";
    _WareRequest.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
    _WareRequest.get(url);
}

void RemotePackConfig::WareResult(int code,const QJsonObject & body){
    WarehouseConfig ware;
    if(code != 200){
        return;
    }
    QJsonArray warelist = body.value("data").toArray();
    for(int i = 0;i<warelist.size();i++){
        QJsonObject obj = warelist.at(i).toObject();
        ware.warehouse_id = obj.value("warehouse_id").toInt();
        ware.serial_number = obj.value("serial_number").toString();
        ware.name = obj.value("name").toString();
        ware.factory_id = obj.value("factory_id").toInt();
        ware.factory_name = obj.value("factory_name").toString();
        ware.remark = obj.value("remark").toString();
        ware.is_disable = obj.value("is_disable").toInt();
        ware.create_time = obj.value("create_time").toInt();
        ware.modified_time = obj.value("modified_time").toInt();
        Warehouse.append(ware);
    }
    emit wareConfigUp(Warehouse);
}

void RemotePackConfig::refDengJiList()
{
    getDdengji = true;
    // todo: http://doc.vanlai.net:3001/web/#/1?page_id=35
    QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/level/list";
    _request.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
    _request.get(url);

}

bool RemotePackConfig::getConfigBool(const QJsonObject & data)
{
    QJsonArray value_array = data.value("value").toArray();
    int vSize = value_array.size();
    QString select;
    for(int i = 0; i < vSize; ++i){
    QJsonValue value_content = value_array.at(i);
        if(value_content.isObject()){
            QJsonObject value_object = value_content.toObject();
            if(value_object.value("selected").toBool(false)){
                select = value_object.value("name").toString();
            }
        }
    }
//    select = select.trimmed();
    if(select.contains("是") || select.contains("yes",Qt::CaseInsensitive))
        return true;
    return false;
}

QStringList RemotePackConfig::getConfigStringList(const QJsonObject & data)
{
        QJsonArray value_array = data.value("value").toArray();
        QStringList list;
        int vSize = value_array.size();
        for(int i = 0; i < vSize; ++i){
        QJsonValue value_content = value_array.at(i);
        if(value_content.isObject()){
            QJsonObject value_object = value_content.toObject();
            list.append(value_object.value("value").toString());
        }
        }

        return list;
}

int RemotePackConfig::getConfigInt(const QJsonObject & data)
{
    QJsonArray value_array = data.value("value").toArray();
    int list;
    int vSize = value_array.size();
    for(int i = 0; i < vSize; ++i){
        QJsonValue value_content = value_array.at(i);
        if(value_content.isObject()){
            QJsonObject value_object = value_content.toObject();
            if(value_content.isObject()){
                QJsonObject value_object = value_content.toObject();
                if(value_object.value("selected").toBool(false)){
                    list = value_object.value("value").toInt();
                }
            }
        }
    }

    return list;
}


QPair<int,int> RemotePackConfig::getConfigpAIR(const QJsonObject & data)
{
    QJsonArray value_array = data.value("value").toArray();
    QPair<int,int> list;
    int vSize = value_array.size();
    bool frist = true;
    for(int i = 0; i < vSize; ++i){
        QJsonObject value_object = value_array.at(i).toObject();
//        if(value_object.value("selected").toBool(false)){
            if(frist) {
                list.first = value_object.value("value").toInt();
                frist = false;
            } else {
                list.second = value_object.value("value").toInt();
            }

//        }

    }

    return list;
}

QPair<int,int> RemotePackConfig::daBaoJingDu(const QJsonObject & data)
{
    QJsonArray value_array = data.value("value").toArray();
    QPair<int,int> list;
    int vSize = value_array.size();
    bool frist = true;
    for(int i = 0; i < vSize; ++i){
    QJsonValue value_content = value_array.at(i);
    auto ary = value_content.toArray();
        for(int j = 0; j < ary.size(); ++j){
            QJsonObject value_object = ary.at(j).toObject();
            if(value_object.value("selected").toBool(false)){
                if(frist) {
                    list.first = value_object.value("value").toInt();
                    frist = false;
                } else {
                    list.second = value_object.value("value").toInt();
                }

        }

    }
    }

    return list;
}

QList<double> RemotePackConfig::getConfigStringDouble(const QJsonObject & data)
{
        QJsonArray value_array = data.value("value").toArray();
        QList<double>  list;
        int vSize = value_array.size();
        for(int i = 0; i < vSize; ++i){
        QJsonValue value_content = value_array.at(i);
        if(value_content.isObject()){
            QJsonObject value_object = value_content.toObject();
            list.append(value_object.value("value").toDouble());
        }
        }


        return list;
}

QList<int> RemotePackConfig::getConfigStringInt(const QJsonObject & data)
{
        QJsonArray value_array = data.value("value").toArray();
        QList<int>  list;
        int vSize = value_array.size();
        for(int i = 0; i < vSize; ++i){
        QJsonValue value_content = value_array.at(i);
        if(value_content.isObject()){
            QJsonObject value_object = value_content.toObject();
            list.append(value_object.value("value").toInt());
        }
        }


        return list;
}
