package com.adealink.weparty.setting.about

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.AppBase
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.PackageUtil
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.module.setting.Setting
import com.adealink.weparty.module.webview.Web
import com.adealink.weparty.setting.R
import com.adealink.weparty.setting.databinding.ActivityAboutBinding
import com.adealink.weparty.update.updateManager
import com.adealink.weparty.url.UrlConfig
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Setting.About.PATH],
    desc = "关于"
)
class AboutActivity : BaseActivity() {

    private val binding by viewBinding(ActivityAboutBinding::inflate)

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

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.setTitle(getCompatString(R.string.setting_about, AppBase.appName))
            binding.topBar.setPadding(0, top, 0, 0)
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                height = top + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt()
            }
        }
        binding.tvVersion.text = PackageUtil.getVersionName()

        binding.vPrivacyPolicy.onClick {
            goPrivacyPolicy()
        }
        binding.vTermsService.onClick {
            goTermsService()
        }
        binding.vCommunityCovenant.onClick {
            goCommunityCovenant()
        }
        binding.vVersionUpdate.onClick {
            checkVersionUpdate()
        }

    }

    private fun goPrivacyPolicy() {
        Router.build(this, Web.FullScreen.PATH)
            .putExtra(
                Web.Common.EXTRA_URL,
                UrlConfig.privacyPolicy
            )
            .putExtra(Web.Common.EXTRA_SHOW_TOPBAR, true)
            .start()
    }

    private fun goTermsService() {
        Router.build(this, Web.FullScreen.PATH)
            .putExtra(
                Web.Common.EXTRA_URL,
                UrlConfig.serviceTerms
            )
            .putExtra(Web.Common.EXTRA_SHOW_TOPBAR, true)
            .start()
    }

    private fun goCommunityCovenant() {
        Router.build(this, Web.FullScreen.PATH)
            .putExtra(
                Web.Common.EXTRA_URL,
                UrlConfig.communityPolicy
            )
            .putExtra(Web.Common.EXTRA_SHOW_TOPBAR, true)
            .start()
    }

    private fun checkVersionUpdate() {
        updateManager.checkUpdate(this)
    }

}