import 'package:account/src/features/change_password/domain/change_password_use_case.dart';
import 'package:account/src/features/change_password/domain/models/entities/change_password_request_entity.dart';
import 'package:account/src/features/change_password/presentation/providers/change_password_use_case_provider.dart';
import 'package:account/src/features/change_password/presentation/state/change_password_view_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:legacy_shared/legacy_shared.dart';

// import 'package:sf_localizations/sf_localizations.dart';

final changePasswordViewModelProvider =
NotifierProvider.autoDispose<ChangePasswordViewModel, ChangePasswordViewState>(
  ChangePasswordViewModel.new,
);

class ChangePasswordViewModel extends Notifier<ChangePasswordViewState> {
  late final ChangePasswordUseCase _changePasswordUseCase;

  late final TextEditingController currentPasswordController;
  late final TextEditingController newPasswordController;
  late final TextEditingController repeatPasswordController;
  late final TextEditingController passwordController;

  @override
  ChangePasswordViewState build() {
    _changePasswordUseCase = ref.read(changePasswordUseCaseProvider);

    currentPasswordController = TextEditingController();
    currentPasswordController.addListener(_onCurrentPasswordChanged);

    newPasswordController = TextEditingController();
    newPasswordController.addListener(_onNewPasswordChanged);

    repeatPasswordController = TextEditingController();
    repeatPasswordController.addListener(_onRepeatPasswordController);

    ref.read(loggedUserProvider.future)
        .then(setUser);

    ref.onDispose(disposeControllers);

    return const ChangePasswordViewState();
  }

  void setUser(UserEntity user) {
    state = state.copyWith(loggedUser: user);
  }

  void _onCurrentPasswordChanged() {
    final value = currentPasswordController.text;

    if (value == state.currentPassword) return;

    state = state.copyWith(
      currentPassword: value,
    );
  }

  void _onNewPasswordChanged() {
    final value = newPasswordController.text;

    if (value == state.newPassword) return;

    state = state.copyWith(
      newPassword: value,
    );
  }

  void _onRepeatPasswordController() {
    final value = repeatPasswordController.text;

    if (value == state.repeatPassword) return;

    state = state.copyWith(
      repeatPassword: value,
    );
  }

  bool _validateForm() {
    if (state.currentPassword.trim().isEmpty){
    state = state.copyWith(errorMessage: 'errorMessageCurrentPasswordIsEmpty');
    return false;
    }
    if (state.newPassword.trim().isEmpty){
      state = state.copyWith(errorMessage: 'errorMessageNewPasswordIsEmpty');
      return false;
    }
    if (state.repeatPassword.trim().isEmpty){
      state = state.copyWith(errorMessage: 'errorMessageRepeatPasswordIsEmpty');
      return false;
    }
    if (state.newPassword.trim() != state.repeatPassword.trim()){
      state = state.copyWith(errorMessage: 'errorMessagePasswordsDontMatch');
      return false;
    }
    return true;
  }

  ChangePasswordRequestEntity _toRequest() {

    return ChangePasswordRequestEntity(
      password: state.newPassword.trim(),
    );
  }

  Future<bool> changePassword() async {
    if (state.isLoading) return false;
    if (!_validateForm()) return false;

    try {
      final request = _toRequest();

      await _changePasswordUseCase.changePassword(userId: state.loggedUser!.id, request: request);
      ref.invalidate(loggedUserProvider);

      return true;
    } catch (e) {
      if (!ref.mounted) return false;
      _finishWithError(message: e.toString());
      return false;
    }

  }

  void _finishWithError({required String message}) {
    state = state.copyWith(
      isLoading: false,
      errorMessage: message,
    );
  }

  void disposeControllers() {
    currentPasswordController.removeListener(_onCurrentPasswordChanged);
    currentPasswordController.dispose();

    newPasswordController.removeListener(_onNewPasswordChanged);
    newPasswordController.dispose();

    repeatPasswordController.removeListener(_onRepeatPasswordController);
    repeatPasswordController.dispose();

  }
}