import 'package:absensi_sas/provider/current_user_provider.dart';
import 'package:absensi_sas/repository/presensi_repository.dart';

import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../model/check_distance_model.dart';
import '../../provider/current_check_distance_provider.dart';
import '../../provider/dio_provider.dart';
import '../../provider/graphql_provider.dart';
import '../../repository/base_repository.dart';
import 'check_presensi_jam_provider.dart';

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

class CheckDistanceStateInit extends CheckDistanceState {
  CheckDistanceStateInit() : super(DateTime.now());
}

class CheckDistanceStateLoading extends CheckDistanceState {
  CheckDistanceStateLoading() : super(DateTime.now());
}

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

class CheckDistanceStateDone extends CheckDistanceState {
  final CheckDistanceModel model;
  CheckDistanceStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class CheckDistanceNotifier extends StateNotifier<CheckDistanceState> {
  final Ref ref;
  CheckDistanceNotifier({
    required this.ref,
  }) : super(CheckDistanceStateInit());

  void checkDistance(
    String M_StaffID,
    String M_CompanyID,
    String CurrentLatitude,
    String CurrentLongitude,
  ) async {
    try {
      state = CheckDistanceStateLoading();
      final graphql = ref.read(graphqlProvider(''));
      final dio = ref.read(dioProvider);
      final resp =
          await PresensiRepository(graphql: graphql, dio: dio).checkDistance(
        M_StaffID,
        M_CompanyID,
        CurrentLatitude,
        CurrentLongitude,
      );
      // set ke global state provider
      ref.read(currentCheckDistanceProvider.notifier).update((state) => resp);

      // panggil provider cek jam masuk dan pulang (checkPresensiJamProvider)
      final user = ref.read(currentUserProvider);
      Map<String, dynamic> inpVariablesCheckPresensiJam = {
        "M_StaffID": M_StaffID,
        "M_CompanyID": M_CompanyID,
        "token": user?.token ?? "",
      };
      ref.read(checkPresensiJamProvider.notifier).checkPresensiJam(
            M_StaffID,
            M_CompanyID,
            user?.token ?? "",
            inpVariablesCheckPresensiJam,
          );

      state = CheckDistanceStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = CheckDistanceStateError(message: e.message ?? "");
      } else {
        state = CheckDistanceStateError(message: e.toString());
      }
    }
  }
}

// provider
final checkDistanceProvider =
    StateNotifierProvider<CheckDistanceNotifier, CheckDistanceState>(
  (ref) => CheckDistanceNotifier(ref: ref),
);
