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 SendQrCodeState extends Equatable {
  final DateTime date;
  const SendQrCodeState(this.date);
  @override
  List<Object?> get props => [date];
}

class SendQrCodeStateInit extends SendQrCodeState {
  SendQrCodeStateInit() : super(DateTime.now());
}

class SendQrCodeStateLoading extends SendQrCodeState {
  SendQrCodeStateLoading() : super(DateTime.now());
}

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

class SendQrCodeStateDone extends SendQrCodeState {
  final String model;
  SendQrCodeStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class SendQrCodeNotifier extends StateNotifier<SendQrCodeState> {
  final Ref ref;
  SendQrCodeNotifier({
    required this.ref,
  }) : super(SendQrCodeStateInit());

  void senQrCode(
      {required String token, required List<String> arrOrderID}) async {
    try {
      state = SendQrCodeStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await SuratJalanRepository(dio: dio)
          .sendQrCode(token: token, arrOrderID: arrOrderID);
      state = SendQrCodeStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = SendQrCodeStateError(message: e.message.toString());
      } else {
        state = SendQrCodeStateError(message: e.toString());
      }
    }
  }
}

//provider
final SendQrCodeProvider =
    StateNotifierProvider<SendQrCodeNotifier, SendQrCodeState>(
        (ref) => SendQrCodeNotifier(ref: ref));
