import 'dart:convert';

import 'package:equatable/equatable.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../repository/base_repository.dart';

import '../../provider/dio_provider.dart';
import '../../repository/kurir_tolak_repository.dart';

//provider
final kurirTolakProvider =
    StateNotifierProvider<KurirTolakNotifier, KurirTolakState>(
        (ref) => KurirTolakNotifier(ref: ref));

// notifier
class KurirTolakNotifier extends StateNotifier<KurirTolakState> {
  final Ref ref;
  KurirTolakNotifier({
    required this.ref,
  }) : super(KurirTolakStateInit());

  void tolakKurir(
      {required String id,
      required String tipeId,
      required String deliveryId,
      required String note}) async {
    try {
      state = KurirTolakStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await KurirTolakRepository(dio: dio).getKurirTolak(
          id: id, tipeId: tipeId, deliveryId: deliveryId, note: note);
      state = KurirTolakStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = KurirTolakStateError(message: e.message.toString());
      } else {
        state = KurirTolakStateError(message: e.toString());
      }
    }
  }
}

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

class KurirTolakStateInit extends KurirTolakState {
  KurirTolakStateInit() : super(DateTime.now());
}

class KurirTolakStateLoading extends KurirTolakState {
  KurirTolakStateLoading() : super(DateTime.now());
}

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

class KurirTolakStateDone extends KurirTolakState {
  final bool model;
  KurirTolakStateDone({
    required this.model,
  }) : super(DateTime.now());
}
