import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../models/response_save_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 pengantaranHasilPasienSave = StateNotifierProvider<
        PengantaranhasilPasienSaveNotifier, PengantaranhasilPasienSaveState>(
    (ref) => PengantaranhasilPasienSaveNotifier(ref: ref));

// 2. notifier
class PengantaranhasilPasienSaveNotifier
    extends StateNotifier<PengantaranhasilPasienSaveState> {
  final Ref ref;
  PengantaranhasilPasienSaveNotifier({required this.ref})
      : super(PengantaranhasilPasienSaveStateInit());
  void pengantaranHasilPasienSave({
    required String courierID,
    required String tipeID,
    required String deliveryID,
  }) async {
    try {
      state = PengantaranhasilPasienSaveStateLoading();
      final resp =
          await PengantaranHasilPasienRepository(dio: ref.read(dioProvider))
              .pengantaranHasilPasienSave(
                courierID: courierID,
                tipeID: tipeID,
                deliveryID: deliveryID
              );

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

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

class PengantaranhasilPasienSaveStateInit
    extends PengantaranhasilPasienSaveState {
  PengantaranhasilPasienSaveStateInit() : super(DateTime.now());
}

class PengantaranhasilPasienSaveStateLoading
    extends PengantaranhasilPasienSaveState {
  PengantaranhasilPasienSaveStateLoading() : super(DateTime.now());
}

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

class PengantaranhasilPasienSaveStateDone
    extends PengantaranhasilPasienSaveState {
  final ResponseSavePengantaranHasilModel model;
  PengantaranhasilPasienSaveStateDone({
    required this.model,
  }) : super(DateTime.now());
}
