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

import '../../models/history_type_model.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 HistoryTypeListState extends Equatable {
  final DateTime date;
  const HistoryTypeListState(this.date);
  @override
  List<Object?> get props => [date];
}

class HistoryTypeListStateInit extends HistoryTypeListState {
  HistoryTypeListStateInit() : super(DateTime.now());
}

class HistoryTypeListStateLoading extends HistoryTypeListState {
  HistoryTypeListStateLoading() : super(DateTime.now());
}

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

class HistoryTypeListStateDone extends HistoryTypeListState {
  final List<HistoryTypeModel> model;
  HistoryTypeListStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class HistoryTypeListNotifier extends StateNotifier<HistoryTypeListState> {
  final Ref ref;
  HistoryTypeListNotifier({
    required this.ref,
  }) : super(HistoryTypeListStateInit());

  void getHistoryType() async {
    try {
      state = HistoryTypeListStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await HistoryRepository(dio: dio).getHistoryType();
      state = HistoryTypeListStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = HistoryTypeListStateError(message: e.message.toString());
      } else {
        state = HistoryTypeListStateError(message: e.toString());
      }
    }
  }
}

//provider
final HistoryTypeListProvider =
    StateNotifierProvider<HistoryTypeListNotifier, HistoryTypeListState>(
        (ref) => HistoryTypeListNotifier(ref: ref));
