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

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

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

class GetSpecimenStateInit extends GetSpecimenState {
  GetSpecimenStateInit() : super(DateTime.now());
}

class GetSpecimenStateLoading extends GetSpecimenState {
  GetSpecimenStateLoading() : super(DateTime.now());
}

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

class GetSpecimenStateDone extends GetSpecimenState {
  final List<GetSpecimenModel> model;
  GetSpecimenStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetSpecimenNotifier extends StateNotifier<GetSpecimenState> {
  final Ref ref;
  GetSpecimenNotifier({
    required this.ref,
  }) : super(GetSpecimenStateInit());

  void getData(
      {required List<Map<String, String>> arrTest,
      required String token}) async {
    try {
      state = GetSpecimenStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await RegistrationRepository(dio: dio)
          .getSpecimen(arrTest: arrTest, token: token);
      state = GetSpecimenStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = GetSpecimenStateError(message: e.message.toString());
      } else {
        state = GetSpecimenStateError(message: e.toString());
      }
    }
  }
}

//provider
final GetSpecimenProvider =
    StateNotifierProvider<GetSpecimenNotifier, GetSpecimenState>(
        (ref) => GetSpecimenNotifier(ref: ref));
