# WebViewHelper.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 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.util.android

    import android.annotation.SuppressLint
    import android.content.Context
    import android.content.res.AssetManager
    import android.content.res.Configuration
    import android.os.Build
    import android.webkit.WebSettings
    import android.webkit.WebView
    import androidx.webkit.WebSettingsCompat
    import androidx.webkit.WebViewFeature
    import timber.log.Timber
    import java.io.BufferedReader
    import java.io.IOException
    import java.io.InputStreamReader

    object WebViewHelper {

        fun createHtmlWithStyleSheet(rawHtml: String, context: Context): String {
            val htmlToShow = readAssetHeaderAndStyleHtml(context)
            return htmlToShow.replace("[CONTENT]", rawHtml)
        }

        private fun readAssetHeaderAndStyleHtml(context: Context): String {
            try {
                val inputStream =
                    context.assets.open("article_with_styles.html", AssetManager.ACCESS_BUFFER)
                inputStream.use { `is` ->
                    val reader = BufferedReader(InputStreamReader(`is`))
                    val result = StringBuilder()
                    var mLine: String? = reader.readLine()
                    while (mLine != null) {
                        result.append(mLine)
                        mLine = reader.readLine()
                    }
                    return result.toString()
                }
            } catch (e: IOException) {
                Timber.e(e, "Could not load html and style asset")
            }

            return ""
        }

        @SuppressLint("ClickableViewAccessibility")
        fun WebView.setUpDarkModeSupport() {
            // Support dark mode :)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                val nightModeFlag =
                    resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK

                when (nightModeFlag) {
                    Configuration.UI_MODE_NIGHT_YES -> {
                        settings.forceDark = WebSettings.FORCE_DARK_ON
                    }
                    Configuration.UI_MODE_NIGHT_NO -> {
                        settings.forceDark = WebSettings.FORCE_DARK_OFF
                    }
                    else -> {
                        settings.forceDark = WebSettings.FORCE_DARK_AUTO
                    }
                }

                if (nightModeFlag == Configuration.UI_MODE_NIGHT_YES) {
                    if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
                        WebSettingsCompat.setForceDarkStrategy(
                            settings,
                            WebSettingsCompat.DARK_STRATEGY_WEB_THEME_DARKENING_ONLY // Only use the injected CSS from the HTML
                        )
                    }
                }
            }
        }
    }