#ifndef SERVICE_H
#define SERVICE_H

#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <thread>
#include <memory>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <chrono>
#include <mutex>

namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;

struct PlatformData {
    double price;
    double deviationCompensation;
    bool isOpenLong;
    bool isOpenShort;
    std::vector<double> pricesHistory; // 用于存储历史价格以计算平均值
};

class MyService {
public:
    MyService(unsigned short port);
    void start();

private:
    void doHttpSession(tcp::socket socket);
    void doWebSocketSession(tcp::socket socket);

    void onHttpRequest(http::request<http::string_body> req);
    void onWebSocketMessage(const std::string& message);

    void updatePrice(const std::string &platform, double price);
    void adjustDecimalPoints();
    void checkDeviationCompensation();
    void calculateAveragePrices();
    void calculateDeviationPercentage();
    void determinePosition();

    unsigned short port_;
    std::map<std::string, PlatformData> platforms_; // 存储每个平台的数据
    std::chrono::time_point<std::chrono::steady_clock> lastCompensationTime_;
    std::mutex dataMutex_;
    const int compensationIntervalMinutes = 120; // 默认2小时
    const int averagePeriodMinutes = 15; // 默认15分钟
    const double entryThresholdPercentage = 0.01; // 示例开仓条件百分比
};

#endif // SERVICE_H
