package com.adealink.weparty.webview

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
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.router.annotation.RouterUri
import com.adealink.frame.share.shareManager
import com.adealink.weparty.R
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.databinding.ActivityWebviewBinding
import com.adealink.weparty.module.webview.Web
import com.adealink.weparty.util.QMUIStatusBarHelper
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.webview.callback.IWebViewFragmentCallback
import com.adealink.weparty.webview.constant.NavigationBarMode

@RouterUri(path = [Web.FullScreen.PATH], desc = "全屏web页")
class WebViewActivity : BaseActivity(), IWebViewFragmentCallback {

    @BindExtra(name = Web.Common.EXTRA_URL, desc = "链接")
    var url: String? = null

    @BindExtra(name = Web.Common.EXTRA_SHOW_TOPBAR, desc = "显示顶部栏")
    var showTopbar: Boolean = false

    @BindExtra(name = Web.Common.EXTRA_SAFE_AREA, desc = "控制安全区域(顶部状态栏)")
    var safeArea: Boolean = false

    @BindExtra(Web.Common.EXTRA_LOADING_URL, desc = "加载图标链接")
    var loadingUrl: String? = null

    @BindExtra(Web.Common.EXTRA_DIALOG_TAG, desc = "dialog的tag")
    var fgTag: String? = ""


    private val binding by viewBinding(ActivityWebviewBinding::inflate)

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

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        setContentView(binding.root)
        if (showTopbar) {
            binding.topBar.show()
            this@WebViewActivity.applyImmersive { top ->
                binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                    topMargin = top
                }
            }
            binding.root.fitsSystemWindows = false
        } else {
            binding.topBar.gone()
            binding.root.fitsSystemWindows = safeArea
        }

        webFragment.arguments = Bundle().apply {
            putString(Web.Common.EXTRA_URL, url)
            putString(Web.Common.EXTRA_LOADING_URL, loadingUrl)
            putString(Web.Common.EXTRA_DIALOG_TAG, fgTag)
        }
        webFragment.replaceAndShow(supportFragmentManager, R.id.v_webview, url ?: "")
        webFragment.setWebFragmentCallback(this)
    }

    override fun closeWebView() {
        finish()
    }

    override fun setTitle(title: String?) {
        binding.topBar.setTitle(title ?: "")
    }

    override fun setImmersionMode() {
        binding.topBar.setToTransparentMode()
        this@WebViewActivity.applyImmersive { top ->
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
        binding.root.fitsSystemWindows = false
        setNavigationBarMode(NavigationBarMode.DARK)
    }

    override fun setNavigationBarMode(mode: NavigationBarMode) {
        when (mode) {
            NavigationBarMode.DARK -> {
                QMUIStatusBarHelper.setStatusBarDarkMode(this)
                setNavigationBarColor(R.color.black)
            }

            NavigationBarMode.LIGHT -> {
                QMUIStatusBarHelper.setStatusBarLightMode(this)
            }
        }
    }

    override fun hideNavigationBar() {
        binding.topBar.visibility = View.GONE
        binding.root.fitsSystemWindows = false
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        webFragment.onActivityResult(requestCode, resultCode, data)
        shareManager.onActivityResult(requestCode, resultCode, data)
//        WalletModule.onActivityResult(requestCode, resultCode, data)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (webFragment.onKeyDown(keyCode, event)) {
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

}