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

import '../../repository/result_delivery_repository.dart';

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

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

class ResultDeliverPersonStateInit extends ResultDeliverPersonState {
  ResultDeliverPersonStateInit() : super(DateTime.now());
}

class ResultDeliverPersonStateLoading extends ResultDeliverPersonState {
  ResultDeliverPersonStateLoading() : super(DateTime.now());
}

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

class ResultDeliverPersonStateDone extends ResultDeliverPersonState {
  final bool model;
  ResultDeliverPersonStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class ResultDeliverPersonNotifier
    extends StateNotifier<ResultDeliverPersonState> {
  final Ref ref;
  ResultDeliverPersonNotifier({
    required this.ref,
  }) : super(ResultDeliverPersonStateInit());

  void setData({
    required String id,
    required String name,
    required String note,
  }) async {
    try {
      state = ResultDeliverPersonStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await ResultDeliveryRepository(dio: dio)
          .setDeliveryPerson(id: id, name: name, note: note);
      state = ResultDeliverPersonStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = ResultDeliverPersonStateError(message: e.message.toString());
      } else {
        state = ResultDeliverPersonStateError(message: e.toString());
      }
    }
  }
}

//provider
final ResultDeliverPersonProvider = StateNotifierProvider<
    ResultDeliverPersonNotifier,
    ResultDeliverPersonState>((ref) => ResultDeliverPersonNotifier(ref: ref));
