package com.yidian.shenghuoquan.newscontent.ui.setting

import android.os.Bundle
import androidx.core.content.ContextCompat
import com.orhanobut.hawk.Hawk
import com.yidian.common.HawkConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.XRouterPathConstants.Companion.SYSTEM_SETTING
import com.yidian.common.base.BaseActivity
import com.yidian.common.extensions.initTitleBar
import com.yidian.common.services.AppUpgradeService
import com.yidian.common.utils.SystemSettingUtils
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.bean.HintOptionBean
import com.yidian.shenghuoquan.newscontent.databinding.ActivitySystemSettingBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.callback.IUserLogoutCallBack
import com.yidian.shenghuoquan.newscontent.ui.dialog.HintDialog
import com.yidian.shenghuoquan.newscontent.utils.SensitiveInfoUtil
import com.yidian.utils.ToastUtil
import com.yidian.xarc.xbase.utils.AppUtils
import com.yidian.xpage.XPageManager
import com.yidian.yac.core.zap.ZapTicket


/**
 * 系统设置页面
 */
class SystemSettingActivity : BaseActivity<ActivitySystemSettingBinding>() {

  override fun createViewBinding(): ActivitySystemSettingBinding {
    return ActivitySystemSettingBinding.inflate(layoutInflater)
  }

  override fun getXPageName(): String {
    return SYSTEM_SETTING
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initClick()
    initView()
  }

  private fun initView() {
    initTitleBar(viewBind.include.toolbar, viewBind.include.tvTitle, resources.getString(R.string.setting))
    // 显示手机号
    val phone = Hawk.get(HawkConfig.Mobile, "")
    viewBind.tvUserMobile.text = SensitiveInfoUtil.mobileEncrypt(phone)
    // 显示版本号
    viewBind.tvAppVersion.text = AppUtils.INSTANCE.getCurrentVersionName(this)
  }

  private fun initClick() {
    // 通知设置
    viewBind.clNotice.setOnClickListener {
      SystemSettingUtils.openNotifySetting(this)
    }
    // 退出登录
    viewBind.tvLogout.setOnClickListener {
      showConfirmDialog()
    }
    // 检查app更新
    viewBind.clCheckUpgrade.setOnClickListener {
      ZapTicket(AppUpgradeService.SERVICE_NAME).withAction(AppUpgradeService.ACTION_CHECK_APP_UPGRADE).onResult {
        if (it.code != 0) ToastUtil.showToast(this, it.tips)
      }.ship()
    }
  }

  private fun showConfirmDialog() {
    val dialog = HintDialog(this, resources.getString(R.string.conform_logout),
      arrayListOf(HintOptionBean("取消"), HintOptionBean("确定")),
      object : HintDialog.OnHintOptionCallback {

        override fun onLeftOption() {
        }

        override fun onRightOption() {
          ApiService.userLogout(logoutCallBack)
        }
      })
    dialog.setRightBtnColor(ContextCompat.getColor(this, R.color.color_FFFF3A3A))
    dialog.show()
  }

  private val logoutCallBack = object : IUserLogoutCallBack {
    override fun userLogoutSuccess() {
      ToastUtil.showToast(this@SystemSettingActivity, "退出登录成功")
      XPageManager.push(XRouterPathConstants.LOGIN_LIFE_CIRCLE, null)
    }
  }

}
