import { NativeModules, Platform } from 'react-native';
import * as SMWorkoutLibrary from '../src/SMWorkout';

const LINKING_ERROR =
  `The package 'react-native-smkit-ui' doesn't seem to be linked. Make sure: \n\n` +
  Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
  '- You rebuilt the app after installing the package\n' +
  '- You are not using Expo Go\n';

// Fix: Use bracket notation to access NativeModules property
const SMKitUIManager = NativeModules['SMKitUIManager']
  ? NativeModules['SMKitUIManager']
  : new Proxy(
      {},
      {
        get() {
          throw new Error(LINKING_ERROR);
        },
      }
    );

/**
 * This function will configure the sdk
 * @param {string} key - your auth key
 */
export function configure(key: string): Promise<string> {
  return SMKitUIManager.configure(key);
}

/**
 * start an assessment session.
 *
 * @param {SMWorkoutLibrary.AssessmentTypes} type - The type of assessment to start.
 * @param {boolean} [showSummary=true] - Determines if the summary should be shown after assessment completion.
 * @param {SMWorkoutLibrary.UserData | null} userData - User data for the assessment session, or `null` if no user data is provided.
 * @param {boolean} [forceShowUserDataScreen=false] - Forces the display of the user data screen even if user data is provided.
 * @param {string} customAssessmentID - A unique identifier for a custom assessment session.
 * @returns {Promise<{ summary: string; didFinish: boolean }>} - A promise that resolves with an object containing the summary and a flag indicating whether the assessment finished.
 */
export function startAssessment(
  type: SMWorkoutLibrary.AssessmentTypes,
  showSummary: boolean = true,
  userData: SMWorkoutLibrary.UserData | null,
  forceShowUserDataScreen: boolean = false,
  customAssessmentID: string
): Promise<{ summary: string; didFinish: boolean }> {
  return SMKitUIManager.startAssessment(
    type,
    showSummary,
    userData?.toJson(),
    forceShowUserDataScreen,
    customAssessmentID
  );
}

/**
 * Starts a custom workout session.
 *
 * @param {SMWorkoutLibrary.SMWorkout} workout - The custom workout configuration.
 * @returns {Promise<{ summary: string; didFinish: boolean }>} - A promise that resolves with an object containing the summary and a flag indicating if the workout session finished.
 */
export function startCustomWorkout(
  workout: SMWorkoutLibrary.SMWorkout
): Promise<{ summary: string; didFinish: boolean }> {
  return SMKitUIManager.startCustomWorkout(workout.toJson());
}

/**
 * Initiates a custom assessment session.
 *
 * @param {SMWorkoutLibrary.SMWorkout} assessment - The assessment configuration for the session.
 * @param {SMWorkoutLibrary.UserData | null} userData - User data for the assessment, or `null` if no user data is provided.
 * @param {boolean} [forceShowUserDataScreen=false] - Forces the display of the user data screen even if user data is provided.
 * @param {boolean} [showSummary=true] - Determines if the summary should be shown after assessment completion.
 * @returns {Promise<{ summary: string; didFinish: boolean }>} - A promise that resolves with an object containing the summary and a flag indicating if the assessment finished.
 */
export function startCustomAssessment(
  assessment: SMWorkoutLibrary.SMWorkout,
  userData: SMWorkoutLibrary.UserData | null,
  forceShowUserDataScreen: boolean = false,
  showSummary: boolean = true
): Promise<{ summary: string; didFinish: boolean }> {
  return SMKitUIManager.startCustomAssessment(
    assessment.toJson(),
    userData?.toJson(),
    forceShowUserDataScreen,
    showSummary
  );
}

/**
 * Starts a workout program using the provided workout configuration.
 *
 * @param {SMWorkoutLibrary.WorkoutConfig} workoutConfig - The configuration for the workout program.
 * @returns {Promise<{ summary: string; didFinish: boolean }>} - A promise that resolves with an object containing the summary and a flag indicating if the workout program finished.
 */
export function startWorkoutProgram(
  workoutConfig: SMWorkoutLibrary.WorkoutConfig
): Promise<{ summary: string; didFinish: boolean }> {
  return SMKitUIManager.startWorkoutProgram(workoutConfig.toJson());
}

/**
 * Sets a text language for the current workout session
 * @param {SMWorkoutLibrary} language - The language that you would like to set.
 */
export function setSessionLanguage(language: SMWorkoutLibrary.Language) {
  SMKitUIManager.setSessionLanguage(language);
}

/**
 * Sets the closure target for the workout session.
 * @param {SMWorkoutLibrary.EndExercisePreferences} endExercisePreferences - The closure target to set.
 */
export function setEndExercisePreferences(
  endExercisePreferences: SMWorkoutLibrary.EndExercisePreferences
) {
  SMKitUIManager.setEndExercisePreferences(endExercisePreferences);
}

/**
 * Sets the counter preferences for the workout session.
 * @param {SMWorkoutLibrary.CounterPreferences} counterPreferences - The counter preferences to set.
 */
export function setCounterPreferences(
  counterPreferences: SMWorkoutLibrary.CounterPreferences
) {
  SMKitUIManager.setCounterPreferences(counterPreferences);
}
