import 'dart:io';

void main() {
  const int minTemp = 15;
  const int maxTemp = 30;

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

  while (true) {
    print('\n=== Smart Home Automation System ===');
    print('1. Turn Lights On/Off');
    print('2. Set Thermostat Temperature');
    print('3. Arm/Disarm Security System');
    print('4. View System Status');
    print('5. Exit');
    stdout.write('Enter your choice: ');

    String? choice = stdin.readLineSync();

    if (choice == null) continue;

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

      case '2':
        stdout.write('Enter temperature (\$minTemp–\$maxTemp °C): ');
        String? input = stdin.readLineSync();
        if (input != null) {
          int? temp = int.tryParse(input);
          if (temp != null && temp >= minTemp && temp <= maxTemp) {
            thermostat = temp;
            print('Thermostat set to \$thermostat°C');
          } else {
            print('❌ Invalid temperature. Please enter a value between \$minTemp and \$maxTemp.');
          }
        }
        break;

      case '3':
        stdout.write('Type "arm" to arm or "disarm" to disarm: ');
        String? input = stdin.readLineSync();
        if (input != null) {
          if (input.toLowerCase() == 'arm') {
            securityArmed = true;
            print('Security system ARMED.');
          } else if (input.toLowerCase() == 'disarm') {
            securityArmed = false;
            print('Security system DISARMED.');
          } else {
            print('❌ Invalid input. Type "arm" or "disarm".');
          }
        }
        break;

      case '4':
        String lightsStatus = lightsOn ? "ON" : "OFF";
        String securityStatus = securityArmed ? "ARMED" : "DISARMED";
        String dayMessage = "";
        if (thermostat % 3 == 0 && thermostat % 5 == 0) {
          dayMessage = "Perfect Day";
        } else if (thermostat % 3 == 0) {
          dayMessage = "Cool Day";
        } else if (thermostat % 5 == 0) {
          dayMessage = "Warm Day";
        }
        print('\n--- System Status ---');
        print('Lights: \$lightsStatus');
        print('Thermostat: \$thermostat°C \${dayMessage.isNotEmpty ? "(\$dayMessage)" : ""}');
        print('Security System: \$securityStatus');
        break;

      case '5':
        print('Exiting Smart Home System... Goodbye!');
        return;

      default:
        print('❌ Invalid choice. Please select from 1 to 5.');
    }
  }
}
