import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/response_save_work_tunda_model.dart';
import '../../repository/input_lain_lain_repository.dart';
import '../../repository/work_tunda_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 workTundaSave = StateNotifierProvider<WorkTundaSaveNotifier,
    WorkTundaSaveState>((ref) => WorkTundaSaveNotifier(ref: ref));

// 2. notifier
class WorkTundaSaveNotifier
    extends StateNotifier<WorkTundaSaveState> {
  final Ref ref;
  WorkTundaSaveNotifier({required this.ref})
      : super(WorkTundaSaveStateInit());
  void WorkTundaSave({
    required String courierID,
    required String tipeID,
    required String deliveryID,
    required String note
  }) async {
    try {
      state = WorkTundaSaveStateLoading();
      final resp = await WorkTundaRepository(dio: ref.read(dioProvider))
          .inputWorkTunda(
              courierID: courierID,
              tipeID: tipeID,
              deliveryID: deliveryID,
              note: note
              );

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

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

class WorkTundaSaveStateInit extends WorkTundaSaveState {
  WorkTundaSaveStateInit() : super(DateTime.now());
}

class WorkTundaSaveStateLoading extends WorkTundaSaveState {
  WorkTundaSaveStateLoading() : super(DateTime.now());
}

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

class WorkTundaSaveStateDone extends WorkTundaSaveState {
  final ResponseSaveWorkTundaModel model;
  WorkTundaSaveStateDone({
    required this.model,
  }) : super(DateTime.now());
}
