import 'dart:io';

void main() {
  const int minTemperature = 15;
  const int maxTemperature = 30;

  bool lightsOn = false;
  int thermostatTemperature = 20;
  bool securityArmed = false;

  print("Welcome to the Smart Home Automation System!");

  while (true) {
    print("\nMenu:");
    print("1. Toggle Lights");
    print("2. Set Thermostat Temperature");
    print("3. Arm/Disarm Security System");
    print("4. View System Status");
    print("5. Exit");
    stdout.write("Choose an option (1-5): ");

    String? choice = stdin.readLineSync();

    switch (choice) {
      case '1':
        lightsOn = !lightsOn;
        print("Lights are now ${lightsOn ? 'ON' : 'OFF'}.");
        break;

      case '2':
        thermostatTemperature = setThermostatTemperature(minTemperature, maxTemperature);
        break;

      case '3':
        securityArmed = !securityArmed;
        print("Security system is now ${securityArmed ? 'ARMED' : 'DISARMED'}.");
        break;

      case '4':
        viewSystemStatus(lightsOn, thermostatTemperature, securityArmed);
        break;

      case '5':
        print("Exiting the system. Goodbye!");
        return;

      default:
        print("Invalid option. Please choose again.");
    }
  }
}

int setThermostatTemperature(int min, int max) {
  int temperature = -1;
  while (true) {
    stdout.write("Enter thermostat temperature ($min°C to $max°C): ");
    String? input = stdin.readLineSync();
    
    if (input != null && int.tryParse(input) != null) {
      temperature = int.parse(input);
      if (temperature >= min && temperature <= max) {
        print("Thermostat set to $temperature°C.");
        return temperature;
      } else {
        print("Please enter a temperature between $min°C and $max°C.");
      }
    } else {
      print("Invalid input. Please enter a valid number.");
    }
  }
}

void viewSystemStatus(bool lightsOn, int thermostatTemperature, bool securityArmed) {
  String lightsStatus = lightsOn ? "ON" : "OFF";
  String securityStatus = securityArmed ? "ARMED" : "DISARMED";
  String temperatureMessage = "Thermostat is set to $thermostatTemperature°C.";

  // FizzBuzz-like logic for temperature
  if (thermostatTemperature % 3 == 0 && thermostatTemperature % 5 == 0) {
    temperatureMessage += " Perfect Day.";
  } else if (thermostatTemperature % 3 == 0) {
    temperatureMessage += " Cool Day.";
  } else if (thermostatTemperature % 5 == 0) {
    temperatureMessage += " Warm Day.";
  }

  print("\n--- System Status ---");
  print("Lights: $lightsStatus");
  print("Thermostat: $temperatureMessage");
  print("Security System: $securityStatus");
}
