#include <Arduino.h>

// Define pin numbers
const int RELAY_PIN_1 = 13;
const int RELAY_PIN_2 = 14;
const int RELAY_PIN_3 = 15;
const int RELAY_PIN_4 = 16;
const int RELAY_PIN_5 = 17;
const int RELAY_PIN_6 = 18;
const int RELAY_PIN_7 = 19;
const int RELAY_PIN_8 = 12;

const int WASHER_SWITCH_PIN = 11;
const int WIPER_SWITCH_PIN_1 = 2;
const int WIPER_SWITCH_PIN_2 = 3;
const int WIPER_SWITCH_PIN_3 = 4;
const int WIPER_SWITCH_PIN_4 = 5;
const int WIPER_SWITCH_PIN_5 = 6;
const int FAN_SWITCH_PIN_1 = 7;
const int FAN_SWITCH_PIN_2 = 8;
const int FAN_SWITCH_PIN_3 = 9;
const int FAN_SWITCH_PIN_4 = 10;

// Wiper motor states
bool isWiperParked = true;
bool isWiperIntermittentLow = false;
bool isWiperIntermittentHigh = false;
bool isWiperLow = false;
bool isWiperHigh = false;

// Washer motor state
bool isWasherOn = false;

// Fan motor states
bool isFanLow = false;
bool isFan1 = false;
bool isFan2 = false;
bool isFanHigh = false;

// Timer variables for intermittent states
unsigned long intermittentLowStartTime = 0;
unsigned long intermittentHighStartTime = 0;
const unsigned long intermittentLowInterval = 8000;  // 8 seconds
const unsigned long intermittentHighInterval = 16000;  // 16 seconds

void setup() {
  // Initialize relay pins as outputs
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(RELAY_PIN_2, OUTPUT);
  pinMode(RELAY_PIN_3, OUTPUT);
  pinMode(RELAY_PIN_4, OUTPUT);
  pinMode(RELAY_PIN_5, OUTPUT);
  pinMode(RELAY_PIN_6, OUTPUT);
  pinMode(RELAY_PIN_7, OUTPUT);
  pinMode(RELAY_PIN_8, OUTPUT);

  // Initialize switch pins as inputs
  pinMode(WASHER_SWITCH_PIN, INPUT);
  pinMode(WIPER_SWITCH_PIN_1, INPUT);
  pinMode(WIPER_SWITCH_PIN_2, INPUT);
  pinMode(WIPER_SWITCH_PIN_3, INPUT);
  pinMode(WIPER_SWITCH_PIN_4, INPUT);
  pinMode(WIPER_SWITCH_PIN_5, INPUT);
  pinMode(FAN_SWITCH_PIN_1, INPUT);
  pinMode(FAN_SWITCH_PIN_2, INPUT);
  pinMode(FAN_SWITCH_PIN_3, INPUT);
  pinMode(FAN_SWITCH_PIN_4, INPUT);
}

void loop() {
  checkWiperSwitch();
  checkWasherSwitch();
  checkFanSwitch();
}

