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

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

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

class WorkTypeStateInit extends WorkTypeState {
  WorkTypeStateInit() : super(DateTime.now());
}

class WorkTypeStateLoading extends WorkTypeState {
  WorkTypeStateLoading() : super(DateTime.now());
}

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

class WorkTypeStateDone extends WorkTypeState {
  final List<WorkTypeModel> model;
  WorkTypeStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class WorkTypeNotifier extends StateNotifier<WorkTypeState> {
  final Ref ref;
  WorkTypeNotifier({
    required this.ref,
  }) : super(WorkTypeStateInit());

  void getData() async {
    try {
      state = WorkTypeStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await FilterRepository(dio: dio).getType();
      state = WorkTypeStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = WorkTypeStateError(message: e.message.toString());
      } else {
        state = WorkTypeStateError(message: e.toString());
      }
    }
  }
}

//provider
final WorkTypeProvider = StateNotifierProvider<WorkTypeNotifier, WorkTypeState>(
    (ref) => WorkTypeNotifier(ref: ref));
