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

class PrivacyController extends BaseSettingsController {
  // Privacy settings
  final RxBool showActivityStatus = true.obs;
  final RxBool allowComments = false.obs;
  final RxBool shareUsageData = true.obs;
  final RxBool allowPersonalizedAds = false.obs;

  // Profile visibility options
  final RxString profileVisibility = 'Public'.obs;
  final List<String> visibilityOptions = ['Public', 'Friends', 'Private'];

  // Messaging permissions
  final RxString messagingPermission = 'Everyone'.obs;
  final List<String> messagingOptions = ['Everyone', 'Friends', 'Nobody'];

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

  // Toggle privacy setting
  void togglePrivacySetting(String setting, bool value) {
    switch (setting) {
      case 'activity_status':
        showActivityStatus.value = value;
        break;
      case 'allow_comments':
        allowComments.value = value;
        break;
      case 'share_usage_data':
        shareUsageData.value = value;
        break;
      case 'personalized_ads':
        allowPersonalizedAds.value = value;
        break;
    }

    saveSettings();
  }

  // Change profile visibility
  void changeProfileVisibility(String visibility) {
    if (visibilityOptions.contains(visibility)) {
      profileVisibility.value = visibility;
      saveSettings();
    }
  }

  // Change messaging permissions
  void changeMessagingPermission(String permission) {
    if (messagingOptions.contains(permission)) {
      messagingPermission.value = permission;
      saveSettings();
    }
  }

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

      // TODO: Save privacy settings to persistent storage
      final prefs = Get.find<dynamic>(); // Replace with your storage solution
      // await prefs.setBool('show_activity_status', showActivityStatus.value);
      // await prefs.setBool('allow_comments', allowComments.value);
      // await prefs.setBool('share_usage_data', shareUsageData.value);
      // await prefs.setBool('allow_personalized_ads', allowPersonalizedAds.value);
      // await prefs.setString('profile_visibility', profileVisibility.value);
      // await prefs.setString('messaging_permission', messagingPermission.value);

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

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

      // TODO: Load privacy settings from persistent storage
      final prefs = Get.find<dynamic>(); // Replace with your storage solution
      // showActivityStatus.value = prefs.getBool('show_activity_status') ?? true;
      // allowComments.value = prefs.getBool('allow_comments') ?? false;
      // shareUsageData.value = prefs.getBool('share_usage_data') ?? true;
      // allowPersonalizedAds.value = prefs.getBool('allow_personalized_ads') ?? false;
      // profileVisibility.value = prefs.getString('profile_visibility') ?? 'Public';
      // messagingPermission.value = prefs.getString('messaging_permission') ?? 'Everyone';
    } catch (e) {
      setError('Failed to load privacy settings: ${e.toString()}');
    } finally {
      setLoading(false);
    }
  }
}
