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

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

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

class TotalWorkStateInit extends TotalWorkState {
  TotalWorkStateInit() : super(DateTime.now());
}

class TotalWorkStateLoading extends TotalWorkState {
  TotalWorkStateLoading() : super(DateTime.now());
}

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

class TotalWorkStateDone extends TotalWorkState {
  final WorkTotalModel model;
  TotalWorkStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class TotalWorkNotifier extends StateNotifier<TotalWorkState> {
  final Ref ref;
  TotalWorkNotifier({
    required this.ref,
  }) : super(TotalWorkStateInit());

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

//provider
final TotalWorkProvider =
    StateNotifierProvider<TotalWorkNotifier, TotalWorkState>(
        (ref) => TotalWorkNotifier(ref: ref));
