import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../repository/get_sample_repository.dart';

import '../../repository/result_delivery_repository.dart';

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

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

class SampleDeliverPersonStateInit extends SampleDeliverPersonState {
  SampleDeliverPersonStateInit() : super(DateTime.now());
}

class SampleDeliverPersonStateLoading extends SampleDeliverPersonState {
  SampleDeliverPersonStateLoading() : super(DateTime.now());
}

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

class SampleDeliverPersonStateDone extends SampleDeliverPersonState {
  final bool model;
  SampleDeliverPersonStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class SampleDeliverPersonNotifier
    extends StateNotifier<SampleDeliverPersonState> {
  final Ref ref;
  SampleDeliverPersonNotifier({
    required this.ref,
  }) : super(SampleDeliverPersonStateInit());

  void setData({
    required String id,
    required String name,
    required String note,
    // Inputan INFO
    required String jumlahPasien,
    required String edtaRutin,
    required String darahCitras,
    required String serum,
    required String urine,
    required String pleura,
    required String otherTextBahan,
    required String totalOther,
  }) async {
    try {
      state = SampleDeliverPersonStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await GetSampleRepository(dio: dio).setDeliveryPerson(
        id: id,
        name: name,
        note: note,
        // INPUTAN INFO
        jumlahPasien: jumlahPasien,
        edtaRutin: edtaRutin,
        darahCitras: darahCitras,
        serum: serum,
        urine: urine,
        pleura: pleura,
        otherTextBahan: otherTextBahan,
        totalOther: totalOther
      );
      state = SampleDeliverPersonStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = SampleDeliverPersonStateError(message: e.message.toString());
      } else {
        state = SampleDeliverPersonStateError(message: e.toString());
      }
    }
  }
}

//provider
final SampleDeliverPersonProvider = StateNotifierProvider<
    SampleDeliverPersonNotifier,
    SampleDeliverPersonState>((ref) => SampleDeliverPersonNotifier(ref: ref));
