import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mitra_corporate/model/chart_data_model.dart';
import 'package:mitra_corporate/repository/dashboard_repository.dart';

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

// 3. state provider
final ChartDataProvider =
    StateNotifierProvider<ChartDataNotifier, ChartDataState>(
        (ref) => ChartDataNotifier(ref: ref));

// 2. notifier
class ChartDataNotifier extends StateNotifier<ChartDataState> {
  final Ref ref;
  ChartDataNotifier({required this.ref}) : super(ChartDataStateInit());
  void ChartData(
      {required String token,
      required String filter,
      required String company_id,
      bool isRememberMe = false}) async {
    try {
      state = ChartDataStateLoading();
      final resp = await DashboardRepository(dio: ref.read(dioProvider))
          .getChartData(token: token, filter: filter, company_id: company_id);

      state = ChartDataStateDone(model: resp);

      // print(shared.getString(Constant.bearerName));
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = ChartDataStateError(message: e.message ?? "");
      } else {
        state = ChartDataStateError(message: e.toString());
      }
    }
  }
}

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

class ChartDataStateInit extends ChartDataState {
  ChartDataStateInit() : super(DateTime.now());
}

class ChartDataStateLoading extends ChartDataState {
  ChartDataStateLoading() : super(DateTime.now());
}

class ChartDataStateError extends ChartDataState {
  final String? message;
  ChartDataStateError({
    required this.message,
  }) : super(DateTime.now());
}

class ChartDataStateDone extends ChartDataState {
  final ChartDataModel model;
  ChartDataStateDone({
    required this.model,
  }) : super(DateTime.now());
}
