package com.base.browserwhite.ui.activity.webbrowser

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.net.Uri
import android.net.http.SslError
import android.util.Log
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import android.webkit.CookieManager
import android.webkit.JsResult
import android.webkit.SslErrorHandler
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebStorage
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.base.browserwhite.MyApplication
import com.base.browserwhite.bean.BookmarkBean
import com.base.browserwhite.bean.ConstObject.webPrivacy
import com.base.browserwhite.bean.DownloadBean
import com.base.browserwhite.bean.HistoryBean
import com.base.browserwhite.databinding.FragmentWebViewBinding
import com.base.browserwhite.ui.activity.download.DownloadAdapter
import com.base.browserwhite.ui.activity.download.DownloadDialog.showDownloadFinishDialog
import com.base.browserwhite.ui.activity.download.DownloadDialog.showDownloadVideoDialog
import com.base.browserwhite.ui.fragment.BaseFragment
import com.base.browserwhite.ui.views.PermissionDialog.showPermissionBottomSheet
import com.base.browserwhite.utils.ColorUtils
import com.base.browserwhite.utils.DownloadUtils
import com.base.browserwhite.utils.DownloadUtils.getDownloadJson
import com.base.browserwhite.utils.DownloadUtils.getDownloadJsonBean
import com.base.browserwhite.utils.DownloadUtils.saveDownloadRecordFile
import com.base.browserwhite.utils.LogEx
import com.base.browserwhite.utils.PermissionUtils.checkStorePermission
import com.base.browserwhite.utils.SpBeanUtils
import com.base.browserwhite.utils.SpBeanUtils.HISTORY_SP_KEY
import com.base.browserwhite.utils.Utils.dpToPx
import com.google.gson.Gson
import com.liulishuo.filedownloader.FileDownloader
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import kotlin.random.Random


class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {

    private val TAG = "WebViewFragment"
    var url: String = ""

    var bookmarkBean: BookmarkBean = BookmarkBean()

    override val binding: FragmentWebViewBinding by lazy {
        FragmentWebViewBinding.inflate(layoutInflater)
    }

    var onPageFinished: ((uri: String?) -> Unit)? = null

    private var downloadAdapter: DownloadAdapter? = null

    override fun setView() {

        initWebSettings()
        bookmarkBean.url = url
        loadWebView(url)
    }

    var downloadDialog: Dialog? = null
    override fun setListener() {
        super.setListener()
        binding.tvReload.setOnClickListener {
            reloadWebView()
        }
        binding.webView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            if (scrollY == 0) {
                binding.refreshLayout.setEnabled(true)
            } else {
                binding.refreshLayout.setEnabled(false)
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            lifecycleScope.launch {
                binding.refreshLayout.isRefreshing = true
                delay(Random.nextLong(1500, 2500))
                binding.refreshLayout.isRefreshing = false
            }
            reloadWebView()
        }

        binding.flDownload.setOnClickListener {
            if (!requireContext().checkStorePermission()) {
                val launcher = (requireActivity() as WebBrowserActivity).launcher
                requireContext().showPermissionBottomSheet(launcher){}
                return@setOnClickListener
            }

            if (downloadAdapter == null) {
                downloadAdapter = DownloadAdapter()
            }
            downloadAdapter?.let { adapter ->
                downloadDialog = requireContext().showDownloadVideoDialog(
                    adapter, downloadList,
                    dismissAction = {
                        requireContext().saveDownloadRecordFile(downloadList)
                        downloadDialog = null
                    },
                )
            }
        }


    }

    fun dropAndBounceButton(button: View) {
        // 初始位置
        button.isVisible = downloadList.isNotEmpty()

        val startTranslationY = button.translationY

        // 掉落位置
        val dropTranslationY = -(MyApplication.context.dpToPx(60f)).toFloat()
        val dropTranslationY1 = -(MyApplication.context.dpToPx(20f)).toFloat()
        val dropTranslationY2 = -(MyApplication.context.dpToPx(20f)).toFloat()

        // 回弹高度
        val animator = ValueAnimator.ofFloat(dropTranslationY, startTranslationY, dropTranslationY1, startTranslationY)
        animator.interpolator = AccelerateDecelerateInterpolator()
        animator.duration = 1000
        animator.addUpdateListener { animation ->
            // 更新按钮的位置
            button.translationY = animation.animatedValue as Float
        }
        animator.start()
        // 创建掉落动画

    }

