#ifndef HTTP_SERVER_H
#define HTTP_SERVER_H

#include "common_config.h"
#include "lwip/api.h"
#include "mbedtls/ssl.h"

#ifdef HTTP_SERVER_ENABLE

#define HTTP_AUTH_ENABLE
     
#define SSL_TASK_PRIO   ( configMAX_PRIORITIES - 3 )
#define FRAME_SIZE      (1000)

#define SEND_BUF_MAX_LEN      4000
#define RECIVE_BUF_MAX_LEN    1500

typedef enum
{
	ALL_ACCESS = 0,
  TIME_ACCESS
} ACCESS_TYPE_t;

typedef enum
{
	COMMON_ANSWER = 0,
  PARAMETER_ANSWER,
	HISTORY_ANSWER,
	UPS_HISTORY_ANSWER

} REQ_TYPE_SEND_t;

typedef enum
{
  SEND_REQUIRED_YES = 0,
  SEND_REQUIRED_NO,
  SEND_REQUIRED_FILE
} REQ_TYPE_t;

struct http_state
{
  char *file;
  u32_t left;
  u32_t reqnum;
  void *locked;
};

struct fs_file {
  char *data;
  int len;
};

void HTTP_Init();

/**
  * @brief  Выводим запросы в консоль
  */
void vTaskPrintHttp(void *pvParameters);

/**
  * @brief  Установка даты производства
  */
void HTTP_Prodate(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Парсинг строки настроек
  */
void HTTP_SetSettings(char *buf, uint16_t lenBuf);

/**
  * @brief  
  */
void HTTP_SetInfo(char *buf, uint16_t lenBuf);

/**
  * @brief  
  */
char* HTTP_SetSettingsPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

#ifdef NOTIFICATION_CONTROL_ENABLE
/**
  * @brief
  * @retval None
  */
char *HTTP_SnmpParam(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief
  * @retval None
  */
void HTTP_SetNotification(char *buf, uint16_t lenBuf);
#endif

/**
  * @brief  
  */
char* HTTP_SetInfoPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

#ifdef HTTP_AUTH_ENABLE
/**
  * @brief  
  */
char* HTTP_LoginPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_LogoutPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Смена пароля пользователя
  * @retval None
  */
char *HTTP_ChangeUserPwd(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);
#endif

/**
  * @brief  
  */
char* HTTP_UpsHistoryPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_HistoryPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_Reset(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_Reboot(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_Confirm(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Запуск/останов теста UPS
  */
char* HTTP_UPSTest(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Выклюение UPS
  */
char* HTTP_UPSshutdown(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Проверка пароля для перехода в режим bootloader
  */
char* HTTP_ConfirmBootPwd(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Проверка пароля для входа в Web
  */
int HTTP_ConfirmWebPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_GetRequest(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_NoFound(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
uint8_t GetParamValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen);
uint8_t GetParamValue0(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen);

uint8_t GetCookieValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen);

/**
  * @brief  
  * @retval None
  */
uint8_t GetParamValueInEnd(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen);

void ClearParamString(char *inBuf);

#endif

#endif /* HTTP_SERVER_H */
