package com.swago.user

import android.content.Intent
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.swago.baseswago.UserVm
import com.swago.baseswago.activity.BaseXActivity
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.constant.UrlConstant
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.SpUtil
import com.swago.user.black.TabBlackActivity
import com.swago.user.databinding.ActivitySettingBinding
import com.tencent.qcloud.tim.uikit.TUIKit
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack

/**
 *@date 2021/8/27 14:52
 *description:
 */
@Route(path = ARouteConstant.User.setting)
class SettingActivity : BaseXActivity<ActivitySettingBinding>() {

    private val userVm by viewModels<UserVm>()

    override fun loadData() {
    }

    override fun initOther() {
        setBackView(binding.toolbar.ivBack)
        binding.toolbar.tvTitle.text = AppContext.getContext().resources.getString(R.string.setting)

        binding.tvVersion.text = getVersionName()

        binding.tvUserAgreement.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.Base.webView)
                    .withString("url", UrlConstant.USER_AGREEMENT)
                    .navigation()
            }
        })

        binding.tvUserPrivacy.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                ARouter.getInstance().build(ARouteConstant.Base.webView)
                    .withString("url", UrlConstant.USER_PRIVACY)
                    .navigation()
            }
        })

        binding.btLogout.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                userVm.logout()
            }
        })
    }

    override fun initLiveData() {
        userVm.logoutLiveData.observe(this) {
            TUIKit.logout(object : IUIKitCallBack {
                override fun onSuccess(data: Any?) {}
                override fun onError(module: String?, errCode: Int, errMsg: String?) {}
            })
            ARouter.getInstance().build(ARouteConstant.Login.login)
                .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .navigation()
        }
    }


    fun getVersionName(): String {
        return try {
            val context = AppContext.getContext()
            val packageManager = context.packageManager
            val info = packageManager.getPackageInfo(context.packageName, 0)
            return info.versionName
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }
}
