import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../model/display_counter_dedicated_modelv2.dart';
import '../repository/base_repository.dart';
import '../repository/service_repository.dart';
import 'dio_provider.dart';

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

class DisplayCounterDedicatedStateInit extends DisplayCounterDedicatedState {
  DisplayCounterDedicatedStateInit() : super(DateTime.now());
}

class DisplayCounterDedicatedStateLoading extends DisplayCounterDedicatedState {
  DisplayCounterDedicatedStateLoading() : super(DateTime.now());
}

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

class DisplayCounterDedicatedStateDone extends DisplayCounterDedicatedState {
  // final List<DisplayCounterDedicatedModel> model;
  final List<DisplayCounterDedicatedModelV2> model;
  DisplayCounterDedicatedStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class DisplayLayananNotifier
    extends StateNotifier<DisplayCounterDedicatedState> {
  final Ref ref;
  DisplayLayananNotifier({
    required this.ref,
  }) : super(DisplayCounterDedicatedStateInit());

  void listDisplayByCounterID(List<int> counterID, String branchID) async {
    try {
      state = DisplayCounterDedicatedStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await ServiceRepository(dio: dio)
          .getDataByCounterID(counterID, branchID);
      state = DisplayCounterDedicatedStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        if (e.message == "XMLHttpRequest error." ||
            e.message == "XMLHttpRequest error.") {
          state =
              DisplayCounterDedicatedStateError(message: "Connection Error");
        } else {
          state = DisplayCounterDedicatedStateError(message: e.message);
        }
      } else {
        state = DisplayCounterDedicatedStateError(message: e.toString());
      }
    }
  }
}

//provider
final displayProvider =
    StateNotifierProvider<DisplayLayananNotifier, DisplayCounterDedicatedState>(
        (ref) => DisplayLayananNotifier(ref: ref));
