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

import 'package:mitra_corporate/model/surat_jalan_model.dart';

import 'package:mitra_corporate/repository/dashboard_repository.dart';

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

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

// 3. state provider
final DashboardDeliveryProvider =
    StateNotifierProvider<DashboardDeliveryNotifier, DashboardDeliveryState>(
        (ref) => DashboardDeliveryNotifier(ref: ref));

// 2. notifier
class DashboardDeliveryNotifier extends StateNotifier<DashboardDeliveryState> {
  final Ref ref;
  DashboardDeliveryNotifier({required this.ref})
      : super(DashboardDeliveryStateInit());
  void DashboardDelivery(
      {required String token,
      required String company_id,
      bool isRememberMe = false}) async {
    try {
      state = DashboardDeliveryStateLoading();
      final resp = await DashboardRepository(dio: ref.read(dioProvider))
          .getOrder(token: token, companyID: company_id);

      state = DashboardDeliveryStateDone(model: resp);

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

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

class DashboardDeliveryStateInit extends DashboardDeliveryState {
  DashboardDeliveryStateInit() : super(DateTime.now());
}

class DashboardDeliveryStateLoading extends DashboardDeliveryState {
  DashboardDeliveryStateLoading() : super(DateTime.now());
}

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

class DashboardDeliveryStateDone extends DashboardDeliveryState {
  final List<SuratJalanModel> model;
  DashboardDeliveryStateDone({
    required this.model,
  }) : super(DateTime.now());
}
