import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/response_search_pengantaran_hasil_model.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/pengantaran_hasil_pasien_repository.dart';

// 3. state provider
final pengantaranHasilPasienSearchNoLab = StateNotifierProvider<PengantaranhasilPasienNotifier, PengantaranhasilPasienState>(
    (ref) => PengantaranhasilPasienNotifier(ref: ref));

// 2. notifier
class PengantaranhasilPasienNotifier extends StateNotifier<PengantaranhasilPasienState> {
  final Ref ref;
  PengantaranhasilPasienNotifier({required this.ref}) : super(PengantaranhasilPasienStateInit());
  void loadListNoLabPengantaranHasilPasien(
      {
        required String search
      }) async {
    try {
      state = PengantaranhasilPasienStateLoading();
      final resp = await PengantaranHasilPasienRepository(dio: ref.read(dioProvider))
          .loadListNoLabPengantaranHasilPasien(search: search);

      state = PengantaranhasilPasienStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = PengantaranhasilPasienStateError(message: e.message ?? "");
      } else {
        state = PengantaranhasilPasienStateError(message: e.toString());
      }
    }
  }
}

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

class PengantaranhasilPasienStateInit extends PengantaranhasilPasienState {
  PengantaranhasilPasienStateInit() : super(DateTime.now());
}

class PengantaranhasilPasienStateLoading extends PengantaranhasilPasienState {
  PengantaranhasilPasienStateLoading() : super(DateTime.now());
}

class PengantaranhasilPasienStateError extends PengantaranhasilPasienState {
  final String? message;
  PengantaranhasilPasienStateError({
    required this.message,
  }) : super(DateTime.now());
}

class PengantaranhasilPasienStateDone extends PengantaranhasilPasienState {
  final List<ResponseSearchPengantaranHasilModel> model;
  PengantaranhasilPasienStateDone({
    required this.model,
  }) : super(DateTime.now());
}