    fun reloadWebView() {
        binding.llError.visibility = View.GONE
        binding.webView.visibility = View.VISIBLE
        binding.progressBar.visibility = View.VISIBLE
        binding.progressBar.progress = 0
        downloadList.clear()
        binding.flDownload.visibility = View.GONE
        binding.webView.reload()
    }

    fun loadWebView(loadUrl: String) {
        binding.llError.visibility = View.GONE
        binding.webView.visibility = View.VISIBLE
        LogEx.logDebug(TAG, "loadUrl=$loadUrl")
        binding.webView.loadUrl(loadUrl)
    }

    @SuppressLint("SetJavaScriptEnabled")
    fun initWebSettings() {

        val webSettings = binding.webView.settings
        webSettings.allowFileAccess = true// 设置允许访问文件数据
        webSettings.setSupportZoom(true)
        webSettings.builtInZoomControls = true
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        webSettings.domStorageEnabled = true
        webSettings.databaseEnabled = true
        @Suppress("DEPRECATION")
        webSettings.allowFileAccessFromFileURLs = true
        WebStorage.getInstance().deleteAllData()

        // 关键性代码，这里要给webView添加这行代码，才可以点击之后正常播放音频。记录一下。
        webSettings.mediaPlaybackRequiresUserGesture = false

        //设置WebView属性，能够执行Javascript脚本
        webSettings.javaScriptEnabled = true

        //设置混合内容模式：对于HTTPS和HTTP混合内容的加载，需要设置WebView以允许混合内容：
        webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW

        //使用CookieManager设置允许跨域Cookie
        CookieManager.getInstance().setAcceptThirdPartyCookies(binding.webView, true)

        //适应屏幕设置
        webSettings.useWideViewPort = true
        webSettings.loadWithOverviewMode = true

        //网页使用localStorage等Web存储API，需要启用DOM存储
        webSettings.domStorageEnabled = true
        webSettings.setUseWideViewPort(false);


        //设置WebChromeClient
        binding.webView.webChromeClient = object : WebChromeClient() {

            override fun onProgressChanged(view: WebView, newProgress: Int) {
                LogEx.logDebug(TAG, "onProgressChanged newProgress=$newProgress")

                binding.progressBar.progress = newProgress
                if (binding.progressBar.progress >= 100) {
                    binding.progressBar.visibility = View.GONE
                }
            }

            override fun onReceivedTitle(view: WebView?, title: String?) {
                super.onReceivedTitle(view, title)
                bookmarkBean.name = title ?: "about:black"
            }

            override fun onShowFileChooser(
                webView: WebView?,
                filePathCallback: ValueCallback<Array<Uri?>?>,
                fileChooserParams: FileChooserParams?
            ): Boolean {
                return true
            }

            override fun onJsAlert(
                view: WebView?,
                url: String?,
                message: String?,
                result: JsResult?
            ): Boolean = true
        }
        binding.webView.webViewClient = object : WebViewClient() {


            override fun onLoadResource(view: WebView?, url: String?) {
                super.onLoadResource(view, url)
                if (url?.endsWith(".mp4") == true) {
                    LogEx.logDebug(TAG, "onLoadResource url=$url")
                    addVideoLink(url)
                }
            }

            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                return super.shouldOverrideUrlLoading(view, request)
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                LogEx.logDebug(TAG, "onPageFinished")
                super.onPageFinished(view, url)
                val tittle = view?.title.toString()
                bookmarkBean.name = tittle
                bookmarkBean.url = url ?: ""
                onPageFinished?.invoke(url)
                onPageFinished = null

                if (!webPrivacy) {
                    //添加历史记录
                    SpBeanUtils.addSpBean(
                        HISTORY_SP_KEY,
                        HistoryBean(
                            name = tittle, url = url ?: "",
                            time = System.currentTimeMillis(),
                            color = ColorUtils.getRandomColor()
                        )
                    )
                }
            }

            override fun onReceivedSslError(
                view: WebView?,
                handler: SslErrorHandler?,
                error: SslError?
            ) {
                LogEx.logDebug(TAG, "onReceivedSslError")
                super.onReceivedSslError(view, handler, error)
                binding.webView.visibility = View.GONE
                binding.llError.visibility = View.GONE
                if (error != null) {
                    val domain = error.url
                    // 获取错误码
                    val primaryError = error.primaryError
                    // 根据错误码获取错误描述的中英文对照文本
                    val errorString = getSslErrorString(primaryError)
                    binding.tvErrorReason.text = errorString
                }
            }

            override fun onReceivedError(
                view: WebView?,
                request: WebResourceRequest?,
                error: WebResourceError?
            ) {
                LogEx.logDebug(TAG, "onReceivedError ${error?.errorCode} ${error?.description}")
                super.onReceivedError(view, request, error)
                return
            }
        }

