/*
 * BusinessConfig.h
 *
 *  Created on: 2022年9月29日
 *      Author: Allen
 */
#include <string.h>
#include <time.h>
#include <termio.h>
#include <atomic>
#include "uart/CommDef.h"
#include "storage/StoragePreferences.h"
#include "utils/BrightnessHelper.h"
#include "utils/TimeHelper.h"
#include "utils/TimeUtil.h"
#include "utils/Log.h"
#include "net/tcp_client.h"
#include "net/tcp_model.h"
#include "security/SecurityManager.h"



#ifndef JNI_SERVICE_BUSINESSCONFIG_H_
#define JNI_SERVICE_BUSINESSCONFIG_H_

#define WDKL_VERSION "wdkl_version"

#define STORE_DEVICE_ID "sdevice_id"
#define STORE_DEVICE_TYPE "device_type"
#define STORE_PARTID "part_id"
#define STORE_PARTNAME "part_name"
#define STORE_CUSTOMER_ID "customer_id"
#define STORE_GATEWAY "gateway"
#define STORE_HTTP_PORT "http_port"
#define STORE_TCP_LOCAL_IP "tcp_local_ip"
#define STORE_TCP_PORT "tcp_port"
#define STORE_SIGNAL_TYPE "signal_type"	//语音信令方式， TCP、SIP

#define STORE_DAY_START "day_start"
#define STORE_DAY_LIGHT "day_light"
#define STORE_DAY_VOL "day_vol"
#define STORE_DAY_RING_TIMES "day_ring_times"
#define STORE_NIGHT_START "night_start"
#define STORE_NIGHT_LIGHT "night_light"
#define STORE_NIGHT_VOL "night_vol"
#define STORE_NIGHT_RING_TIMES "night_ring_times"

#define STORE_DOCTOR_TITLE "doctor_title"
#define STORE_DOCTOR_VISIBLE "doctor_visible"
#define STORE_NURSE_TITLE "nurse_title"
#define STORE_NURSE_VISIBLE "nurse_visible"

#define STORE_LORA_CMD "lora_cmd"
#define STORE_LORE_CONTENT "lora_content"

#define STORE_AUDO_ANSWER "auto_answer"

static std::string version = "v2.0.8";
static int versionNo = 8;
static std::string serverIP = "172.28.100.100";
static std::string tcpIP = "172.28.100.100";
static int serverHttpPort = 8006;
static int serverTcpPort = 5086;

static UINT uartBaudRate1 = B115200;
static UINT uartBaudRate2 = B115200;
static UINT uartBaudRate3 = B115200;

static bool isHumAble = true;


static std::string getHttpGateway(){
	std::string sserverIP = StoragePreferences::getString(STORE_GATEWAY, serverIP);
	int sserverHttpPort = StoragePreferences::getInt(STORE_HTTP_PORT, serverHttpPort);
	char gateWay[100];
	sprintf(gateWay,"http://%s:%d",sserverIP.c_str(),sserverHttpPort);
	LOGD("getHttpGateway %s",gateWay);
	return gateWay;
}

static std::string getTcpGateway(){
	std::string sserverIP = StoragePreferences::getString(STORE_TCP_LOCAL_IP, tcpIP);
//	StoragePreferences::putInt(STORE_TCP_PORT, serverTcpPort);
	int sserverTcpPort = StoragePreferences::getInt(STORE_TCP_PORT, serverTcpPort);
	char gateWay[100];
	sprintf(gateWay,"%s:%d",sserverIP.c_str(),sserverTcpPort);
	LOGD("getTcpGateway %s",gateWay);
	return gateWay;
}


static std::string getVersion() {
	LOGD("version -> %s",version.c_str());
	return version;
}

static int getVersionNo() {
	LOGD("versionNo -> %d", versionNo);
	return versionNo;
}

/**
 * 设置科室名称
 */
void NavibarSetPartName(const std::string& partName);

/**
 * 设置床位区块颜色和文字
 */
void SetPainterInfo(uint32_t bgColor, const std::string& msg);

void setReinforce(bool result);

/**
 * 刷新设备数据
 */
void dataRefresh();

/**
 * 设置增援按钮是否可用
 */
void setBtnHelpVisible(bool visible);

/**
 * 取消 增援一分钟后自动取消
 */
void cancelAutoBtnHelpTimer();

void s433DataHandle(std::string s433DataStr);

void sendToLora(int cmd, int content);
void sendDeleteToLoraAndRestart(std::string del, std::string mac);

void setUartList(std::string text);

void threadInit();


enum CallFinishType {
	CANCEL,
	REJECT,
	BUSY,
	HANDOFF,
	FAILED,
	ACCEPT,
	A1CLICK,
	KEY6_CLICK,
};
void callActivityFinish(CallFinishType type);
void buildSIP(std::string toSipId);
void acceptSIP();

void sendVoiceTcp(std::string action, TcpModel model, int toId);

/**
 * 是否白天
 */
static bool checkIsDay(){
	struct tm *t = TimeHelper::getDateTime();
	//获得当前日期
	char cDate[20];
	sprintf(cDate,"%d-%02d-%02d",
			1900 + t->tm_year, 1+ t->tm_mon, t->tm_mday);
	std::string cDateStr = cDate;
	//组合白天与黑夜开始时间
	std::string dayStart = cDateStr + StoragePreferences::getString(STORE_DAY_START, " 07:00:00");
	std::string nightStart = cDateStr + StoragePreferences::getString(STORE_NIGHT_START, " 18:00:00");

	//LOGD("dayStart %s . nightStart %s", dayStart.c_str(), nightStart.c_str());

	time_t tDayStart = TimeUtil::StringToDatetime(dayStart);
	time_t tNightStart = TimeUtil::StringToDatetime(nightStart);
	time_t currentTime = mktime(t) - 28800;
	if (currentTime>=tDayStart && currentTime<= tNightStart){
		return true;
	}
	return false;

	//LOGD("tiDayStart %ld . tNightStart %ld . currentTime %ld", tDayStart, tNightStart, currentTime);
}

static std::string isOutgoing = "isOutgoing";
static std::string audioOnly = "audioOnly";

class CallingStatus{
public:
	virtual ~CallingStatus();
	static CallingStatus* instance();

	void setBusy(bool busy);
	void setTcpModel(TcpModel tcpModel);
	void clearTcpModel();

	static bool busy();
	TcpModel getTcpModel();
	long getInteractionId();
private:
	CallingStatus();
private:
  std::atomic<bool> _busy;
  TcpModel _tcpModel;
  long _interactionId;
};

static std::string appUpdate = "appUpdate";

std::string GetDeviceUid();

class UartMsg{
public:
    const char* msg;
    int count;
};
static std::vector<UartMsg> queueMsgs;
void addUartMsg(const char* msg, int count);
UartMsg getUartMsg();


class S433Msg {
public:
	std::string msg;
	int count;
	int route;
};

static std::vector<S433Msg> s433Msgs;
void addS433Msg(std::string, int count, int route);
S433Msg getS433Msg();
bool isS433Empty();


#endif /* JNI_SERVICE_BUSINESSCONFIG_H_ */
