package com.humand.demo

import android.app.Application
import android.content.res.Configuration
import androidx.emoji2.bundled.BundledEmojiCompatConfig
import androidx.emoji2.text.EmojiCompat
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.ReactPackage
import com.facebook.react.ReactNativeApplicationEntryPoint.loadReactNative
import com.facebook.react.common.ReleaseLevel
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint
import com.humand.demo.modules.AudioPlayer.AudioPlayerPackage
import com.humand.demo.modules.AudioRecorder.AudioRecorderPackage
import com.humand.demo.modules.FaceDetectionConfirmation.FaceDetectionConfirmationPackage
import com.humand.demo.modules.FullScreenNotificationPermission.FullScreenNotificationPermissionPackage
import com.humand.demo.modules.HMTextInput.HMTextInputPackage
import com.humand.demo.modules.IncomingCallMessagingService.rn.callModule.CallModulePackage
import com.humand.demo.modules.RNSwipeable.RNSwipeablePackage
import com.humand.demo.modules.Torch.TorchPackage
import com.humand.demo.modules.CallService.CallServicePackage
import com.humand.demo.modules.JSIInstaller.JSIInstallerPackage
import com.humand.demo.modules.LivestreamService.LivestreamServicePackage
import com.humand.demo.modules.VolumeButtonListener.VolumeButtonListenerPackage
import expo.modules.ApplicationLifecycleDispatcher
import expo.modules.ExpoReactHostFactory

class MainApplication : Application(), ReactApplication {

  override val reactHost: ReactHost by lazy {
    ExpoReactHostFactory.getDefaultReactHost(
      context = applicationContext,
      packageList =
        PackageList(this).packages.apply {
          add(FaceDetectionConfirmationPackage())
          add(TorchPackage())
          add(AudioRecorderPackage())
          add(FullScreenNotificationPermissionPackage())
          add(HMTextInputPackage())
          add(AudioPlayerPackage())
          add(RNSwipeablePackage())
          add(CallModulePackage())
          add(CallServicePackage())
          add(LivestreamServicePackage())
          add(VolumeButtonListenerPackage())
          add(JSIInstallerPackage())
        }
    )
  }

  override fun onCreate() {
    super.onCreate()
    EmojiCompat.init(BundledEmojiCompatConfig(this))
    try {
      DefaultNewArchitectureEntryPoint.releaseLevel = ReleaseLevel.valueOf(BuildConfig.REACT_NATIVE_RELEASE_LEVEL.uppercase())
    } catch (e: IllegalArgumentException) {
      DefaultNewArchitectureEntryPoint.releaseLevel = ReleaseLevel.STABLE
    }
    loadReactNative(this)
    ApplicationLifecycleDispatcher.onApplicationCreate(this)
  }

  override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)
    ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig)
  }
}
