/**
 * qsfera Android client application
 *
 * @author Juan Carlos Garrote Gascón
 *
 * Copyright (C) 2021 ownCloud GmbH.
 * <p>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.qsfera.android.presentation.settings.privacypolicy

import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.LinearLayout
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import eu.qsfera.android.R
import eu.qsfera.android.extensions.showMessageInSnackbar
import eu.qsfera.android.ui.activity.enableEdgeToEdgePostSetContentView
import eu.qsfera.android.ui.activity.enableEdgeToEdgePreSetContentView
import eu.qsfera.android.utils.PreferenceUtils
import timber.log.Timber

/**
 * Activity to show the privacy policy to the user
 */
class PrivacyPolicyActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // edge-to-edge
        enableEdgeToEdgePreSetContentView(false)

        setContentView(R.layout.activity_privacy_policy)

        // edge-to-edge
        enableEdgeToEdgePostSetContentView { insets ->
            findViewById<View>(R.id.qsfera_app_bar).updatePadding(top = insets.top)
        }

        val toolbar = findViewById<Toolbar>(R.id.standard_toolbar).apply {
            setTitle(R.string.actionbar_privacy_policy)
            isVisible = true
        }
        findViewById<ConstraintLayout>(R.id.root_toolbar).isVisible = false

        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        // Display the progress in a progress bar, like the browser app does.
        val progressBar = findViewById<ProgressBar>(R.id.syncProgressBar)

        // Allow or disallow touches with other visible windows
        findViewById<LinearLayout>(R.id.activityPrivacyPolicyLayout).apply {
            filterTouchesWhenObscured = PreferenceUtils.shouldDisallowTouchesWithOtherVisibleWindows(this@PrivacyPolicyActivity)
        }

        findViewById<WebView>(R.id.privacyPolicyWebview).apply {
            settings.javaScriptEnabled = true
            // Next two settings grant that non-responsive webs are zoomed out when loaded
            settings.useWideViewPort = true
            settings.loadWithOverviewMode = true
            // Enable zoom but hide display zoom controls
            settings.builtInZoomControls = true
            settings.displayZoomControls = false

            webChromeClient = object : WebChromeClient() {
                override fun onProgressChanged(view: WebView, progress: Int) {
                    progressBar.progress = progress //Set the web page loading progress
                    if (progress == 100) {
                        progressBar.isVisible = false
                    }
                }
            }

            webViewClient = object : WebViewClient() {
                override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
                    showMessageInSnackbar(message = getString(R.string.privacy_policy_error) + description)
                }

                override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                    val url = request?.url ?: return false
                    val scheme = url.scheme?.lowercase()
                    if (scheme == "http" || scheme == "https") return false
                    return try {
                        val intent = Intent(Intent.ACTION_VIEW, url).apply {
                            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        }
                        startActivity(intent)
                        true
                    } catch (e: ActivityNotFoundException) {
                        Timber.d(e, "No activity to handle %s", url)
                        false
                    }
                }
            }

            val urlPrivacyPolicy = resources.getString(R.string.url_privacy_policy)
            loadUrl(urlPrivacyPolicy)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        var retval = true
        if (item.itemId == android.R.id.home) {
            finish()
        } else {
            retval = super.onOptionsItemSelected(item)
        }
        return retval
    }

}
