package es.verdnatura

import android.app.Application
import android.graphics.Color
import android.media.MediaPlayer
import android.os.Handler
import android.os.Looper
import es.verdnatura.dataStore.DataStoreLocal
import es.verdnatura.di.viewModelModule
import es.verdnatura.domain.RestClient
import es.verdnatura.domain.SalixService
import es.verdnatura.domain.VerdnaturaService
import es.verdnatura.domain.toast
import es.verdnatura.presentation.common.InteceptorListener
import es.verdnatura.presentation.common.LoginListener
import es.verdnatura.presentation.common.MainActivityListener
import es.verdnatura.presentation.common.SignListener
import es.verdnatura.presentation.view.component.CustomDialogMainActivity
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.loadKoinModules
import org.koin.core.context.startKoin

class MobileApplication : Application(), InteceptorListener {
    private lateinit var interceptoreListener: InteceptorListener
    private var mainActivityListener: MainActivityListener? = null
    private var loginListener: LoginListener? = null
    private var signListener: SignListener? = null

    fun setMainListener(listener: MainActivityListener) {
        mainActivityListener = listener
    }

    fun setLoginListener(listener: LoginListener) {
        loginListener = listener
    }

    fun setSignListener(listener: SignListener) {
        signListener = listener
    }

    lateinit var silex: VerdnaturaService
    lateinit var salix: SalixService
    var userId: Int? = null
    var userName: String? = null
    var renewPeriod: Long = 0
    var renewInterval: Long = 0
    var userPassword: String? = null
    var hasNetwork: Boolean = true
    var serialNumber: String = ""
    var versionName: String = ""
    var androidId: String = ""
    var mperror: MediaPlayer? = null
    var mpok: MediaPlayer? = null
    var dataStoreApp = DataStoreLocal(this)
    private lateinit var customDialog: CustomDialogMainActivity
    private val handler = Handler(Looper.getMainLooper())

    override fun onCreate() {
        super.onCreate()
        mperror = MediaPlayer.create((this), R.raw.error)
        mpok = MediaPlayer.create((this), R.raw.ok)
        startKoin {
            androidContext(this@MobileApplication)
            val moduleList = listOf(viewModelModule)
            loadKoinModules(moduleList)
        }

        var restClient = RestClient(this, this)
        silex = restClient.restClient
        salix = restClient.salixClient

    }

    fun playSoundIsOK(bool: Boolean) {
        if (bool) mpok!!.start() else mperror!!.start()
    }

    fun messageToast(isError: Boolean, message: String) {
        val playSound = when (isError) {
            null -> null
            false -> mpok
            true -> mperror
        }
        playSound?.start()
        message.toast(
            this, color = if (isError) {
                Color.RED
            } else {
                Color.BLUE
            }
        )
    }

    override fun onInterceptionResult(visibility: Int) {
        if (mainActivityListener != null) {
            handler.post {
                (this.mainActivityListener)?.onApplicationEventAnimation(visibility)
            }
        }
        if (loginListener != null) {
            handler.post {
                (this.loginListener)?.onApplicationEventAnimation(visibility)
            }
        }
        if (signListener != null) {
            handler.post {
                (this.signListener)?.onApplicationEventAnimation(visibility)
            }
        }
    }

    fun getLanguage(): String {
        val settings = this.resources.configuration
        return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            settings.locales.get(0).language
        } else {
            @Suppress("DEPRECATION")
            settings.locale.language
        }
    }
}