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

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

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

class GetDeliveryTypeStateInit extends GetDeliveryTypeState {
  GetDeliveryTypeStateInit() : super(DateTime.now());
}

class GetDeliveryTypeStateLoading extends GetDeliveryTypeState {
  GetDeliveryTypeStateLoading() : super(DateTime.now());
}

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

class GetDeliveryTypeStateDone extends GetDeliveryTypeState {
  final List<DeliveryTypeModel> model;
  GetDeliveryTypeStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetDeliveryTypeNotifier extends StateNotifier<GetDeliveryTypeState> {
  final Ref ref;
  GetDeliveryTypeNotifier({
    required this.ref,
  }) : super(GetDeliveryTypeStateInit());

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

//provider
final GetDeliveryTypeProvider =
    StateNotifierProvider<GetDeliveryTypeNotifier, GetDeliveryTypeState>(
        (ref) => GetDeliveryTypeNotifier(ref: ref));
