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 SampleReceivePersonState extends Equatable {
  final DateTime date;
  const SampleReceivePersonState(this.date);
  @override
  List<Object?> get props => [date];
}

class SampleReceivePersonStateInit extends SampleReceivePersonState {
  SampleReceivePersonStateInit() : super(DateTime.now());
}

class SampleReceivePersonStateLoading extends SampleReceivePersonState {
  SampleReceivePersonStateLoading() : super(DateTime.now());
}

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

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

//notifier
class SampleReceivePersonNotifier
    extends StateNotifier<SampleReceivePersonState> {
  final Ref ref;
  SampleReceivePersonNotifier({
    required this.ref,
  }) : super(SampleReceivePersonStateInit());

  void setData(
      {required String id,
      required String name,
      required String note,
      required String suhuSample}) async {
    try {
      state = SampleReceivePersonStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await GetSampleRepository(dio: dio).setReceivePerson(
        id: id,
        name: name,
        note: note,
        suhuSample: suhuSample,
      );
      state = SampleReceivePersonStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = SampleReceivePersonStateError(message: e.message.toString());
      } else {
        state = SampleReceivePersonStateError(message: e.toString());
      }
    }
  }
}

//provider
final SampleReceivePersonProvider = StateNotifierProvider<
    SampleReceivePersonNotifier,
    SampleReceivePersonState>((ref) => SampleReceivePersonNotifier(ref: ref));
