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

import 'package:ticket_booth/provider/dio_provider.dart';
import 'package:ticket_booth/repository/base_repository.dart';

import 'package:ticket_booth/repository/queue_repository.dart';

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

class QueueStateInit extends QueueState {
  QueueStateInit() : super(DateTime.now());
}

class QueueStateLoading extends QueueState {
  QueueStateLoading() : super(DateTime.now());
}

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

class QueueStateDone extends QueueState {
  final Map<String, dynamic> response;
  QueueStateDone({
    required this.response,
  }) : super(DateTime.now());
}

//notifier
class QueueNotifier extends StateNotifier<QueueState> {
  final Ref ref;
  QueueNotifier({
    required this.ref,
  }) : super(QueueStateInit());

  void get(
      {required service_id,
      required booth_id,
      required hostIp,
      required branchID}) async {
    try {
      state = QueueStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await QueueRepository(dio: dio).getData(
          service_id: service_id,
          booth_id: booth_id,
          hostIp: hostIp,
          branchID: branchID);
      state = QueueStateDone(response: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = QueueStateError(message: e.message);
      } else {
        state = QueueStateError(message: e.toString());
      }
    }
  }
}

//provider
final QueueProvider = StateNotifierProvider<QueueNotifier, QueueState>(
    (ref) => QueueNotifier(ref: ref));
