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

import 'package:mitra_corporate/repository/surat_jalan_repository.dart';

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

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

class SendSuratJalanStateInit extends SendSuratJalanState {
  SendSuratJalanStateInit() : super(DateTime.now());
}

class SendSuratJalanStateLoading extends SendSuratJalanState {
  SendSuratJalanStateLoading() : super(DateTime.now());
}

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

class SendSuratJalanStateDone extends SendSuratJalanState {
  final String number;
  SendSuratJalanStateDone({
    required this.number,
  }) : super(DateTime.now());
}

//notifier
class SendSuratJalanNotifier extends StateNotifier<SendSuratJalanState> {
  final Ref ref;
  SendSuratJalanNotifier({
    required this.ref,
  }) : super(SendSuratJalanStateInit());

  void Send({required String token, required String deliveryID}) async {
    try {
      state = SendSuratJalanStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await SuratJalanRepository(dio: dio)
          .sendSuratJalan(token: token, deliveryID: deliveryID);
      state = SendSuratJalanStateDone(number: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = SendSuratJalanStateError(message: e.message.toString());
      } else {
        state = SendSuratJalanStateError(message: e.toString());
      }
    }
  }
}

//provider
final SendSuratJalanProvider =
    StateNotifierProvider<SendSuratJalanNotifier, SendSuratJalanState>(
        (ref) => SendSuratJalanNotifier(ref: ref));
