import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mitra_corporate/model/surat_jalan_detail_model.dart';
import 'package:mitra_corporate/repository/surat_jalan_repository.dart';

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

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

class GetSuratJalanDetailStateInit extends GetSuratJalanDetailState {
  GetSuratJalanDetailStateInit() : super(DateTime.now());
}

class GetSuratJalanDetailStateLoading extends GetSuratJalanDetailState {
  GetSuratJalanDetailStateLoading() : super(DateTime.now());
}

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

class GetSuratJalanDetailStateDone extends GetSuratJalanDetailState {
  final List<SuratJalanDetailModel> model;
  GetSuratJalanDetailStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetSuratJalanDetailNotifier
    extends StateNotifier<GetSuratJalanDetailState> {
  final Ref ref;
  GetSuratJalanDetailNotifier({
    required this.ref,
  }) : super(GetSuratJalanDetailStateInit());

  void getData({required String token, required String deliveryID}) async {
    try {
      state = GetSuratJalanDetailStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await SuratJalanRepository(dio: dio)
          .getSuratJalanSetail(token: token, deliveryID: deliveryID);
      state = GetSuratJalanDetailStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = GetSuratJalanDetailStateError(message: e.message.toString());
      } else {
        state = GetSuratJalanDetailStateError(message: e.toString());
      }
    }
  }
}

//provider
final GetSuratJalanDetailProvider = StateNotifierProvider<
    GetSuratJalanDetailNotifier,
    GetSuratJalanDetailState>((ref) => GetSuratJalanDetailNotifier(ref: ref));
