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

class PendingWorkStateInit extends PendingWorkState {
  PendingWorkStateInit() : super(DateTime.now());
}

class PendingWorkStateLoading extends PendingWorkState {
  PendingWorkStateLoading() : super(DateTime.now());
}

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

class PendingWorkStateDone extends PendingWorkState {
  final List<PendingWorkModel> model;
  PendingWorkStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class PendingWorkNotifier extends StateNotifier<PendingWorkState> {
  final Ref ref;
  PendingWorkNotifier({
    required this.ref,
  }) : super(PendingWorkStateInit());

  void getData({required String id}) async {
    try {
      state = PendingWorkStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await HomeScreenRepository(dio: dio).getPendingWork(id: id);
      state = PendingWorkStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = PendingWorkStateError(message: e.message.toString());
      } else {
        state = PendingWorkStateError(message: e.toString());
      }
    }
  }
}

//provider
final PendingWorkProvider =
    StateNotifierProvider<PendingWorkNotifier, PendingWorkState>(
        (ref) => PendingWorkNotifier(ref: ref));
