import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/di/chicken_di.dart';
import 'package:rasadyar_chicken/data/models/request/change_password/change_password_request_model.dart';
import 'package:rasadyar_chicken/data/models/response/iran_province_city/iran_province_city_model.dart';
import 'package:rasadyar_chicken/data/models/response/user_profile/user_profile.dart';
import 'package:rasadyar_chicken/data/repositories/chicken/chicken_repository.dart';
import 'package:rasadyar_core/core.dart';

class ProfileLogic extends GetxController {
  ChickenRepository chickenRepository = diChicken.get<ChickenRepository>();
  GService gService = Get.find<GService>();
  TokenStorageService tokenService = Get.find<TokenStorageService>();
  RxInt selectedInformationType = 0.obs;
  Rxn<Jalali> birthDate = Rxn<Jalali>();

  Rx<Resource<UserProfile>> userProfile = Rx<Resource<UserProfile>>(Resource.loading());
  Rx<Resource<UserLocalModel>> userLocal = Rx<Resource<UserLocalModel>>(Resource.loading());

  TextEditingController nameController = TextEditingController();
  TextEditingController lastNameController = TextEditingController();
  TextEditingController nationalCodeController = TextEditingController();
  TextEditingController nationalIdController = TextEditingController();
  TextEditingController birthdayController = TextEditingController();

  TextEditingController oldPasswordController = TextEditingController();
  TextEditingController newPasswordController = TextEditingController();
  TextEditingController retryNewPasswordController = TextEditingController();

  RxList<IranProvinceCityModel> cites = <IranProvinceCityModel>[].obs;
  Rxn<IranProvinceCityModel> selectedProvince = Rxn();
  Rxn<IranProvinceCityModel> selectedCity = Rxn();

  GlobalKey<FormState> formKey = GlobalKey();
  ImagePicker imagePicker = ImagePicker();
  Rxn<XFile> selectedImage = Rxn<XFile>();
  RxnString _base64Image = RxnString();
  RxBool isOnLoading = false.obs;

  @override
  void onInit() {
    super.onInit();
    ever(selectedImage, (data) async {
      if (data?.path != null) {
        _base64Image.value = await convertImageToBase64(data!.path);
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
    getUserProfile();
    getUserRole();
    selectedProvince.listen((p0) => getCites());
    userProfile.listen((data) {
      nameController.text = data.data?.firstName ?? '';
      lastNameController.text = data.data?.lastName ?? '';
      nationalCodeController.text = data.data?.nationalCode ?? '';
      nationalIdController.text = data.data?.nationalId ?? '';
      birthdayController.text = data.data?.birthday?.toJalali.formatCompactDate() ?? '';
      birthDate.value = data.data?.birthday?.toJalali;
      selectedProvince.value = IranProvinceCityModel(
        name: data.data?.province ?? '',
        id: data.data?.provinceNumber ?? 0,
      );

      selectedCity.value = IranProvinceCityModel(
        name: data.data?.city ?? '',
        id: data.data?.cityNumber ?? 0,
      );
    });
  }

  @override
  void onClose() {
    super.onClose();
  }

  Future<void> getUserProfile() async {
    userProfile.value = Resource.loading();
    await safeCall<UserProfile?>(
      call: () async =>
          await chickenRepository.getUserProfile(token: tokenService.accessToken.value!),
      onSuccess: (result) {
        if (result != null) {
          userProfile.value = Resource.success(result);
        }
      },
      onError: (error, stackTrace) {},
    );
  }

  Future<void> getCites() async {
    await safeCall(
      call: () => chickenRepository.getCity(provinceName: selectedProvince.value?.name ?? ''),
      onSuccess: (result) {
        if (result != null && result.isNotEmpty) {
          cites.value = result;
        }
      },
    );
  }

  Future<void> updateUserProfile() async {
    UserProfile userProfile = UserProfile(
      firstName: nameController.text,
      lastName: lastNameController.text,
      nationalCode: nationalCodeController.text,
      nationalId: nationalIdController.text,
      birthday: birthDate.value?.toDateTime().formattedDashedGregorian.toString(),
      image: _base64Image.value,
      personType: 'self',
      type: 'self_profile',
    );
    isOnLoading.value = true;
    await safeCall(
      call: () async => await chickenRepository.updateUserProfile(
        token: tokenService.accessToken.value!,
        userProfile: userProfile,
      ),
      onSuccess: (result) {
        isOnLoading.value = false;
      },
      onError: (error, stackTrace) {
        isOnLoading.value = false;
      },
    );
  }

  Future<void> updatePassword() async {
    if (formKey.currentState?.validate() ?? false) {
      ChangePasswordRequestModel model = ChangePasswordRequestModel(
        username: userProfile.value.data?.mobile,
        password: newPasswordController.text,
      );

      await safeCall(
        call: () async => await chickenRepository.updatePassword(
          token: tokenService.accessToken.value!,
          model: model,
        ),
      );
    }
  }

  Future<void> getUserRole() async {
    userLocal.value = Resource.loading();
    await safeCall<UserLocalModel?>(
      call: () async => tokenService.getUserLocal(Module.chicken),
      onSuccess: (result) {
        if (result != null) {
          userLocal.value = Resource.success(result);
        }
      },
      onError: (error, stackTrace) {},
    );
  }

  void clearPasswordForm() {
    oldPasswordController.clear();
    newPasswordController.clear();
    retryNewPasswordController.clear();
  }

  Future<void> changeUserRole(String newRole) async {
    dLog(newRole);
    await gService.saveRoute(Module.chicken, newRole);

    Get.offAllNamed(newRole);
  }
}
