import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../repository/filter_repository.dart';

import '../../models/work_status_model.dart';

import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';

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

class WorkStatusStateInit extends WorkStatusState {
  WorkStatusStateInit() : super(DateTime.now());
}

class WorkStatusStateLoading extends WorkStatusState {
  WorkStatusStateLoading() : super(DateTime.now());
}

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

class WorkStatusStateDone extends WorkStatusState {
  final List<WorkStatusModel> model;
  WorkStatusStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class WorkStatusNotifier extends StateNotifier<WorkStatusState> {
  final Ref ref;
  WorkStatusNotifier({
    required this.ref,
  }) : super(WorkStatusStateInit());

  void getData() async {
    try {
      state = WorkStatusStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await FilterRepository(dio: dio).getStatus();
      state = WorkStatusStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = WorkStatusStateError(message: e.message.toString());
      } else {
        state = WorkStatusStateError(message: e.toString());
      }
    }
  }
}

//provider
final WorkStatusProvider =
    StateNotifierProvider<WorkStatusNotifier, WorkStatusState>(
        (ref) => WorkStatusNotifier(ref: ref));
