import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../models/response_save_work_suhu_model.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/work_input_suhu_repository.dart';

// 3. state provider
final workInputSuhuSave = StateNotifierProvider<WorkInputSuhuNotifier,
    WorkInputSuhuState>((ref) => WorkInputSuhuNotifier(ref: ref));

// 2. notifier
class WorkInputSuhuNotifier
    extends StateNotifier<WorkInputSuhuState> {
  final Ref ref;
  WorkInputSuhuNotifier({required this.ref})
      : super(WorkInputSuhuStateInit());
  void workInputSuhu({
    required String deliveryId,
    required String suhu
  }) async {
    try {
      state = WorkInputSuhuStateLoading();
      final resp = await WorkInputSuhuRepository(dio: ref.read(dioProvider))
          .inputWorkSuhu(
              deliveryId: deliveryId,
              suhu: suhu,
              );

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

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

class WorkInputSuhuStateInit extends WorkInputSuhuState {
  WorkInputSuhuStateInit() : super(DateTime.now());
}

class WorkInputSuhuStateLoading extends WorkInputSuhuState {
  WorkInputSuhuStateLoading() : super(DateTime.now());
}

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

class WorkInputSuhuStateDone extends WorkInputSuhuState {
  final ResponseSaveWorkSuhuModel model;
  WorkInputSuhuStateDone({
    required this.model,
  }) : super(DateTime.now());
}
