import 'package:absensi_sas/model/check_distance_model.dart';
import 'package:absensi_sas/model/check_presensi_jam_model.dart';
import '../model/rekap_kehadiran_home_screen_model.dart';
import 'base_repository.dart';

class PresensiRepository extends BaseRepository {
  PresensiRepository({required super.graphql, required super.dio});

  // check distance
  Future<CheckDistanceModel> checkDistance(
    String M_StaffID,
    String M_CompanyID,
    String CurrentLatitude,
    String CurrentLongitude,
  ) async {
    const String query =
        r'''query($M_StaffID:String!, $M_CompanyID:String!, $CurrentLatitude:String!, $CurrentLongitude:String!){ queryCheckDistance(M_StaffID:$M_StaffID, M_CompanyID:$M_CompanyID, CurrentLatitude:$CurrentLatitude, CurrentLongitude:$CurrentLongitude){ status message selfie unit max_distance current_distance } }''';

    Map<String, dynamic> inpVariables = {
      "M_StaffID": M_StaffID,
      "M_CompanyID": M_CompanyID,
      "CurrentLatitude": CurrentLatitude,
      "CurrentLongitude": CurrentLongitude,
    };
    final resp = await postGraphQlMutation(query, inpVariables);
    // final loginData = AuthModel.fromJson(resp['userLogin']);

    // print(inpVariables);

    print('obj queryCheckDistance : ${resp["queryCheckDistance"]}');

    // final result = AuthModel(
    //   token: resp["loginAttendance"]["token"],
    //   model: StaffModel.fromJson(resp["loginAttendance"]),
    // );

    final result = CheckDistanceModel(
        currentDistance: resp['queryCheckDistance']['current_distance'],
        maxDistance: resp['queryCheckDistance']['max_distance'],
        message: resp['queryCheckDistance']['message'],
        selfie: resp['queryCheckDistance']['selfie'],
        status: resp['queryCheckDistance']['status'],
        unit: resp['queryCheckDistance']['unit']);
    return result;
  }

