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

// 3. state provider
final inputLainLainSave = StateNotifierProvider<InputLainLainSaveNotifier,
    InputLainLainSaveState>((ref) => InputLainLainSaveNotifier(ref: ref));

// 2. notifier
class InputLainLainSaveNotifier
    extends StateNotifier<InputLainLainSaveState> {
  final Ref ref;
  InputLainLainSaveNotifier({required this.ref})
      : super(InputLainLainSaveStateInit());
  void inputLainLainSave({
    required String courierID,
    required String tipeID,
    required String branchID,
    required String branchCode,
    required String pickup,
    required String destination,
    required String note
  }) async {
    try {
      state = InputLainLainSaveStateLoading();
      final resp = await InputLainLainRepository(dio: ref.read(dioProvider))
          .inputLainLainSave(
              courierID: courierID,
              tipeID: tipeID,
              branchID: branchID,
              branchCode: branchCode,
              pickup: pickup,
              destination: destination,
              note: note
              );

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

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

class InputLainLainSaveStateInit extends InputLainLainSaveState {
  InputLainLainSaveStateInit() : super(DateTime.now());
}

class InputLainLainSaveStateLoading extends InputLainLainSaveState {
  InputLainLainSaveStateLoading() : super(DateTime.now());
}

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

class InputLainLainSaveStateDone extends InputLainLainSaveState {
  final ResponseSavePengantaranHasilModel model;
  InputLainLainSaveStateDone({
    required this.model,
  }) : super(DateTime.now());
}
