package com.reactnativesuperpoweredklaas

import android.content.Context
import android.media.AudioManager
import android.os.Build
import androidx.annotation.RequiresApi
import com.facebook.react.bridge.*
import java.util.*


class SuperpoweredKlaasModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

  override fun getName(): String {
    return "SuperpoweredKlaas"
  }

  @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  @ReactMethod
  fun initialize(tracksPaths: ReadableArray) {
    setup(
      "WVlaNi9aQ21ha0ltSDY0NTA0NTFiMDllNGE0ZTE1Yzg2M2I4MDVmZGFjYjBjYTMzMTM0ZTJtQ3o4OWJtLjZuZERIM0ZYbjF6",
      false,
      false,
      false,
      true,
      true,
      false,
      false
    )
    val tracksAmount = tracksPaths.size()
    var sampleRateString: String? = null
    var bufferSizeString: String? = null
    val audioManager = Objects.requireNonNull(currentActivity)
      ?.getSystemService(Context.AUDIO_SERVICE) as AudioManager
    sampleRateString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE)
    bufferSizeString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)
    if (sampleRateString == null) sampleRateString = "4800"
    if (bufferSizeString == null) bufferSizeString = "480"
    val sampleRate = sampleRateString.toInt()
    val bufferSize = bufferSizeString.toInt()
    val tempDir = currentActivity!!.cacheDir.absolutePath
    val trackPathsArray = arrayOfNulls<String>(tracksAmount)
    for (i in 0 until tracksAmount) trackPathsArray[i] = tracksPaths.getString(i)
    multiTracksPlayer(
      trackPathsArray,
      tracksAmount,
      sampleRate,
      bufferSize,
      tempDir
    )
  }

  @ReactMethod
  fun getDisplayPosition(promise: Promise) {
    promise.resolve(getDisplayPosition())
  }

  external fun getDisplayPosition(): Int

  @ReactMethod
  fun getTotalBufferedEndPercent(promise: Promise) {
    promise.resolve(getBufferedEndPercent(-1))
  }

  @ReactMethod
  fun getBufferedEndPercent(index: Int, promise: Promise) {
    promise.resolve(getBufferedEndPercent(index))
  }

  @ReactMethod
  external fun pause()

  @ReactMethod
  external fun play()

  @ReactMethod
  external fun setPosition(second: Double, andStop: Boolean)

  @ReactMethod
  external fun setup(
    licenseKey: String,
    enableAudioAnalysis: Boolean,
    enableFFTAndFrequencyDomain: Boolean,
    enableAudioTimeStretching: Boolean,
    enableAudioEffects: Boolean,
    enableAudioPlayerAndDecoder: Boolean,
    enableCryptographics: Boolean,
    enableNetworking: Boolean
  )

  private external fun multiTracksPlayer(
    trackPaths: Array<String?>,
    tracksAmount: Int,
    sampleRate: Int,
    bufferSize: Int,
    tempDir: String
  )

  @ReactMethod
  external fun destroy()

  private external fun getBufferedEndPercent(index: Int): Double

  @ReactMethod
  external fun setVolume(index: Int, left: Float, right: Float)

  @ReactMethod
  external fun setRate(rate: Float)

  @ReactMethod
  external fun setPitchShiftCents(pitchShiftCents: Int)

  companion object {
    init {
      System.loadLibrary("cpp")
    }
  }

}
