#ifndef INC_STATE_MACHINE_H
#define INC_STATE_MACHINE_H

#include <stdint.h>
#include <stdbool.h>
#include "ADBMS_LL_Driver.h"
#include "AMS_HighLevel.h"
#include "PWM_control.h"
#include "TMP1075.h"
#include <math.h>

// Minimum vehicle side voltage to exit precharge
#define MIN_VEHICLE_SIDE_VOLTAGE 150000 // mV
// Time to wait after reaching 95% of battery voltage before exiting precharge
// Set this to 1000 in scruti to demonstrate the voltage on the multimeter
#define PRECHARGE_95_DURATION 0 // ms
// Time to wait for discharge
#define DISCHARGE_DURATION 5000 // ms
// Time to wait after there is no more error condition before exiting TS_ERROR
#define NO_ERROR_TIME 1000 // ms
// Time to wait for charger voltage before going to TS_ERROR
#define MAX_CHARGING_CHECK_DURATION 2000 // ms
// Time to wait between closing relays
#define RELAY_CLOSE_WAIT 10 // ms

typedef enum {              //  states -> 3 bit. valid transitions: (all could transition to error)
  STATE_INACTIVE,           // INACTIVE   ->  PRECHARGE, CHARGING, ERROR  
  STATE_PRECHARGE,          // PRECHARGE  ->  INACTIVE, READY, DISCHARGE, ERROR
  STATE_READY,              // READY      ->  ACTIVE, DISCHARGE, ERROR
  STATE_ACTIVE,             // ACTIVE     ->  READY, DISCHARGE, ERROR
  STATE_DISCHARGE,          // DISCHARGE  ->  INACTIVE, PRECHARGE, ERROR
  STATE_CHARGING_PRECHARGE,
  STATE_CHARGING,           // CHARGING   ->  INACTIVE, DISCHARGE, ERROR
  STATE_ERROR,              // ERROR      ->  INACTIVE, DISCHARGE, ERROR
} State;
  
typedef struct {  
  uint16_t bms_timeout : 1;
  uint16_t bms_overtemp : 1;
  uint16_t bms_fault : 1;

  uint16_t temperature_error : 1;
  uint16_t current_error : 1;
  uint16_t voltage_error : 1;

  uint16_t temperature_sensor_missing : 1;
  uint16_t current_sensor_missing : 1;
  uint16_t voltage_missing : 1;
  uint16_t battery_missing : 1;
  uint16_t relay_missing : 1;

  uint16_t state_transition_fail : 1;
} ErrorKind;

//typedef enum {} WarningKind;

typedef struct {
  State current_state;
  State target_state;
  uint16_t error_source; // TSErrorSource (bitmask)
  ErrorKind error_type; // TSErrorKind
} StateHandle;

extern StateHandle state;
static bool relay_closed = 0;
static bool precharge_closed = 0;
extern int16_t RELAY_BAT_SIDE_VOLTAGE;
extern int16_t RELAY_ESC_SIDE_VOLTAGE;
extern int16_t CURRENT_MEASUREMENT;

void sm_init();
void sm_update();

State sm_update_inactive();
State sm_update_precharge();
State sm_update_ready();
State sm_update_active();
State sm_update_discharge();
State sm_update_charging_precharge(); 
State sm_update_charging();
State sm_update_error();

typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay;
void sm_set_relay_positions(State state);
void sm_set_relay(Relay relay, bool closed);
void sm_check_charging();
void sm_check_cell_temps(int8_t* id, int16_t* temp);

void sm_handle_ams_in(const uint8 *data);
void sm_check_errors();
void sm_set_error(ErrorKind error_kind, bool is_errored);

void sm_test_cycle_states();

#endif /* "INC_STATE_MACHINE_H" */