# 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.content.Context
    import android.content.ContextWrapper
    import android.content.Intent
    import android.net.Uri
    import androidx.activity.ComponentActivity
    import androidx.fragment.app.FragmentActivity

    fun Context.composeEmail(address: String, subject: String?) {
        val intent = Intent(Intent.ACTION_SENDTO).apply {
            data = Uri.parse("mailto:")
            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 = Uri.parse(telUrl)
        }
        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
    }