import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mitra_corporate/model/registration_filter_model.dart';
import 'package:mitra_corporate/repository/registration_repository.dart';

import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';

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

class GetFilterRegistrationStateInit extends GetFilterRegistrationState {
  GetFilterRegistrationStateInit() : super(DateTime.now());
}

class GetFilterRegistrationStateLoading extends GetFilterRegistrationState {
  GetFilterRegistrationStateLoading() : super(DateTime.now());
}

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

class GetFilterRegistrationStateDone extends GetFilterRegistrationState {
  final RegistrationFilterModel model;
  GetFilterRegistrationStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetFilterRegistrationNotifier
    extends StateNotifier<GetFilterRegistrationState> {
  final Ref ref;
  GetFilterRegistrationNotifier({
    required this.ref,
  }) : super(GetFilterRegistrationStateInit());

  void getData({required String token}) async {
    try {
      state = GetFilterRegistrationStateLoading();
      final dio = ref.read(dioProvider);
      final resp =
          await RegistrationRepository(dio: dio).getFilter(token: token);
      state = GetFilterRegistrationStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = GetFilterRegistrationStateError(message: e.message.toString());
      } else {
        state = GetFilterRegistrationStateError(message: e.toString());
      }
    }
  }
}

//provider
final GetFilterRegistrationProvider = StateNotifierProvider<
        GetFilterRegistrationNotifier, GetFilterRegistrationState>(
    (ref) => GetFilterRegistrationNotifier(ref: ref));
