import 'dart:ui';

import 'package:get/get.dart';
import 'package:taafee_mobile/common/const/const.dart';
import 'package:taafee_mobile/core/local_storage/local_storage.dart';
import 'package:taafee_mobile/features/account/data_layer/model/change_password.dart';
import 'package:taafee_mobile/features/account/data_layer/model/feedback.dart';
import 'package:taafee_mobile/features/account/data_layer/source/account_service.dart';
import 'package:taafee_mobile/features/auth/data_layer/source/auth_service.dart';
import 'package:taafee_mobile/features/card/data_layer/model/card_model.dart';
import 'package:rx_future/rx_future.dart';
import '../data_layer/model/edit_account.dart';

class AccountController extends GetxController {
  AccountService accountService = AccountService();
  AuthService authService = AuthService();
  LocalStorage storage = LocalStorage();

  /// ------------------edit profile-----------------///
  EditAccountModel editAccountModel = EditAccountModel();
  RxFuture<void> editAccountState = RxFuture(null);

  Future<void> editProfile({
    void Function()? onSuccess,
    void Function(Object)? onError,
    void Function(Object)? onConnectionError,
  }) async {
    editAccountState.observe((value) async {
      await accountService.editProfile(editAccountModel);
    }, onSuccess: (value) async {
      await authService
          .showUser(onConnectionError: onConnectionError)
          .then((user) {
        storage.saveUser(user);
      });
      onSuccess?.call();
    }, onError: onError);
  }

  ///------------------change password----------------///
  ChangePasswordModel changePasswordModel = ChangePasswordModel.zero();
  RxFuture<void> changePasswordState = RxFuture(null);

  Future<void> changePassword({
    void Function()? onSuccess,
    void Function(Object)? onError,
  }) {
    return changePasswordState.observe(
      (value) async {
        return await accountService.changePassword(changePasswordModel);
      },
      onSuccess: (value) {
        onSuccess?.call();
      },
      onError: onError,
    );
  }

  ///----------select language-------///
  RxString selectedLanguageSvg = 'english'.obs;

  void getSelectedLanguageIcon() {
    String selectedLanguage = storage.getLanguage() ?? 'en';
    switch (selectedLanguage) {
      case 'en':
        selectedLanguageSvg.value = 'english';
        break;
      case 'ar':
        selectedLanguageSvg.value = 'arabic';
        break;
    }
    selectedLanguageSvg.refresh();
  }

  Future<void> changeLanguage(Languages language) async {
    if (language == Languages.english) {
      selectedLanguageSvg.value = 'english';
    }
    if (language == Languages.arabic) {
      selectedLanguageSvg.value = 'arabic';
    }

    Get.updateLocale(Locale(language.code));
    String languageCode = language.code;
    await storage.saveLanguage(languageCode);
    selectedLanguageSvg.refresh();
  }

  ///----------------user card-----------///
  RxFuture<List<CardModel>> userCardsState = RxFuture(<CardModel>[]);
  void getUserCards() {
    userCardsState.observe((p0) async {
      return await accountService.getUserCards();
    });
  }

  ///----------------feedbackmessage-----------///
  RxFuture<void> sendFeedbackState = RxFuture(null);
  FeedbackMessage feedbackMessageModel = FeedbackMessage(message: '');
  Future<void> sendFeedback(
      {void Function(void)? onSuccess, void Function(Object)? onError}) async {
    sendFeedbackState.observe(
      (p0) async {
        await accountService.sendFeedback(feedbackMessageModel);
      },
      onSuccess: onSuccess,
      onError: onError,
    );
  }
}
