# Context.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2020-2023 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */
    package dk.nsp.mingraviditet.core.shared.extension

    import android.annotation.SuppressLint
    import android.content.Context
    import android.content.ContextWrapper
    import android.content.Intent
    import android.content.pm.ActivityInfo
    import android.content.res.Configuration
    import android.net.ConnectivityManager
    import android.net.NetworkCapabilities
    import androidx.activity.ComponentActivity
    import androidx.core.net.toUri
    import androidx.fragment.app.FragmentActivity

    fun Context.composeEmail(address: String, subject: String?) {
        val intent = Intent(Intent.ACTION_SENDTO).apply {
            data = "mailto:".toUri()
            putExtra(Intent.EXTRA_EMAIL, arrayOf(address))
            putExtra(Intent.EXTRA_SUBJECT, subject)
            flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
        }
        startActivity(Intent.createChooser(intent, "Choose email client"))
    }

    fun Context.openDialer(url: String) {
        val intent = Intent(Intent.ACTION_DIAL).apply {
            val telUrl = if (url.startsWith("tel:")) {
                url
            } else {
                "tel:$url"
            }
            data = telUrl.toUri()
        }
        startActivity(intent)
    }

    inline fun <reified Activity : FragmentActivity> Context.getActivity(): Activity? {
        return when (this) {
            is Activity -> this
            else -> {
                var context = this
                while (context is ContextWrapper) {
                    context = context.baseContext
                    if (context is Activity) return context
                }
                null
            }
        }
    }

    fun Context.findActivity(): ComponentActivity? = when (this) {
        is ComponentActivity -> this
        is ContextWrapper -> baseContext.findActivity()
        else -> null
    }

    fun Context.toggleOrientation() {
        val activity = findActivity() ?: return
        val newOrientation: Int = if (isInLandscape()) {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }

        activity.requestedOrientation = newOrientation
    }

    fun Context.isInLandscape(): Boolean {
        val orientation = resources.configuration.orientation
        return orientation == Configuration.ORIENTATION_LANDSCAPE
    }

    @SuppressLint("MissingPermission")
    fun Context.hasInternetConnection(): Boolean {
        val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        val network = connectivityManager.activeNetwork ?: return false
        val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
        return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
            capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
            capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
    }