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_instansi_repository.dart';
import '../../repository/pengantaran_hasil_pasien_repository.dart';

// 3. state provider
final pengantaranHasilInstansi = StateNotifierProvider<PengantaranhasilInstansiNotifier, PengantaranhasilInstansiState>(
    (ref) => PengantaranhasilInstansiNotifier(ref: ref));

// 2. notifier
class PengantaranhasilInstansiNotifier extends StateNotifier<PengantaranhasilInstansiState> {
  final Ref ref;
  PengantaranhasilInstansiNotifier({required this.ref}) : super(PengantaranhasilInstansiStateInit());
  void loadListNoSuratJalanPengantaranHasilInstansi(
      {
        required String search
      }) async {
    try {
      state = PengantaranhasilInstansiStateLoading();
      final resp = await PengantaranHasilInstansiRepository(dio: ref.read(dioProvider))
          .loadListNoSuratJalanPengantaranHasilInstansi(search: search);

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

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

class PengantaranhasilInstansiStateInit extends PengantaranhasilInstansiState {
  PengantaranhasilInstansiStateInit() : super(DateTime.now());
}

class PengantaranhasilInstansiStateLoading extends PengantaranhasilInstansiState {
  PengantaranhasilInstansiStateLoading() : super(DateTime.now());
}

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

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