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

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

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

class OtherDeliveryLocationStateInit extends OtherDeliveryLocationState {
  OtherDeliveryLocationStateInit() : super(DateTime.now());
}

class OtherDeliveryLocationStateLoading extends OtherDeliveryLocationState {
  OtherDeliveryLocationStateLoading() : super(DateTime.now());
}

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

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

//notifier
class OtherDeliveryLocationNotifier
    extends StateNotifier<OtherDeliveryLocationState> {
  final Ref ref;
  OtherDeliveryLocationNotifier({
    required this.ref,
  }) : super(OtherDeliveryLocationStateInit());

  void setData(
      {required String id,
      required String step,
      required String lat,
      required String long,
      required String address,
      required String branchID}) async {
    try {
      state = OtherDeliveryLocationStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await GetOtherRepository(dio: dio).setLocation(
          id: id,
          step: step,
          lat: lat,
          long: long,
          address: address,
          branchID: branchID);
      state = OtherDeliveryLocationStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = OtherDeliveryLocationStateError(message: e.message.toString());
      } else {
        state = OtherDeliveryLocationStateError(message: e.toString());
      }
    }
  }
}

//provider
final OtherDeliveryLocationProvider = StateNotifierProvider<
        OtherDeliveryLocationNotifier, OtherDeliveryLocationState>(
    (ref) => OtherDeliveryLocationNotifier(ref: ref));
