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

class OtherDeliverPersonStateInit extends OtherDeliverPersonState {
  OtherDeliverPersonStateInit() : super(DateTime.now());
}

class OtherDeliverPersonStateLoading extends OtherDeliverPersonState {
  OtherDeliverPersonStateLoading() : super(DateTime.now());
}

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

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

//notifier
class OtherDeliverPersonNotifier
    extends StateNotifier<OtherDeliverPersonState> {
  final Ref ref;
  OtherDeliverPersonNotifier({
    required this.ref,
  }) : super(OtherDeliverPersonStateInit());

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

//provider
final OtherDeliverPersonProvider =
    StateNotifierProvider<OtherDeliverPersonNotifier, OtherDeliverPersonState>(
        (ref) => OtherDeliverPersonNotifier(ref: ref));
