package com.adealink.weparty.webview

import android.content.DialogInterface
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.WindowManager
import androidx.fragment.app.FragmentManager
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.util.DisplayUtil
import com.adealink.weparty.R
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.databinding.DialogWebviewBinding
import com.adealink.weparty.module.webview.Web
import com.adealink.weparty.module.webview.Web.Common.Companion.EXTRA_HEIGHT
import com.adealink.weparty.module.webview.Web.Common.Companion.EXTRA_TOP_RADIUS
import com.adealink.weparty.webview.callback.IWebViewFragmentCallback

class WebViewDialogFragment : BottomDialogFragment(R.layout.dialog_webview),
    IWebViewFragmentCallback {

    private val binding by viewBinding(DialogWebviewBinding::bind)

    private var url: String = ""

    @BindExtra(EXTRA_HEIGHT, desc = "高度")
    var heightPx = 0
        set(value) {
            field = value
            if (isAdded) {
                resetDialogAttributes()
            }
        }

    @BindExtra(Web.Common.EXTRA_TRANSPARENT, desc = "是否背景透明")
    var transparent: Boolean? = false

    private val webFragment: WebViewFragment by fastLazy { WebViewFragment() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        webFragment.arguments = this.arguments
        webFragment.setWebFragmentCallback(this)
        webFragment.replaceAndShow(childFragmentManager, R.id.v_webview, url)

        val topRadius = arguments?.getInt(EXTRA_TOP_RADIUS) ?: 16.dp()
        val bgDrawable = GradientDrawable().apply {
            shape = GradientDrawable.RECTANGLE
            setColor(getCompatColor(R.color.white))
            cornerRadii = floatArrayOf(
                topRadius.toFloat(), topRadius.toFloat(),
                topRadius.toFloat(), topRadius.toFloat(),
                0f, 0f,
                0f, 0f,
            )
        }
        binding.root.background = bgDrawable

        dialog?.setOnKeyListener { _: DialogInterface?, keyCode: Int, keyEvent: KeyEvent ->
            if (webFragment.onKeyDown(keyCode, keyEvent)) {
                return@setOnKeyListener true
            }
            false
        }
        if (transparent == true) {
            binding.root.setBackgroundColor(getCompatColor(R.color.transparent))
        }
    }

    override fun onStart() {
        super.onStart()
        resetDialogAttributes()
    }

    private fun resetDialogAttributes() {
        val dialog = dialog ?: return
        val window = dialog.window ?: return
        val layoutParams = window.attributes
        layoutParams.gravity = Gravity.BOTTOM
        layoutParams.flags = layoutParams.flags and WindowManager.LayoutParams.FLAG_DIM_BEHIND.inv()
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
        layoutParams.height = getRealHeight()
        layoutParams.dimAmount = dimAmount
        window.attributes = layoutParams
    }

    private fun getRealHeight(): Int {
        var finalHeight = heightPx
        val window = dialog?.window
        if (window != null && heightPx > DisplayUtil.getScreenHeight() - DisplayUtil.getStatusBarHeight(
                window
            )
        ) {
            finalHeight = DisplayUtil.getScreenHeight() - DisplayUtil.getStatusBarHeight(window)
        }
        return if (finalHeight > 0) finalHeight else (DisplayUtil.getScreenHeight() * 0.5).toInt()
    }

    fun showUrl(fragmentManager: FragmentManager, url: String) {
        this.url = url
        if (isAdded) {
            if (webFragment.isAdded) {
                webFragment.showUrl(url)
            }
        } else {
            show(fragmentManager)
        }
    }


    override fun closeWebView() {
        dismiss()
    }

}