#ifndef RSPFACTORY
#define RSPFACTORY

#include "global.h"
#include "fmtool.h"
#include "QsLog.h"
#include "fmp_settings.h"
#include "tools.h"
#include "fmglobal.h"

#include <QFile>
#include <QDir>
#include <QJsonObject>
#include <QApplication>

class RspFactory
{
public:

    static bool GetResponseJson(ReqType type, QJsonObject &response, const QJsonObject &json)
    {
        if(json.isEmpty())
            return false;
        if(type == SIGN)
            return GetSignResponse(response, json);
        if(type == PAY)
            return GetPayResponse(response, json);
        if(type == REFUND)
            return GetRefundResponse(response, json);
        if(type == ENDDAY)
            return GetEndDayResponse(response, json);
        if(type == FINDS)
            return GetFindResponse(response, json);
        if(type == AGAIN_PRINT)
            return GetAgainPrintResponse(response, json);
        return false;
    }

#ifndef FM_TEST
private:
#endif

    static bool GetSignResponse(QJsonObject &response, const QJsonObject &json)
    {
        response = json;
        return true;
    }

    static bool GetPayResponse(QJsonObject &response, const QJsonObject &json)
    {
        response = json;
        return true;
    }

    static bool GetRefundResponse(QJsonObject &response, const QJsonObject &json)
    {
        response = json;
        return true;
    }


    static bool GetEndDayResponse(QJsonObject &response, const QJsonObject &json)
    {
        response = json;

        if(json[JSON_KEY_STATUSCODE].toInt() == 100)
            return SetPinter(response, json, CONFIG_PRINT_ENDDAY_NAME);

        return true;
    }


    static bool GetAgainPrintResponse(QJsonObject &response, const QJsonObject &json)
    {
        response = json;

        if(json[JSON_KEY_STATUSCODE].toInt() == 100)
            return SetPinter(response, json, CONFIG_PRINT_AGAIN_NAME);

        return true;
    }


    static bool GetFindResponse(QJsonObject &response, const QJsonObject &json)
    {
        response = json;
        return true;
    }

    static bool SetPinter(QJsonObject &response, const QJsonObject &json, QString configKey=CONFIG_PRINT_ENDDAY_NAME)
    {
        if(!json.contains(JSON_KEY_EXT) || !json[JSON_KEY_EXT].isObject())
            return false;

        QJsonObject obj = json[JSON_KEY_EXT].toObject();
        QString str = obj[JSON_KEY_PRINT].toString();

        QString path;
        ToolS::GetPath(path);
        FMPSettings setting;
        path = setting.GetValue(CONFIG_PRINT_PATH, path).toString();

        QDir dir(path);
        if(!dir.exists()) {
            bool isOk = dir.mkpath(path);
            QLOG_INFO() << "Make path: " << path << (isOk ? " Success." : " Fail.");
        }

        QString printFileName = setting.GetValue(configKey, PRINT_FILE_NAME).toString();

        QString filePath = dir.absolutePath();
        if(!filePath.endsWith('/') && !filePath.endsWith('\\')) {
            filePath.append('/');
        }
        filePath.append(printFileName);
        response[JSON_KEY_PRINTPATH] = filePath;
        QFile file(filePath);
        file.open(QFile::WriteOnly);

        if(!file.isOpen())
            return false;

        char buf[MAX_PRINT_LEN] = { 0 };
        FMTool::SetString(buf, str.toUtf8().length(), str);

        file.write(buf);
        file.flush();
        file.close();
        return true;
    }
};

#endif // RSPFACTORY
