import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:queuedisplay/model/branch_model.dart';
import 'package:queuedisplay/provider/dio_provider.dart';
import 'package:queuedisplay/repository/base_repository.dart';
import 'package:queuedisplay/repository/counter_repository.dart';

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

class BranchListStateInit extends BranchListState {
  BranchListStateInit() : super(DateTime.now());
}

class BranchListStateLoading extends BranchListState {
  BranchListStateLoading() : super(DateTime.now());
}

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

class BranchListStateDone extends BranchListState {
  final List<BranchModel> model;
  BranchListStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class BranchListNotifier extends StateNotifier<BranchListState> {
  final Ref ref;
  BranchListNotifier({
    required this.ref,
  }) : super(BranchListStateInit());

  void list() async {
    try {
      state = BranchListStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await CounterRepository(dio: dio).getBranch();
      state = BranchListStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = BranchListStateError(message: e.message);
      } else {
        state = BranchListStateError(message: e.toString());
      }
    }
  }
}

//provider
final BranchListProvider =
    StateNotifierProvider<BranchListNotifier, BranchListState>(
        (ref) => BranchListNotifier(ref: ref));
