import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../provider/dio_provider.dart';
import '../../repository/auth_repository.dart';
import '../../repository/base_repository.dart';

// 3. state provider
final ChangePasswordProvider =
    StateNotifierProvider<ChangePasswordNotifier, ChangePasswordState>(
        (ref) => ChangePasswordNotifier(ref: ref));

// 2. notifier
class ChangePasswordNotifier extends StateNotifier<ChangePasswordState> {
  final Ref ref;
  ChangePasswordNotifier({required this.ref})
      : super(ChangePasswordStateInit());
  void ChangePassword({
    required String token,
    required String current_password,
    required String new_password,
    required String password_confirmation,
  }) async {
    try {
      state = ChangePasswordStateLoading();
      final resp = await AuthRepository(dio: ref.read(dioProvider))
          .changePassword(
              token: token,
              current_password: current_password,
              new_password: new_password,
              password_confirmation: password_confirmation);

      // print(resp);
      state = ChangePasswordStateDone(message: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = ChangePasswordStateError(message: e.message ?? "");
      } else {
        state = ChangePasswordStateError(message: e.toString());
      }
    }
  }
}

// 1. state
abstract class ChangePasswordState extends Equatable {
  final DateTime date;
  const ChangePasswordState(this.date);
  @override
  List<Object?> get props => [date];
}

class ChangePasswordStateInit extends ChangePasswordState {
  ChangePasswordStateInit() : super(DateTime.now());
}

class ChangePasswordStateLoading extends ChangePasswordState {
  ChangePasswordStateLoading() : super(DateTime.now());
}

class ChangePasswordStateError extends ChangePasswordState {
  final String message;
  ChangePasswordStateError({
    required this.message,
  }) : super(DateTime.now());
}

class ChangePasswordStateDone extends ChangePasswordState {
  final String message;
  ChangePasswordStateDone({
    required this.message,
  }) : super(DateTime.now());
}
