import 'package:account/src/features/delete_account/domain/delete_account_use_case.dart';
import 'package:account/src/features/delete_account/presentation/providers/delete_account_use_case_provider.dart';
import 'package:account/src/features/delete_account/presentation/state/delete_account_view_state.dart';
import 'package:account/src/features/linked_devices/domain/get_linked_devices_use_case.dart';
import 'package:account/src/features/linked_devices/presentation/providers/get_linked_devices_use_case_provider.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 deleteAccountViewModelProvider =
NotifierProvider.autoDispose<DeleteAccountViewModel, DeleteAccountViewState>(
  DeleteAccountViewModel.new,
);

class DeleteAccountViewModel extends Notifier<DeleteAccountViewState> {
  late final DeleteAccountUseCase _deleteAccountUseCase;
  late final GetLinkedDevicesUseCase _getLinkedDevicesUseCase;
  late final TextEditingController passwordController;

  @override
  DeleteAccountViewState build() {
    _deleteAccountUseCase = ref.read(deleteAccountUseCaseProvider);
    _getLinkedDevicesUseCase = ref.read(getLinkedDevicesUseCaseProvider);

    passwordController = TextEditingController();
    passwordController.addListener(_onPasswordChanged);

    ref.read(loggedUserProvider.future)
      .then((user){
        setUser(user);
        return _getLinkedDevicesUseCase.getLinkedDevices(userId: user.id);
      }).then(setDevices);

    ref.onDispose(disposeListeners);

    return const DeleteAccountViewState();
  }

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

  void setDevices(List<DeviceEntity> devices) {
    state = state.copyWith(
      devices: devices,
      deleteDevices: List<bool>.generate(devices.length, (_)=>false),
    );
  }

  void updateDeleteDevice(int index) {
    List<bool> deleteDevices = state.deleteDevices;
    deleteDevices[index] = !deleteDevices[index];

    state = state.copyWith(
      deleteDevices: deleteDevices,
    );
  }

  void _onPasswordChanged() {
    final value = passwordController.text;
    if (value == state.password) return;

    state = state.copyWith(
      password: value,
      errorMessage: ''
    );
  }

  bool _validateForm() {
    if (state.password.trim().isEmpty) {
      state = state.copyWith(errorMessage: 'errorMessagePasswordIsEmpty');
      return false;
    }
    /*if (state.password.trim() != state.loggedUser.password.trim()) {
      state = state.copyWith(errorMessage: 'errorMessagePasswordsDontMatch');
      return false;
    }*/
    return true;
  }

  void nextStep() {
    final step = state.confirmStep;

    if (step == 0 && !_validateForm()) return;

    state = state.copyWith(confirmStep: step + 1);
  }

  void resetConfirmStep() {
    state = state.copyWith(confirmStep: 0);
  }

  Future<bool> deleteAccount() async {
    if (state.isLoading) return false;

    try {
      state = state.copyWith(isLoading: true);

      await _deleteAccountUseCase.deleteAccount(userId: state.loggedUser!.id);
      if (!ref.mounted) return false;

      ref.invalidate(loggedUserProvider);

      state = state.copyWith(isLoading: false, isDeleted: true);
      return true;
    } catch (e) {
      state = state.copyWith(isLoading: false);
      if (!ref.mounted) return false;
      _finishWithError(message: e.toString());
      return false;
    }

  }

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

  void disposeListeners() {
    passwordController.removeListener(_onPasswordChanged);
    passwordController.dispose();
  }
}