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

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

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

class GetDestinationStateInit extends GetDestinationState {
  GetDestinationStateInit() : super(DateTime.now());
}

class GetDestinationStateLoading extends GetDestinationState {
  GetDestinationStateLoading() : super(DateTime.now());
}

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

class GetDestinationStateDone extends GetDestinationState {
  final List<DestinationModel> model;
  GetDestinationStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetDestinationNotifier extends StateNotifier<GetDestinationState> {
  final Ref ref;
  GetDestinationNotifier({
    required this.ref,
  }) : super(GetDestinationStateInit());

  void getData({required String token}) async {
    try {
      state = GetDestinationStateLoading();
      final dio = ref.read(dioProvider);
      final resp =
          await SuratJalanRepository(dio: dio).getDestination(token: token);
      state = GetDestinationStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = GetDestinationStateError(message: e.message.toString());
      } else {
        state = GetDestinationStateError(message: e.toString());
      }
    }
  }
}

//provider
final GetDestinationProvider =
    StateNotifierProvider<GetDestinationNotifier, GetDestinationState>(
        (ref) => GetDestinationNotifier(ref: ref));
