#include "httplib.h"
#include "common/JsonN.h"
#include "errcode.h"
#include <functional>

class HttpEntity;
using HandlerFunc = Errcode(HttpEntity::*)(const httplib::Request& req, njson& jnode, std::string& errmsg);

struct HandlerOptions
{
    HandlerFunc func;
    std::vector<std::string> requiredKeys;
    std::vector<std::string> keys;

    HandlerOptions(HandlerFunc func, const std::vector<std::string>& requiredKeys)
        : func(func), requiredKeys(requiredKeys)
    {
    }
};

class HttpEntity
{
public:
    httplib::Server httpsvr;
    HttpEntity();
    void listen(std::string addr, int port);
    void runHandler(std::string name, const HandlerOptions& handler, const httplib::Request& req, httplib::Response& resp);
    void registGet(std::string name, void (HttpEntity::* func)(const httplib::Request& req, httplib::Response& resp));

    //void onGet(const httplib::Request& req, httplib::Response& resp);

    Errcode logqueryBaseinfoin(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode login(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryUserList(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode insertUser(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode updateUser(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode deleteUser(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryPermissionList(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode insertPermission(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode updatePermission(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode deletePermission(const httplib::Request& req, njson& json, std::string& errmsg);


    Errcode queryRoleList(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode queryRolePermission(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode insertRole(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode updateRole(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode deleteRole(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryStationList(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode insertStation(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode updateStation(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode deleteStation(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryStationOverview(const httplib::Request& req, njson& json, std::string& errmsg);

    /// 查询场站信息
    // @param: station_id
    // @return: object
    Errcode queryStationInfo(const httplib::Request& req, njson& json, std::string& errmsg);

    /// 查询场站信息
    // @param: station_id
    // @return: object
    Errcode queryStationData(const httplib::Request& req, njson& json, std::string& errmsg);

    // 获取场站今日电压曲线数据，category: 1：储能，2：充电，3：光伏
    Errcode queryStationTodayU(const httplib::Request& req, njson& json, std::string& errmsg);
    // 获取场站今日电流曲线数据，category: 1：储能，2：充电，3：光伏
    Errcode queryStationTodayI(const httplib::Request& req, njson& json, std::string& errmsg);
    // 获取场站今日功率曲线数据，category: 1：储能，2：充电，3：光伏
    Errcode queryStationTodayP(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryDeviceList(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode insertDevice(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode updateDevice(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode deleteDevice(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode queryDevicTypeDef(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode queryDevicByCategory(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode queryDevicCharts(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode queryDeviceBCUDetail(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryPolicyList(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode insertPolicy(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode updatePolicy(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode deletePolicy(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode queryPolicyByType(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode querySystemLogList(const httplib::Request& req, njson& json, std::string& errmsg);
    //Errcode insertSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json);
    Errcode updateSystemLog(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryAlertLogList(const httplib::Request& req, njson& json, std::string& errmsg);
    //Errcode insertAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json);
    Errcode updateAlertLog(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryPredictionDetail(const httplib::Request& req, njson& json, std::string& errmsg);

    /// 总览: 查询累计统计信息
    // @param: station_id  可选，无该参数时查询所有场站累计数据
    Errcode queryStatTotal(const httplib::Request& req, njson& json, std::string& errmsg);

    // 总览: 所有场站统计 (总览页 运行状况)
    Errcode queryStatSystem(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryStatStationGroup(const httplib::Request& req, njson& json, std::string& errmsg);
    // 场站按类别按天统计
    Errcode queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg);

    //Errcode queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryStatDetailList(const httplib::Request& req, njson& json, std::string& errmsg);

    // 场站按类某一天的历史曲线数据
    Errcode queryStatCharts(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode exportStatReport(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryEnvironment(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode queryServiceApiList(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode insertServiceApi(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode updateServiceApi(const httplib::Request& req, njson& json, std::string& errmsg);
    Errcode deleteServiceApi(const httplib::Request& req, njson& json, std::string& errmsg);

    Errcode updateGatewayParams(const httplib::Request& req, njson& json, std::string& errmsg);

    /// ===========================================================================================
    // 查询电网的时段（尖、峰、平、谷）
    // 参数: 日期（年-月-日）
    Errcode queryEGridPeriod(const httplib::Request& req, njson& json, std::string& errmsg);
};