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

class ResultDeliveryLocationStateInit extends ResultDeliveryLocationState {
  ResultDeliveryLocationStateInit() : super(DateTime.now());
}

class ResultDeliveryLocationStateLoading extends ResultDeliveryLocationState {
  ResultDeliveryLocationStateLoading() : super(DateTime.now());
}

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

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

//notifier
class ResultDeliveryLocationNotifier
    extends StateNotifier<ResultDeliveryLocationState> {
  final Ref ref;
  ResultDeliveryLocationNotifier({
    required this.ref,
  }) : super(ResultDeliveryLocationStateInit());

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

//provider
final ResultDeliveryLocationProvider = StateNotifierProvider<
        ResultDeliveryLocationNotifier, ResultDeliveryLocationState>(
    (ref) => ResultDeliveryLocationNotifier(ref: ref));
