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

class PresensiClockOutStateInit extends PresensiClockOutState {
  PresensiClockOutStateInit() : super(DateTime.now());
}

class PresensiClockOutStateLoading extends PresensiClockOutState {
  PresensiClockOutStateLoading() : super(DateTime.now());
}

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

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

//notifier
class PresensiClockOutNotifier extends StateNotifier<PresensiClockOutState> {
  final Ref ref;
  PresensiClockOutNotifier({
    required this.ref,
  }) : super(PresensiClockOutStateInit());

  void presensiClockOut(
    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 = PresensiClockOutStateLoading();
      final graphql = ref.read(graphqlProvider(''));
      final dio = ref.read(dioProvider);
      final resp = await PresensiRepository(graphql: graphql, dio: dio)
          .presensiNormalClockOut(
        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 clockout');
      state = PresensiClockOutStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = PresensiClockOutStateError(message: e.message ?? "");
      } else {
        state = PresensiClockOutStateError(message: e.toString());
      }
    }
  }
}

// provider
final presensiClockOutProvider =
    StateNotifierProvider<PresensiClockOutNotifier, PresensiClockOutState>(
  (ref) => PresensiClockOutNotifier(ref: ref),
);
