import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../models/personal_information_model.dart';
import '../../repository/change_password_repository.dart';
import '../../repository/personal_information_repository.dart';
import '../../repository/work_list_repository.dart';

import '../../models/pending_work_model.dart';
import '../../models/response_save_change_password_model.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/home_screen_repository.dart';

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 ResponseSaveChangePasswordModel model;
  ChangePasswordStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class ChangePasswordNotifier extends StateNotifier<ChangePasswordState> {
  final Ref ref;
  ChangePasswordNotifier({
    required this.ref,
  }) : super(ChangePasswordStateInit());

  void ubahPassword({
    required String userID,
    required String oldPassword,
    required String newPassword,
  }) async {
    try {
      state = ChangePasswordStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await ChangePasswordRepository(dio: dio)
          .ubahPassword(
            userID: userID,
            oldPassword: oldPassword,
            newPassword: newPassword
          );
      state = ChangePasswordStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = ChangePasswordStateError(message: e.message.toString());
      } else {
        state = ChangePasswordStateError(message: e.toString());
      }
    }
  }
}

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