import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/response_list_branch_model.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/input_lain_lain_repository.dart';

// 3. state provider
final inputLainLainCabang = StateNotifierProvider<InputLainLainNotifier, InputLainLainState>(
    (ref) => InputLainLainNotifier(ref: ref));

// 2. notifier
class InputLainLainNotifier extends StateNotifier<InputLainLainState> {
  final Ref ref;
  InputLainLainNotifier({required this.ref}) : super(InputLainLainStateInit());
  void loadListCabangInputLainLain() async {
    try {
      state = InputLainLainStateLoading();
      final resp = await InputLainLainRepository(dio: ref.read(dioProvider))
          .loadListCabangInputLainLain();

      state = InputLainLainStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = InputLainLainStateError(message: e.message ?? "");
      } else {
        state = InputLainLainStateError(message: e.toString());
      }
    }
  }
}

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

class InputLainLainStateInit extends InputLainLainState {
  InputLainLainStateInit() : super(DateTime.now());
}

class InputLainLainStateLoading extends InputLainLainState {
  InputLainLainStateLoading() : super(DateTime.now());
}

class InputLainLainStateError extends InputLainLainState {
  final String? message;
  InputLainLainStateError({
    required this.message,
  }) : super(DateTime.now());
}

class InputLainLainStateDone extends InputLainLainState {
  final List<ResponseListBranchModel> model;
  InputLainLainStateDone({
    required this.model,
  }) : super(DateTime.now());
}
