package com.all.pdfreader.pro.app.ui.act

import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import com.all.pdfreader.pro.app.R
import com.all.pdfreader.pro.app.databinding.ActPrivacyPolicyBinding

class PrivacyPolicyActivity : BaseActivity() {
    private lateinit var binding: ActPrivacyPolicyBinding

    override val TAG = "PrivacyPolicyActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActPrivacyPolicyBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.backBtn.setOnClickListener {
            finish()
        }

        setupWebView()
        loadPrivacyPolicy()
    }

    private fun setupWebView() {
        binding.webView.apply {
            webViewClient = object : WebViewClient() {
                override fun onPageStarted(view: WebView?, url: String?, favicon: android.graphics.Bitmap?) {
                    super.onPageStarted(view, url, favicon)
                    // 页面开始加载，显示进度条
                    binding.progressBar.visibility = android.widget.ProgressBar.VISIBLE
                }

                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
                    // 页面加载完成，隐藏进度条
                    binding.progressBar.visibility = android.widget.ProgressBar.GONE
                }

                override fun onReceivedError(
                    view: WebView?,
                    request: WebResourceRequest?,
                    error: WebResourceError?
                ) {
                    super.onReceivedError(view, request, error)
                    // 显示错误页面或提示，隐藏进度条
                    binding.progressBar.visibility = android.widget.ProgressBar.GONE
                    loadErrorPage()
                }
            }
            webChromeClient = WebChromeClient()
            settings.javaScriptEnabled = false
            settings.builtInZoomControls = true
            settings.displayZoomControls = false
            settings.loadWithOverviewMode = true
            settings.useWideViewPort = true
        }
    }

    private fun loadPrivacyPolicy() {
        val privacyPolicyUrl = "https://www.explore-sun.com/privacy-policy/pdf-reader-pro.html"
        
        binding.webView.loadUrl(privacyPolicyUrl)
    }

    private fun loadErrorPage() {
        val errorHtml = """
            <html>
            <head>
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <style>
                    body { 
                        font-family: Arial, sans-serif; 
                        padding: 40px 20px; 
                        line-height: 1.6;
                        color: #666;
                        text-align: center;
                        background-color: #f8f9fa;
                    }
                    .error-container {
                        max-width: 400px;
                        margin: 0 auto;
                        padding: 40px;
                        background-color: white;
                        border-radius: 8px;
                        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
                    }
                    h1 { color: #e74c3c; margin-bottom: 16px; font-size: 24px; }
                    p { margin-bottom: 20px; }
                    .retry-btn {
                        background-color: #3498db;
                        color: white;
                        padding: 10px 20px;
                        border: none;
                        border-radius: 4px;
                        cursor: pointer;
                        text-decoration: none;
                        display: inline-block;
                    }
                </style>
            </head>
            <body>
                <div class="error-container">
                    <h1>Network connection failed</h1>
                    <p>Unable to load privacy policy content. Please check your network connection.</p>
                    <a href="javascript:window.location.reload()" class="retry-btn">Reload</a>
                </div>
            </body>
            </html>
        """.trimIndent()
        binding.webView.loadDataWithBaseURL(null, errorHtml, "text/html", "utf-8", null)
    }

    @Deprecated("Deprecated in Java")
    override fun onBackPressed() {
        if (binding.webView.canGoBack()) {
            binding.webView.goBack()
        } else {
            super.onBackPressed()
        }
    }
}