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

import '../model/layanan_dokter.dart';
import '../repository/base_repository.dart';
import '../repository/service_repository_layanan_dokter.dart';
import 'dio_provider.dart';

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

class LayananListStateInit extends LayananListState {
  LayananListStateInit() : super(DateTime.now());
}

class LayananListStateLoading extends LayananListState {
  LayananListStateLoading() : super(DateTime.now());
}

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

class LayananListStateDone extends LayananListState {
  final List<LayananDokter> model;
  LayananListStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class LayananListNotifier extends StateNotifier<LayananListState> {
  final Ref ref;
  LayananListNotifier({
    required this.ref,
  }) : super(LayananListStateInit());

  void list() async {
    try {
      state = LayananListStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await ServiceRepositoryLayananDokter(dio: dio).getData();
      state = LayananListStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = LayananListStateError(message: e.message);
      } else {
        state = LayananListStateError(message: e.toString());
      }
    }
  }
}

//provider
final layananProvider =
    StateNotifierProvider<LayananListNotifier, LayananListState>(
        (ref) => LayananListNotifier(ref: ref));
