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

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

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

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

class OtherReceivePersonStateInit extends OtherReceivePersonState {
  OtherReceivePersonStateInit() : super(DateTime.now());
}

class OtherReceivePersonStateLoading extends OtherReceivePersonState {
  OtherReceivePersonStateLoading() : super(DateTime.now());
}

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

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

//notifier
class OtherReceivePersonNotifier
    extends StateNotifier<OtherReceivePersonState> {
  final Ref ref;
  OtherReceivePersonNotifier({
    required this.ref,
  }) : super(OtherReceivePersonStateInit());

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

//provider
final OtherReceivePersonProvider =
    StateNotifierProvider<OtherReceivePersonNotifier, OtherReceivePersonState>(
        (ref) => OtherReceivePersonNotifier(ref: ref));
