#define SWITCH_NUM 8
#define SWITCH_INVERT true

#define PINOUT_WATER_BANK_FILL_PUMP_SWITCH 1
#define PINOUT_STERILIZATION_CHAMBER_FILL_PUMP_SWITCH 3
#define PINOUT_STERILIZATION_CHAMBER_CIRCULATION_PUMP_SWITCH 17
#define PINOUT_STERILIZED_CHAMBER_FILL_PUMP_SWITCH 16
#define PINOUT_STERILIZATION_OZONE_AND_UV_SWITCH 4
#define PINOUT_STERILIZED_UV_SWITCH 0
#define PINOUT_INVERTER_SWITCH 2
#define PINOUT_INSTRUMENTATION_SWITCH 15

static byte SWITCH_OFF = LOW;
static byte SWITCH_ON = HIGH;

static byte switchState[SWITCH_NUM];
static int8_t switchPin[SWITCH_NUM] = {PINOUT_WATER_BANK_FILL_PUMP_SWITCH,  
                                       PINOUT_STERILIZATION_CHAMBER_FILL_PUMP_SWITCH,
                                       PINOUT_STERILIZATION_CHAMBER_CIRCULATION_PUMP_SWITCH,
                                       PINOUT_STERILIZED_CHAMBER_FILL_PUMP_SWITCH,
                                       PINOUT_STERILIZATION_OZONE_AND_UV_SWITCH,
                                       PINOUT_STERILIZED_UV_SWITCH,
                                       PINOUT_INVERTER_SWITCH,
                                       PINOUT_INSTRUMENTATION_SWITCH}; //set -1 to mark as unused

//old code
//#define WATER_BANK_FILL_PUMP_SWITCH 0
//#define STERILIZATION_CHAMBER_FILL_PUMP_SWITCH 1
//#define STERILIZATION_CHAMBER_CIRCULATION_PUMP_SWITCH 2
//#define STERILIZED_CHAMBER_FILL_PUMP_SWITCH 3
//#define STERILIZATION_OZONE_AND_UV_SWITCH 4 
//#define STERILIZED_UV_SWITCH 5 //aka post uv switch
//#define INVERTER_SWITCH 6
//#define INSTRUMENTATION_SWITCH 7 //for sensors and display power

#define WATER_BANK_FILL_PUMP_SWITCH 0
#define STERILIZATION_CHAMBER_FILL_PUMP_SWITCH 1
#define STERILIZATION_CHAMBER_CIRCULATION_PUMP_SWITCH 2
#define STERILIZED_CHAMBER_FILL_PUMP_SWITCH 3
#define STERILIZATION_OZONE_AND_UV_SWITCH 4
#define STERILIZED_UV_SWITCH 5 //aka post uv switch
#define INSTRUMENTATION_SWITCH 6 //for sensors and display power
#define INVERTER_SWITCH 7

void TurnOffAllRelay(){
  for (int i=0; i<SWITCH_NUM; i++){
    if (switchPin[i]>=0){
      digitalWrite(switchPin[i], SWITCH_OFF);
    }
    switchState[i] = SWITCH_OFF;
  }
}

void InitSwitchHandler(){
  if (!SWITCH_INVERT){
    SWITCH_OFF = LOW;
    SWITCH_ON = HIGH;
  }else{
    SWITCH_OFF = HIGH;
    SWITCH_ON = LOW;    
  }
  Serial.println("switch init");
  for (int i=0; i<SWITCH_NUM; i++){
    if (switchPin[i]>=0){
      pinMode(switchPin[i], OUTPUT);
      digitalWrite(switchPin[i], SWITCH_OFF);
    }
    switchState[i] = SWITCH_OFF;  
  }
}

void checkRelay(){

  for(int i=0; i < SWITCH_NUM; i++){
    digitalWrite(PINOUT_WATER_BANK_FILL_PUMP_SWITCH, SWITCH_OFF);
    delay(1000);
  }

  for(int i=0; i<SWITCH_NUM;i++){
    digitalWrite(PINOUT_WATER_BANK_FILL_PUMP_SWITCH, SWITCH_ON);
    delay(1000);
  }


  // digitalWrite(PINOUT_WATER_BANK_FILL_PUMP_SWITCH, SWITCH_OFF);
  // digitalWrite(PINOUT_STERILIZED_CHAMBER_FILL_PUMP_SWITCH, SWITCH_ON);
  // delay(1000);
  // digitalWrite(PINOUT_WATER_BANK_FILL_PUMP_SWITCH, SWITCH_ON);
  // digitalWrite(PINOUT_STERILIZED_CHAMBER_FILL_PUMP_SWITCH, SWITCH_OFF);
  // delay(1000);
  
  // for (int i=0; i<SWITCH_NUM; i++){
  //   Serial.println(switchPin[i]);
  //   // digitalWrite(switchPin[i], SWITCH_ON);
  //   // delay(1000);
  //   digitalWrite(switchPin[i], SWITCH_OFF);
  //   delay(350);
  // }
}

bool GetSwitchState(int idx){
  if (idx<0 || idx>=SWITCH_NUM) return false;
  return (switchState[idx]==SWITCH_ON?true:false);
}

void SetSwitchState(int idx, bool v){
  if (idx<0 || idx>=SWITCH_NUM) return;
  byte state = v?SWITCH_ON:SWITCH_OFF;
  if (switchPin[idx]>=0) digitalWrite(switchPin[idx], state);
  switchState[idx] = state;
}

//used for unit test
void SetSwitchStateSimulated(int idx, bool v){
  if (idx<0 || idx>=SWITCH_NUM) return;
  byte state = v?SWITCH_ON:SWITCH_OFF;
  switchState[idx] = state;
}

uint32_t swDemoT = 0;
int swx = 0;
void SwitchDemo(){
  if (millis()-swDemoT<1000) return;
  swDemoT = millis();
  
  for(int i=0;i<SWITCH_NUM;i++){
    SetSwitchState(i,i==swx?true:false);
  }
  swx++;
  if (swx>=SWITCH_NUM) swx = 0;
}