import '../../repository/presensi_repository.dart';

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

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

class PresensiClockInStateInit extends PresensiClockInState {
  PresensiClockInStateInit() : super(DateTime.now());
}

class PresensiClockInStateLoading extends PresensiClockInState {
  PresensiClockInStateLoading() : super(DateTime.now());
}

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

class PresensiClockInStateDone extends PresensiClockInState {
  final String model;
  PresensiClockInStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class PresensiClockInNotifier extends StateNotifier<PresensiClockInState> {
  final Ref ref;
  PresensiClockInNotifier({
    required this.ref,
  }) : super(PresensiClockInStateInit());

  void presensiClockIn(
    String T_TransactionM_StaffID,
    String T_TransactionM_CompanyID,
    String T_TransactionCurrentLatitude,
    String T_TransactionCurrentLongitude,
    String T_TransactionCurrentDistance,
    String T_TransactionSelfiePhoto,
    String token,
    String isSelfie,
    Map<String, dynamic> paramInpVariables,
  ) async {
    try {
      state = PresensiClockInStateLoading();
      final graphql = ref.read(graphqlProvider(''));
      final dio = ref.read(dioProvider);
      final resp = await PresensiRepository(graphql: graphql, dio: dio)
          .presensiNormalClockIn(
        T_TransactionM_StaffID,
        T_TransactionM_CompanyID,
        T_TransactionCurrentLatitude,
        T_TransactionCurrentLongitude,
        T_TransactionCurrentDistance,
        T_TransactionSelfiePhoto,
        token,
        isSelfie,
        paramInpVariables,
      );

      // panggil provider cek jam masuk dan pulang (checkPresensiJamProvider)
      Map<String, dynamic> inpVariablesCheckPresensiJam = {
        "M_StaffID": T_TransactionM_StaffID,
        "M_CompanyID": T_TransactionM_CompanyID,
        "token": token,
      };
      ref.read(checkPresensiJamProvider.notifier).checkPresensiJam(
            T_TransactionM_StaffID,
            T_TransactionM_CompanyID,
            token,
            inpVariablesCheckPresensiJam,
          );
      print('call check presensi');
      state = PresensiClockInStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = PresensiClockInStateError(message: e.message ?? "");
      } else {
        state = PresensiClockInStateError(message: e.toString());
      }
    }
  }
}

// provider
final presensiClockInProvider =
    StateNotifierProvider<PresensiClockInNotifier, PresensiClockInState>(
  (ref) => PresensiClockInNotifier(ref: ref),
);
