import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/detail_history_model.dart';
import '/models/history_model.dart';
import '/../repository/history_screen_repository.dart';
import '/screen/history_screen/history_screen.dart';

import '../../models/pending_work_model.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/home_screen_repository.dart';

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

class DetailHistoryStateInit extends DetailHistoryState {
  DetailHistoryStateInit() : super(DateTime.now());
}

class DetailHistoryStateLoading extends DetailHistoryState {
  DetailHistoryStateLoading() : super(DateTime.now());
}

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

class DetailHistoryStateDone extends DetailHistoryState {
  final DetailHistoryModel model;
  DetailHistoryStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class DetailHistoryNotifier extends StateNotifier<DetailHistoryState> {
  final Ref ref;
  DetailHistoryNotifier({
    required this.ref,
  }) : super(DetailHistoryStateInit());

  void getData(
      {required String id,
      required String tipeId,
      required String deliveryId}) async {
    try {
      state = DetailHistoryStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await HistoryRepository(dio: dio)
          .getDetail(id: id, tipeId: tipeId, deliveryId: deliveryId);
      state = DetailHistoryStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = DetailHistoryStateError(message: e.message.toString());
      } else {
        state = DetailHistoryStateError(message: e.toString());
      }
    }
  }
}

//provider
final DetailHistoryProvider =
    StateNotifierProvider<DetailHistoryNotifier, DetailHistoryState>(
        (ref) => DetailHistoryNotifier(ref: ref));
