import 'package:get/get.dart';
import 'package:sigap/src/features/personalization/presentasion/controllers/settings/base_settings_controller.dart';

class EmailController extends BaseSettingsController {
  // Email settings
  final RxString currentEmail = "anitarose@example.com".obs;
  final RxBool receiveNewsletter = true.obs;
  final RxBool receivePromotions = false.obs;

  // Email verification status
  final RxBool isEmailVerified = true.obs;

  @override
  void onInit() {
    super.onInit();
    loadSettings();
  }

  // Toggle email preference
  void toggleEmailPreference(String preference, bool value) {
    switch (preference) {
      case 'receive_newsletter':
        receiveNewsletter.value = value;
        break;
      case 'receive_promotions':
        receivePromotions.value = value;
        break;
    }

    saveSettings();
  }

  // Change primary email
  Future<bool> changeEmail(String newEmail, String password) async {
    try {
      setLoading(true);

      // TODO: Implement email change logic with API
      // Normally would validate password and update email

      // Simulate API call
      await Future.delayed(const Duration(seconds: 1));

      if (newEmail.isEmpty || !GetUtils.isEmail(newEmail)) {
        setError('Please enter a valid email address.');
        return false;
      }

      currentEmail.value = newEmail;
      isEmailVerified.value = false; // New email needs verification
      await saveSettings();

      return true;
    } catch (e) {
      setError('Failed to change email: ${e.toString()}');
      return false;
    } finally {
      setLoading(false);
    }
  }

  // Send verification email
  Future<bool> sendVerificationEmail() async {
    try {
      setLoading(true);

      // TODO: Implement send verification email logic with API

      // Simulate API call
      await Future.delayed(const Duration(seconds: 1));

      return true;
    } catch (e) {
      setError('Failed to send verification email: ${e.toString()}');
      return false;
    } finally {
      setLoading(false);
    }
  }

  @override
  Future<bool> saveSettings() async {
    try {
      setLoading(true);

      // TODO: Save email settings to persistent storage
      final prefs = Get.find<dynamic>(); // Replace with your storage solution
      // await prefs.setString('current_email', currentEmail.value);
      // await prefs.setBool('receive_newsletter', receiveNewsletter.value);
      // await prefs.setBool('receive_promotions', receivePromotions.value);
      // await prefs.setBool('is_email_verified', isEmailVerified.value);

      return true;
    } catch (e) {
      setError('Failed to save email settings: ${e.toString()}');
      return false;
    } finally {
      setLoading(false);
    }
  }

  @override
  Future<void> loadSettings() async {
    try {
      setLoading(true);

      // TODO: Load email settings from persistent storage
      final prefs = Get.find<dynamic>(); // Replace with your storage solution
      // currentEmail.value = prefs.getString('current_email') ?? "anitarose@example.com";
      // receiveNewsletter.value = prefs.getBool('receive_newsletter') ?? true;
      // receivePromotions.value = prefs.getBool('receive_promotions') ?? false;
      // isEmailVerified.value = prefs.getBool('is_email_verified') ?? true;
    } catch (e) {
      setError('Failed to load email settings: ${e.toString()}');
    } finally {
      setLoading(false);
    }
  }
}
