import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/detail_history_model.dart';
import '/models/detail_work_model.dart';
import '/models/history_model.dart';
import '../../repository/history_screen_repository.dart';
import '../../repository/work_list_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 DetailWorkState extends Equatable {
  final DateTime date;
  const DetailWorkState(this.date);
  @override
  List<Object?> get props => [date];
}

class DetailWorkStateInit extends DetailWorkState {
  DetailWorkStateInit() : super(DateTime.now());
}

class DetailWorkStateLoading extends DetailWorkState {
  DetailWorkStateLoading() : super(DateTime.now());
}

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

class DetailWorkStateDone extends DetailWorkState {
  final DetailWorkModel model;
  DetailWorkStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class DetailWorkNotifier extends StateNotifier<DetailWorkState> {
  final Ref ref;
  DetailWorkNotifier({
    required this.ref,
  }) : super(DetailWorkStateInit());

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

//provider
final DetailWorkProvider =
    StateNotifierProvider<DetailWorkNotifier, DetailWorkState>(
        (ref) => DetailWorkNotifier(ref: ref));