void checkWiperSwitch() {
  if (digitalRead(WIPER_SWITCH_PIN_1) == HIGH) {
    // Wiper parking/off/default state
    isWiperParked = true;
    isWiperIntermittentLow = false;
    isWiperIntermittentHigh = false;
    isWiperLow = false;
    isWiperHigh = false;

    digitalWrite(RELAY_PIN_1, LOW);
    digitalWrite(RELAY_PIN_2, LOW);
    digitalWrite(RELAY_PIN_3, HIGH);
  } else if (digitalRead(WIPER_SWITCH_PIN_2) == HIGH) {
    // Intermittent low state
    isWiperParked = false;
    isWiperIntermittentLow = true;
    isWiperIntermittentHigh = false;
    isWiperLow = false;
    isWiperHigh = false;
    
    if (intermittentLowStartTime == 0) {
      intermittentLowStartTime = millis();
    }

    if (millis() - intermittentLowStartTime >= intermittentLowInterval) {
      intermittentLowStartTime = millis();
      digitalWrite(RELAY_PIN_3, LOW);
      delay(1000);
      digitalWrite(RELAY_PIN_3, HIGH);
    }
  } else if (digitalRead(WIPER_SWITCH_PIN_3) == HIGH) {
    // Intermittent high state
    isWiperParked = false;
    isWiperIntermittentLow = false;
    isWiperIntermittentHigh = true;
    isWiperLow = false;
    isWiperHigh = false;

    if (intermittentHighStartTime == 0) {
      intermittentHighStartTime = millis();
    }

    if (millis() - intermittentHighStartTime >= intermittentHighInterval) {
      intermittentHighStartTime = millis();
      digitalWrite(RELAY_PIN_3, LOW);
      delay(1000);
      digitalWrite(RELAY_PIN_3, HIGH);
    }
  } else if (digitalRead(WIPER_SWITCH_PIN_4) == HIGH) {
    // Wiper low state
    isWiperParked = false;
    isWiperIntermittentLow = false;
    isWiperIntermittentHigh = false;
    isWiperLow = true;
    isWiperHigh = false;

    digitalWrite(RELAY_PIN_1, LOW);
    digitalWrite(RELAY_PIN_2, LOW);
    digitalWrite(RELAY_PIN_3, LOW);
  } else if (digitalRead(WIPER_SWITCH_PIN_5) == HIGH) {
    // Wiper high state
    isWiperParked = false;
    isWiperIntermittentLow = false;
    isWiperIntermittentHigh = false;
    isWiperLow = false;
    isWiperHigh = true;

    digitalWrite(RELAY_PIN_1, HIGH);
    digitalWrite(RELAY_PIN_2, LOW);
    digitalWrite(RELAY_PIN_3, LOW);
  } else {
    // Default to wiper parking/off/default state
    isWiperParked = true;
    isWiperIntermittentLow = false;
    isWiperIntermittentHigh = false;
    isWiperLow = false;
    isWiperHigh = false;

    digitalWrite(RELAY_PIN_1, LOW);
    digitalWrite(RELAY_PIN_2, LOW);
    digitalWrite(RELAY_PIN_3, HIGH);
  }
}

void checkWasherSwitch() {
  if (digitalRead(WASHER_SWITCH_PIN) == HIGH) {
    // Washer motor control, relay 4
    isWasherOn = true;
    digitalWrite(RELAY_PIN_4, LOW);
  } else {
    isWasherOn = false;
    digitalWrite(RELAY_PIN_4, HIGH);
  }
}

void checkFanSwitch() {
  if (digitalRead(FAN_SWITCH_PIN_1) == HIGH) {
    // Fan motor low state
    isFanLow = true;
    isFan1 = false;
    isFan2 = false;
    isFanHigh = false;

    digitalWrite(RELAY_PIN_5, LOW);  // Inverted
    digitalWrite(RELAY_PIN_6, HIGH);
    digitalWrite(RELAY_PIN_7, HIGH);
    digitalWrite(RELAY_PIN_8, HIGH);
  } else if (digitalRead(FAN_SWITCH_PIN_2) == HIGH) {
    // Fan motor 1 state
    isFanLow = false;
    isFan1 = true;
    isFan2 = false;
    isFanHigh = false;

    digitalWrite(RELAY_PIN_5, HIGH);
    digitalWrite(RELAY_PIN_6, LOW);  // Inverted
    digitalWrite(RELAY_PIN_7, HIGH);
    digitalWrite(RELAY_PIN_8, HIGH);
  } else if (digitalRead(FAN_SWITCH_PIN_3) == HIGH) {
    // Fan motor 2 state
    isFanLow = false;
    isFan1 = false;
    isFan2 = true;
    isFanHigh = false;

    digitalWrite(RELAY_PIN_5, HIGH);
    digitalWrite(RELAY_PIN_6, HIGH);
    digitalWrite(RELAY_PIN_7, LOW);  // Inverted
    digitalWrite(RELAY_PIN_8, HIGH);
  } else if (digitalRead(FAN_SWITCH_PIN_4) == HIGH) {
    // Fan motor high state
    isFanLow = false;
    isFan1 = false;
    isFan2 = false;
    isFanHigh = true;

    digitalWrite(RELAY_PIN_5, HIGH);
    digitalWrite(RELAY_PIN_6, HIGH);
    digitalWrite(RELAY_PIN_7, HIGH);
    digitalWrite(RELAY_PIN_8, LOW);  // Inverted
  } else {
    // Default to fan motor off state
    isFanLow = false;
    isFan1 = false;
    isFan2 = false;
    isFanHigh = false;

    digitalWrite(RELAY_PIN_5, HIGH);
    digitalWrite(RELAY_PIN_6, HIGH);
    digitalWrite(RELAY_PIN_7, HIGH);
    digitalWrite(RELAY_PIN_8, HIGH);
  }
}
