#ifndef __INC_VEHICLE_STATE_H
#define __INC_VEHICLE_STATE_H

#ifdef __cplusplus
extern "C" {
#endif

#include "params.h"
#include "stw_defines.h"
#include <stdint.h>

#define NUM_CONES 12

typedef enum {
  TS_INACTIVE = 0,
  TS_ACTIVE = 1,
  TS_PRECHARGE = 2,
  TS_DISCHARGE = 3,
  TS_ERROR = 4,
  TS_CHARGING_CHECK = 5,
  TS_CHARGING = 6
} TSState;

typedef enum {
  AS_OFF = 0,
  AS_MANUAL = 1,
  AS_READY = 2,
  AS_DRIVING = 3,
  AS_FINISHED = 4,
  AS_EMERGENCY = 5
} ASState;

typedef enum {
  R2D_NONE = 0,
  R2D_TSMS = 1,
  R2D_TSACTIVE = 2,
  R2D_RESETTING_NODES = 3,
  R2D_RESETTING_COMMS = 4,
  R2D_WAITING_INIT = 5,
  R2D_INIT_STAGE1 = 6,
  R2D_INIT_STAGE2 = 7,
  R2D_INIT_SUCCESS = 0xF
} R2DProgress;

typedef enum {
  INICHK_START = 0,
  INICHK_WD_CHECK = 1,
  INICHK_WD_OK = 2,
  INICHK_POS_CHK_1 = 3,
  INICHK_ASB_CHECK_A_1 = 4,
  INICHK_ASB_CHECK_A_2 = 5,
  INICHK_SWITCH_B = 6,
  INICHK_ASB_CHECK_B_1 = 7,
  INICHK_ASB_CHECK_B_2 = 8,
  INICHK_SWITCH_A = 9,
  INICHK_ASB_CHECK_AB_1 = 10,
  INICHK_ASB_CHECK_AB_2 = 11,
  INICHK_POS_CHK_2 = 12,
  INICHK_WAIT_TS = 13,
  INICHK_DONE = 14,
  INICHK_ERROR = 15
} IniChkState;

const char *inichkstate_str(IniChkState state);

typedef enum {
  AMS_ERROR_NONE = 0x00,
  AMS_ERROR_SLAVE_TIMEOUT = 0x01,
  AMS_ERROR_SLAVE_PANIC = 0x02,
  AMS_ERROR_SHUNT_TIMEOUT = 0x03,
  AMS_ERROR_SHUNT_OVERCURRENT = 0x04,
  AMS_ERROR_SHUNT_OVERTEMP = 0x05
} AMSErrorKind;
typedef enum {
  AMS_SLAVEPANIC_OVERTEMP = 0x00,
  AMS_SLAVEPANIC_UNDERTEMP = 0x01,
  AMS_SLAVEPANIC_OVERVOLTAGE = 0x02,
  AMS_SLAVEPANIC_UNDERVOLTAGE = 0x03,
  AMS_SLAVEPANIC_TOO_FEW_TEMP = 0x04,
  AMS_SLAVEPANIC_OPENWIRE = 0x05
} AMSSlavePanicKind;

typedef struct {
  float tire_fl;
  float tire_fr;
  float tire_rl;
  float tire_rr;

  float brake_fl;
  float brake_fr;
  float brake_rl;
  float brake_rr;

  float inv_l;
  float inv_r;
  float mot_l;
  float mot_r;
  float bat_l;
  float bat_r;
} Temperatures;

typedef struct {
  uint16_t x;
  uint16_t y;
} ConePosition;

typedef struct {
  TSState ts_state;
  ASState as_state;
  Mission active_mission;

  int sdc_closed;

  R2DProgress r2d_progress;
  struct {
    int invl_ready : 1;
    int invr_ready : 1;
    int sdc_bfl : 1;
    int sdc_brl : 1;
    int sdc_acc : 1;
    int sdc_hvb : 1;
    int err_sdc : 1;
    int err_ams : 1;
    int err_pdu : 1;
    int err_ini_chk : 1;
    int err_con_mon : 1;
    int err_scs : 1;
    int err_sbspd : 1;
    int err_appsp : 1;
    int err_as : 1;
    int err_ros : 1;
    int err_res : 1;
    int err_invl : 1;
    int err_invr : 1;
  } errors;
  struct {
    AMSErrorKind kind;
    uint8_t arg;
  } last_ams_error;
  struct {
    uint8_t id;
    AMSSlavePanicKind kind;
    uint32_t arg;
  } last_ams_slave_panic;
  IniChkState ini_chk_state;

  unsigned lap_count;
  float lap_last;
  float lap_best;

  Temperatures temps;

  float min_cell_volt;
  float max_cell_temp;
  int soc_ts;

  float lv_bat_voltage;
  int soc_lv;

  float ts_current;
  float ts_voltage_bat;
  float ts_voltage_veh;

  float speed;

  float brake_press_f;
  float brake_press_r;
  float hyd_press_a;
  float hyd_press_b;

  float distance_total;

  uint32_t last_jetson_msg;
  uint32_t last_epsc_msg;
  float desired_angle;
  float measured_angle;
  float desired_speed;

  ConePosition cone_pos[NUM_CONES];

  ParamType last_param_confirmed;
} VehicleState;

extern VehicleState vehicle_state;

#ifdef __cplusplus
} // extern "C"
#endif

#endif // __INC_VEHICLE_STATE_H