import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:queuedisplay/model/sampling_location_model.dart';
import 'package:queuedisplay/provider/dio_provider.dart';
import 'package:queuedisplay/repository/sampling_location_repository.dart';

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

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

class SamplingLocationListInit extends SamplingLocationList {
  SamplingLocationListInit() : super(DateTime.now());
}

class SamplingLocationListLoading extends SamplingLocationList {
  SamplingLocationListLoading() : super(DateTime.now());
}

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

class SamplingLocationListDone extends SamplingLocationList {
  final List<SamplingLocation> model;
  SamplingLocationListDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class SamplingLocationListNotifier extends StateNotifier<SamplingLocationList> {
  final Ref ref;
  SamplingLocationListNotifier({
    required this.ref,
  }) : super(SamplingLocationListInit());

  void list() async {
    try {
      state = SamplingLocationListLoading();
      final dio = ref.read(dioProvider);
      final resp = await SamplingLocationRepository(dio: dio).getData();
      state = SamplingLocationListDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = SamplingLocationListError(message: e.message);
      } else {
        state = SamplingLocationListError(message: e.toString());
      }
    }
  }
}

//provider
final SamplingLocationProvider =
    StateNotifierProvider<SamplingLocationListNotifier, SamplingLocationList>(
        (ref) => SamplingLocationListNotifier(ref: ref));
