import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:ticket_booth/model/booth.dart';
import 'package:ticket_booth/provider/dio_provider.dart';
import 'package:ticket_booth/repository/base_repository.dart';
import 'package:ticket_booth/repository/booth_repository.dart';

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

class BoothListStateInit extends BoothListState {
  BoothListStateInit() : super(DateTime.now());
}

class BoothListStateLoading extends BoothListState {
  BoothListStateLoading() : super(DateTime.now());
}

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

class BoothListStateDone extends BoothListState {
  final List<Booth> model;
  BoothListStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class BoothListNotifier extends StateNotifier<BoothListState> {
  final Ref ref;
  BoothListNotifier({
    required this.ref,
  }) : super(BoothListStateInit());

  void list({required hostIp}) async {
    try {
      state = BoothListStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await BoothRepository(dio: dio).getData(hostIP: hostIp);
      state = BoothListStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = BoothListStateError(message: e.message);
      } else {
        state = BoothListStateError(message: e.toString());
      }
    }
  }
}

//provider
final BoothListProvider =
    StateNotifierProvider<BoothListNotifier, BoothListState>(
        (ref) => BoothListNotifier(ref: ref));
