package com.gh.gamecenter.game.upload

import android.app.Dialog
import android.content.DialogInterface
import android.os.Build
import android.os.Bundle
import android.view.*
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.common.DefaultJsApi
import com.gh.gamecenter.common.view.dsbridge.DWebView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.MtaHelper

/**
 * 光环助手资源协议
 */
class GameResourcePolicyDialogFragment : BaseDialogFragment() {

    var containerView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        containerView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_game_resource_policy, null, false)
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val mVebView = containerView?.findViewById<DWebView>(R.id.webView)
        val settings = mVebView?.settings
        settings?.javaScriptEnabled = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings?.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        // 避免提示网页有害信息不能访问
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            tryWithDefaultCatch {
                settings?.safeBrowsingEnabled = false
            }
        }
        DWebView.setWebContentsDebuggingEnabled(PackageFlavorHelper.IS_TEST_FLAVOR)

        // 适配大于屏幕宽度的页面
        settings?.useWideViewPort = true
        settings?.loadWithOverviewMode = true
        settings?.domStorageEnabled = true

        // 自适应屏幕
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            settings?.layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
        }
        mVebView?.enableForceDark(DarkModeUtils.isDarkModeOn(requireContext()))
        mVebView?.setTransparentBackground()
        mVebView?.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                val title = view?.title
                containerView?.findViewById<TextView>(R.id.titleTv)?.text = title
            }
        }
        mVebView?.addJavascriptObject(DefaultJsApi(requireContext()), null)
        mVebView?.loadUrl(requireContext().getString(R.string.upload_game_policy_url))
        containerView?.findViewById<View>(R.id.refuseTv)?.setOnClickListener {
            MtaHelper.onEvent("游戏上传", "开发者弹窗", "不同意")
            dismissAllowingStateLoss()
            requireActivity().finish()
        }
        containerView?.findViewById<View>(R.id.agreeTv)?.setOnClickListener {
            MtaHelper.onEvent("游戏上传", "开发者弹窗", "同意")
            dismissAllowingStateLoss()
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val createDialog = super.onCreateDialog(savedInstanceState)
        createDialog.setCanceledOnTouchOutside(false)
        createDialog.setOnKeyListener(object : DialogInterface.OnKeyListener {
            override fun onKey(dialog: DialogInterface?, keyCode: Int, event: KeyEvent?): Boolean {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true
                }
                return false
            }
        })

        val window = createDialog.window
        window?.setGravity(Gravity.CENTER)
//        window?.setWindowAnimations(R.style.community_publication_animation)
        return createDialog
    }

    override fun onStart() {
        super.onStart()
        val width = requireContext().resources.displayMetrics.widthPixels - 60F.dip2px()
        val height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
    }
}