import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.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 SampleDeliveryLocationState extends Equatable {
  final DateTime date;
  const SampleDeliveryLocationState(this.date);
  @override
  List<Object?> get props => [date];
}

class SampleDeliveryLocationStateInit extends SampleDeliveryLocationState {
  SampleDeliveryLocationStateInit() : super(DateTime.now());
}

class SampleDeliveryLocationStateLoading extends SampleDeliveryLocationState {
  SampleDeliveryLocationStateLoading() : super(DateTime.now());
}

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

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

//notifier
class SampleDeliveryLocationNotifier
    extends StateNotifier<SampleDeliveryLocationState> {
  final Ref ref;
  SampleDeliveryLocationNotifier({
    required this.ref,
  }) : super(SampleDeliveryLocationStateInit());

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

//provider
final SampleDeliveryLocationProvider = StateNotifierProvider<
        SampleDeliveryLocationNotifier, SampleDeliveryLocationState>(
    (ref) => SampleDeliveryLocationNotifier(ref: ref));
