/*
  控制命令
*/
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include "module/chassis.h"
#include "module/gimbal.h"
#include "module/shoot.h"


/* Exported constants ------------------------------------------------------- */
#define CMD_OK (0)        /* 运行正常 */
#define CMD_ERR_NULL (-1) /* 运行时发现NULL指针 */    
#define CMD_ERR_ERR (-2)  /* 运行时发现了其他错误 */
#define CMD_ERR_MODE (-3) /* 运行时配置了错误的Mode */
#define CMD_ERR_MOTOR (-4) /* 运行时配置了不存在的电机类型 */
#define CMD_ERR_MALLOC (-5) /* 内存分配失败 */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */

typedef union rc_inputdata_u rc_inputdata_u; 

#define CMD_REFEREE_MAX_NUM (3) /* 发送命令限定的最大数量 */

/* 输入源枚举 */
typedef enum {
  CMD_SRC_RC=0,      /* 遥控器 */
  CMD_SRC_PC,      /* 键盘鼠标 */
  CMD_SRC_NUC,     /* 上位机 */
  CMD_SRC_REFEREE, /* 裁判系统 */
  CMD_SRC_NUM,
  CMD_SRC_ERR
} CMD_InputSource_t;

typedef enum {
  CMD_MODULE_CHASSIS=0,
  CMD_MODULE_GIMBAL,
  CMD_MODULE_SHOOT,
  CMD_MODULE_NUM /* 模块数量 */
} CMD_MODULE_t;

/* RC part begin-------------------------------------- */
typedef struct {
  bool online;
  enum {DR16, AT9S} type;
  rc_inputdata_u *inputData;               
} CMD_InputData_RC_t;

/* RC part end---------------------------------------- */

/* PC part begin-------------------------------------- */
/* 键盘按键值 */
typedef enum {
  CMD_KEY_W = 0,
  CMD_KEY_S,
  CMD_KEY_A,
  CMD_KEY_D,
  CMD_KEY_SHIFT,
  CMD_KEY_CTRL,
  CMD_KEY_Q,
  CMD_KEY_E,
  CMD_KEY_R,
  CMD_KEY_F,
  CMD_KEY_G,
  CMD_KEY_Z,
  CMD_KEY_X,
  CMD_KEY_C,
  CMD_KEY_V,
  CMD_KEY_B,
  CMD_L_CLICK,
  CMD_R_CLICK,
  CMD_KEY_NUM,
} CMD_KeyValue_t;

typedef enum {
  CMD_ACTIVE_RISING_EDGE, /* 按下时触发 */
  CMD_ACTIVE_FALLING_EDGE,   /* 抬起时触发 */
  CMD_ACTIVE_PRESSED,  /* 按住时触发 */
} CMD_TriggerType_t;

/* 行为值序列 */
typedef enum {
  CMD_BEHAVIOR_FORE = 0,       /* 向前 */
  CMD_BEHAVIOR_BACK,           /* 向后 */
  CMD_BEHAVIOR_LEFT,           /* 向左 */
  CMD_BEHAVIOR_RIGHT,          /* 向右 */
  CMD_BEHAVIOR_ACCELERATE,     /* 加速 */
  CMD_BEHAVIOR_DECELEBRATE,    /* 减速 */
  CMD_BEHAVIOR_FIRE,           /* 开火 */
  CMD_BEHAVIOR_FIRE_MODE,      /* 切换开火模式 */
  CMD_BEHAVIOR_BUFF,           /* 打符模式 */
  CMD_BEHAVIOR_AUTOAIM,        /* 自瞄模式 */
  CMD_BEHAVIOR_OPENCOVER,      /* 弹舱盖开关 */
  CMD_BEHAVIOR_ROTOR,          /* 小陀螺模式 */
  CMD_BEHAVIOR_REVTRIG,        /* 反转拨弹 */
  CMD_BEHAVIOR_FOLLOWGIMBAL35, /* 跟随云台呈35度 */
  CMD_BEHAVIOR_NUM,
} CMD_Behavior_t;
typedef struct {
  CMD_KeyValue_t key;
  CMD_TriggerType_t trigger_type;
} CMD_KeyMapItem_t;

/* 行为映射的对应按键数组 */
typedef struct {
  CMD_KeyMapItem_t key_map[CMD_BEHAVIOR_NUM];
} CMD_KeyMap_Params_t;

typedef struct {
  float sens_mouse;       /* 鼠标灵敏度 */
  float move_sense;        /* 移动灵敏度 */
  float move_fast_sense;   /* 快速移动灵敏度 */
  float move_slow_sense;   /* 慢速移动灵敏度 */
} CMD_PC_Sensitivity_t;

typedef struct {
  struct {
    int16_t x;
    int16_t y;
    int16_t z;
    bool l_click; /* 左键 */
    bool r_click; /* 右键 */
  } mouse;        /* 鼠标值 */

  uint16_t key; /* 按键值 */

  uint16_t res; /* 保留，未启用 */
} CMD_PC_Data_t;

typedef struct {
  struct {
    bool l_click; /* 左键 */
    bool r_click; /* 右键 */
  } mouse;        /* 鼠标值 */
  uint16_t key; /* 按键值 */
} CMD_PC_LastData_t;

typedef struct {
  bool online;
  CMD_PC_Data_t data;
  CMD_PC_LastData_t lastData;
}CMD_InputData_PC_t;

typedef struct {
  CMD_KeyMap_Params_t map; /* 按键映射行为命令 */
  CMD_PC_Sensitivity_t sensitivity; /* PC灵敏度设置 */
}CMD_PCParams_t;

/* PC part end---------------------------------------- */

/* NUC part begin------------------------------------- */
typedef struct {
  bool online;
}CMD_InputData_NUC_t;

/* NUC part end--------------------------------------- */

/* REF part begin------------------------------------- */
typedef struct {
  bool online;
  CMD_PC_Data_t data;
}CMD_InputData_REF_t;

/* REF part begin------------------------------------- */

/* 底盘控制命令 */
typedef struct {
  CMD_InputSource_t source;
  Chassis_CMD_t cmd;
} CMD_OutputCmd_CHASSIS_t;

/* 云台控制命令 */
typedef struct {
  CMD_InputSource_t source;
  Gimbal_CMD_t cmd;
} CMD_OutputCmd_GIMBAL_t;

/* 射击控制命令 */
typedef struct {
  CMD_InputSource_t source;
  Shoot_CMD_t cmd;
} CMD_OutputCmd_SHOOT_t;

typedef struct {
  CMD_InputData_RC_t rc;
  CMD_InputData_PC_t pc;
  CMD_InputData_NUC_t nuc;
  CMD_InputData_REF_t ref;
} CMD_InputData_t;
typedef struct {
  CMD_OutputCmd_CHASSIS_t chassis;
  CMD_OutputCmd_GIMBAL_t  gimbal;
  CMD_OutputCmd_SHOOT_t   shoot;
} CMD_OutputCmd_t;
typedef struct {
    CMD_InputSource_t source;
    uint8_t priority;
} CMD_SourcePriorityConfig_t;

typedef struct {
  CMD_SourcePriorityConfig_t sourcePriorityConfigs[CMD_SRC_NUM];/* 输入源优先级配置 */
  CMD_PCParams_t pc;
} CMD_Param_t;

typedef struct {

  CMD_Param_t params;

  CMD_InputData_t inputData;
  CMD_OutputCmd_t outCmd;
  

} CMD_t;

/* Exported functions prototypes -------------------------------------------- */
#ifdef __cplusplus
}
#endif