  // presensi normal Clock In
  Future<String> presensiNormalClockIn(
      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 {
    const String query =
        r'''mutation($T_TransactionM_StaffID:String!, $T_TransactionM_CompanyID:String!, $T_TransactionCurrentLatitude:String!, $T_TransactionCurrentLongitude:String!, $T_TransactionCurrentDistance:String!, $T_TransactionSelfiePhoto:String, $token:String!, $isSelfie:String!){ mutationClockInAttendance(T_TransactionM_StaffID:$T_TransactionM_StaffID, T_TransactionM_CompanyID:$T_TransactionM_CompanyID, T_TransactionCurrentLatitude:$T_TransactionCurrentLatitude, T_TransactionCurrentLongitude:$T_TransactionCurrentLongitude, T_TransactionCurrentDistance:$T_TransactionCurrentDistance, T_TransactionSelfiePhoto:$T_TransactionSelfiePhoto, token:$token, isSelfie:$isSelfie){ status message } }''';

    Map<String, dynamic> inpVariables = paramInpVariables;
    print(paramInpVariables);

    final resp = await postGraphQlMutation(query, inpVariables);

    // print(inpVariables);

    print(
        'obj mutationClockInAttendance : ${resp["mutationClockInAttendance"]}');

    // final result = AuthModel(
    //   token: resp["loginAttendance"]["token"],
    //   model: StaffModel.fromJson(resp["loginAttendance"]),
    // );

    final result = resp["mutationClockInAttendance"]['status'];
    return result;
  }

  // presensi normal clock Out
  Future<String> presensiNormalClockOut(
      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 {
    const String query =
        r'''mutation(
          $T_TransactionM_StaffID:String!, 
          $T_TransactionM_CompanyID:String!, 
          $T_TransactionCurrentLatitude:String!, 
          $T_TransactionCurrentLongitude:String!, 
          $T_TransactionCurrentDistance:String!, 
          $T_TransactionSelfiePhoto:String, 
          $token:String!, 
          $isSelfie:String!
          ){ 
          mutationClockOutAttendance(
            T_TransactionM_StaffID:$T_TransactionM_StaffID, 
            T_TransactionM_CompanyID:$T_TransactionM_CompanyID, 
            T_TransactionCurrentLatitude:$T_TransactionCurrentLatitude, 
            T_TransactionCurrentLongitude:$T_TransactionCurrentLongitude, 
            T_TransactionCurrentDistance:$T_TransactionCurrentDistance, 
            T_TransactionSelfiePhoto:$T_TransactionSelfiePhoto, 
            token:$token, isSelfie:$isSelfie
          ){ 
            status 
            message 
          } 
        }''';

    Map<String, dynamic> inpVariables = paramInpVariables;
    print(paramInpVariables);

    final resp = await postGraphQlMutation(query, inpVariables);

    // print(inpVariables);

    print(
        'obj mutationClockOutAttendance : ${resp["mutationClockOutAttendance"]}');

    // final result = AuthModel(
    //   token: resp["loginAttendance"]["token"],
    //   model: StaffModel.fromJson(resp["loginAttendance"]),
    // );

    final result = resp["mutationClockOutAttendance"]['status'];
    return result;
  }

  // check presensi jam
  Future<CheckPresensiJamModel> checkPresensiJam(
    String M_StaffID,
    String M_CompanyID,
    String token,
    Map<String, dynamic> paramInpVariables,
  ) async {
    const String query =
        r'''query($M_StaffID:String!, $M_CompanyID:String!, $token:String!){ 
              queryCheckTimeAttendance(M_StaffID:$M_StaffID, M_CompanyID:$M_CompanyID, token:$token){ 
                status 
                message 
                jam_clock_in 
                jam_clock_out 
                is_absen_clock_in 
                is_absen_clock_out 
              } 
        }''';

    // Map<String, dynamic> inpVariables = paramInpVariables;

    // Map<String, dynamic> X = {
    //   "M_StaffID": M_StaffID,
    //   "M_CompanyID": M_CompanyID,
    //   "token": token+"1",
    // };

    print(paramInpVariables);

    final resp = await postGraphQlQuery(query, paramInpVariables);
    // final resp = await postGraphQlQueryX(query, X);

    // print(inpVariables);

    print('obj queryCheckTimeAttendance : ${resp["queryCheckTimeAttendance"]}');

    final result = CheckPresensiJamModel(
      isAbsenClockIn: resp['queryCheckTimeAttendance']['is_absen_clock_in'],
      isAbsenClockOut: resp['queryCheckTimeAttendance']['is_absen_clock_out'],
      jamClockIn: resp['queryCheckTimeAttendance']['jam_clock_in'],
      jamClockOut: resp['queryCheckTimeAttendance']['jam_clock_out'],
      message: resp['queryCheckTimeAttendance']['message'],
      status: resp['queryCheckTimeAttendance']['status'],
    );
    return result;
  }

  // rekap kehadiran home screen
  Future<RekapKehadiranHomeScreenModel> rekapKehadiranHomeScreen(
    String M_StaffID,
    String M_CompanyID,
    String token,
    Map<String, dynamic> paramInpVariables,
  ) async {
    const String query =
        r'''query($M_StaffID:String!, $M_CompanyID:String!, $token:String!){ 
              queryRekapKehadiranHomeScreen(M_StaffID:$M_StaffID, M_CompanyID:$M_CompanyID, token:$token){ 
                status 
                message 
                kehadiran 
                tidak_hadir 
                lembur 
              } 
        }''';

    // Map<String, dynamic> inpVariables = paramInpVariables;

    // Map<String, dynamic> X = {
    //   "M_StaffID": M_StaffID,
    //   "M_CompanyID": M_CompanyID,
    //   "token": token+"1",
    // };

    print(paramInpVariables);

    final resp = await postGraphQlQuery(query, paramInpVariables);
    // final resp = await postGraphQlQueryX(query, X);

    // print(inpVariables);

    print('obj queryRekapKehadiranHomeScreen : ${resp["queryRekapKehadiranHomeScreen"]}');

    final result = RekapKehadiranHomeScreenModel(
      kehadiran: resp['queryRekapKehadiranHomeScreen']['kehadiran'],
      tidakHadir: resp['queryRekapKehadiranHomeScreen']['tidak_hadir'],
      lembur: resp['queryRekapKehadiranHomeScreen']['lembur'],
      message: resp['queryRekapKehadiranHomeScreen']['message'],
      status: resp['queryRekapKehadiranHomeScreen']['status'],
    );
    return result;
  }
}
