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 ResultReceivePersonState extends Equatable {
  final DateTime date;
  const ResultReceivePersonState(this.date);
  @override
  List<Object?> get props => [date];
}

class ResultReceivePersonStateInit extends ResultReceivePersonState {
  ResultReceivePersonStateInit() : super(DateTime.now());
}

class ResultReceivePersonStateLoading extends ResultReceivePersonState {
  ResultReceivePersonStateLoading() : super(DateTime.now());
}

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

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

//notifier
class ResultReceivePersonNotifier
    extends StateNotifier<ResultReceivePersonState> {
  final Ref ref;
  ResultReceivePersonNotifier({
    required this.ref,
  }) : super(ResultReceivePersonStateInit());

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

//provider
final ResultReceivePersonProvider = StateNotifierProvider<
    ResultReceivePersonNotifier,
    ResultReceivePersonState>((ref) => ResultReceivePersonNotifier(ref: ref));
