package com.abbidot.tracker.ui.activity.set

import android.text.TextUtils
import android.view.View
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.databinding.ActivityAboutBinding
import com.abbidot.tracker.ui.activity.WebViewActivity
import com.abbidot.tracker.util.ViewUtil
import com.qmuiteam.qmui.util.QMUIPackageHelper
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView

class AboutActivity : BaseActivity<ActivityAboutBinding>(ActivityAboutBinding::inflate) {

    private val mWebsiteUrl = "www.abbidot.com"

    override fun getTopBar() = mViewBinding.ilAboutTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_setting_about)
        setLeftBackImage(R.drawable.icon_white_back_svg)

        val version = QMUIPackageHelper.getAppVersion(mContext)
        val versionStr = String.format(getString(R.string.txt_about_version), ": V$version")

        mViewBinding.apply {
            tvAboutVersion.text = versionStr

            //服务协议
            setButtonData(aboutServiceContract.trackerManageSettingItem,
                    getString(R.string.txt_service_contract))
            //隐私政策
            setButtonData(aboutPrivacyPolicy.trackerManageSettingItem,
                    getString(R.string.txt_privacy_policy))
            //官网
            setButtonData(aboutOfficialWebsite.trackerManageSettingItem,
                    getString(R.string.txt_website), mWebsiteUrl)
        }
    }

    private fun setButtonData(commonItemView: QMUICommonListItemView,
                              title: String,
                              content: String = "") {
        commonItemView.apply {
            text = title
            if (!TextUtils.isEmpty(content)) detailText = content
            setOnClickListenerViews(this)
        }
    }


    override fun onClick(v: View?) {
        when (v!!.id) {
            //服务协议
            R.id.about_service_contract -> ViewUtil.instance.goServiceContract(mActivity)
            //隐私政策
            R.id.about_privacy_policy -> ViewUtil.instance.goPrivacyPolicy(mActivity)
            //官网
            R.id.about_official_website -> WebViewActivity.startWebViewActivity(
                mActivity,
                    getString(R.string.txt_website), mWebsiteUrl)
        }
    }
}