        //设置Cookie
        val instance = CookieManager.getInstance()
        instance.setAcceptThirdPartyCookies(binding.webView, true)

    }


    override fun onAttach(context: Context) {
        super.onAttach(context)
    }

    private val downloadList = arrayListOf<DownloadBean>()


    @SuppressLint("NotifyDataSetChanged")
    fun addVideoLink(addUri: String) {
        val olderList = arrayListOf<DownloadBean>()
        val recordFile = requireContext().getDownloadJson()
        olderList.addAll(getDownloadJsonBean(recordFile, Gson()))
        olderList.forEach {
            it.status = FileDownloader.getImpl().getStatus(it.downloadId, it.path)
        }

        val olderBean = olderList.findLast { it.url == addUri }
        if (olderBean != null) {
            LogEx.logDebug(TAG, "old path=${olderBean.path} ${olderBean.downloadId}")
            downloadList.add(olderBean.apply { uiType = 2 })
        } else {
            downloadList.add(DownloadBean(url = addUri).apply {
                name = addUri.split("/").last()
                uiType = 2
            })
        }

        binding.tvDownloadNumber.text = downloadList.size.toString()
        dropAndBounceButton(binding.flDownload)
        downloadList.forEach {
            LogEx.logDebug(TAG, "${it.url} ${it.size} ${it.downloadId} ${it.path}")
        }

        lifecycleScope.launch(Dispatchers.IO) {
            downloadList.filter { it.size <= 0 }.forEach {
                fastGetSize(it)
            }
            launch(Dispatchers.Main) {
                downloadAdapter?.submitList(downloadList)
                downloadAdapter?.notifyDataSetChanged()
            }
        }
    }


    fun canGoBack(): Boolean {
        return binding.webView.canGoBack()
    }

    fun canGoForward(): Boolean {
        return binding.webView.canGoForward()
    }

    fun goBack() {
        return binding.webView.goBack()
    }

    fun goForward() {
        return binding.webView.goForward()
    }


    private fun getSslErrorString(error: Int): String {
        return when (error) {
            SslError.SSL_DATE_INVALID -> "Date Invalid: The certificate's date is incorrect or the certificate has expired."
            SslError.SSL_EXPIRED -> "Expired: The SSL certificate has expired."
            SslError.SSL_IDMISMATCH -> "Hostname Mismatch: The hostname in the certificate does not match the requested hostname."
            SslError.SSL_INVALID -> "Invalid: The certificate is invalid."
            SslError.SSL_NOTYETVALID -> "Not Yet Valid: The certificate is not yet valid."
            SslError.SSL_UNTRUSTED -> "Untrusted: The certificate authority is not trusted."
            else -> "Unknown Error"
        }
    }

    private fun fastGetSize(bean: DownloadBean) = runCatching {
        if (bean.size == -1L) {
            val client = OkHttpClient()

            LogEx.logDebug(TAG, "fastGetSize ${bean.url}")
            val request = Request.Builder()
                .url(bean.url)
                .head() // 发送 HEAD 请求
                .build()

            try {
                client.newCall(request).enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        // 处理请求失败的情况
                        LogEx.logDebug(TAG, "onFailure")
                    }

                    override fun onResponse(call: Call, response: Response) {
                        if (response.isSuccessful) {
                            val contentLength = response.header("Content-Length") // 获取内容长度
                            contentLength?.let {
                                // 将内容长度转换为 Long 类型
                                val videoSize = it.toLong()
                                // 在这里使用视频大小，例如在主线程更新 UI 或存储到变量中
                                bean.size = videoSize
                                LogEx.logDebug(TAG, "videoSize=$videoSize")
                            }
                        }
                    }
                })
            } catch (e: Exception) {
                // 处理异常情况
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        requireContext().saveDownloadRecordFile(downloadList)
        binding.webView.destroy()
    }

}