#ifndef PLATFORMOBJECT_H
#define PLATFORMOBJECT_H

#include <QObject>
#include <vector>
#include <QMutex>

using namespace std;


class QNetworkAccessManager;


struct DataPlatform
{
	QString strPlatformType;
	QString strSymbol;
	__int64 nData; //ms
	float fPrice;
};


//开仓
struct OpenContractInfo
{
	QString tokenId;
	QString symbolId;
	int businessLine;
	QString secondBusinessLine;
	int businessSource;
	int enterPointSource;
	QString delegateCount;
	int orderType;
	int delegateType;
	QString pricedSymbol;
	int timeInForceValue;
	double delegatePrice;
	int languageType;


	OpenContractInfo()
	{
		tokenId = "USDT";
		symbolId = "BTCUSDT_UMCBL";
		businessLine = 10;
		secondBusinessLine = "N/A";
		businessSource = 0;
		enterPointSource = 1;
		delegateCount = "0.02";
		orderType = 0;
		delegateType = 1;
		pricedSymbol = "USDT";
		timeInForceValue = 0;
		delegatePrice = 0;
		languageType = 1;
	}
};


//平仓
struct CloseContractInfo
{
	QString tokenId;
	QString symbolId;
	int businessLine;
	int businessSource;
	int enterPointSource;
	QString secondBusinessLine;
	int timeInForceValue;
	bool cancelOrder;
	QString delegateCount;
	int orderType;
	int delegateType;
	int languageType;


	CloseContractInfo()
	{
		tokenId = "USDT";
		symbolId = "BTCUSDT_UMCBL";
		businessLine = 10;
		businessSource = 10;
		enterPointSource = 1;
		secondBusinessLine = "N/A";
		timeInForceValue = 0;
		cancelOrder = true;
		delegateCount = "0.02";
		orderType = 1;
		delegateType = 3;
		languageType = 1;
	}
};

//撤单
struct CancleContractInfo
{
	int businessLine;
	QString orderId;
	QString tokenId;
	QString symbolId;
	QString secondBusinessLine;	
	int languageType;


	CancleContractInfo()
	{
		orderId = "1242653936208982023";
		tokenId = "USDT";
		symbolId = "BTCUSDT_UMCBL";
		businessLine = 10;
	
		secondBusinessLine = "N/A";
	
		languageType = 1;
	}
};


class PlatformObject : public QObject
{
	Q_OBJECT

public:
	PlatformObject(QObject *parent = nullptr);
	~PlatformObject();

private:
	QString m_strPlatName;  // 平台名称
	double m_price;      // 当前价格
	double m_dBNPrice;//当前bn价格
	double m_deviationCompensation; // 偏差补偿值
	double m_deviationPercentage;    // 偏离度百分比
	bool m_isPositionOpen;          // 当前是否开仓
	bool m_isDealState;//true：开多  false：开空
	bool m_bOrderDirection;           // 下单方向，true :"buy"或 false:"sell"
	double m_dAverage;
	int m_nLever;//杠杆


	bool m_bCon;

	vector<DataPlatform> m_vDataVec;

	QNetworkAccessManager* m_pNetworkManager;
	QMutex m_QMutex;
public:
	void MinData(const DataPlatform &item);
	void SetPlatformName(QString strName);
	float CalcAverageSelf();

	double GetAverage();

	double GetRealPrice();

	double GetRealNewPrice();

	void CalcdeviationCompensation(double dBinance);
	double GetDeviationCompensation();

	void CalcdeviationPercentage(double dBinancePrice);


	 void executeTradingLogic(double entryThreshold);

	 
	 QByteArray PackJson_Open(const OpenContractInfo &item);

	 QByteArray PackJson_Close(const CloseContractInfo &item);

	  QByteArray PackJson_Cancle(const CancleContractInfo &item);

	 void SetPostionState();

	 //开多 - 限价
	 void OpenContractMore_XJ();

	 //开空 - 限价
	  void OpenContractEmpty_XJ();

	  //开空 - 市价
	  void OpenContractEmpty_SJ();


	  //平仓 - 闪现平仓 平多
	   void CloseContractMore();

	   //平仓 - 闪现平仓 平空
	   void CloseContractEmpty();


	  void SendPostRequst(QString strUrl,QByteArray &dataArray);


public slots:
	  void ReplyFinshed();

};

#endif // PLATFORMOBJECT_H
