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

import android.os.Build
import android.os.Bundle
import android.text.Html
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.orhanobut.hawk.Hawk
import com.yidian.bcommon.AppConfig
import com.yidian.bcommon.HawkConfig
import com.yidian.bcommon.XRouterPathConstants
import com.yidian.bcommon.XRouterPathConstants.Companion.SYSTEM_SETTING
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.extensions.clickAntiShake
import com.yidian.bcommon.extensions.initTitleBar
import com.yidian.bcommon.http.bean.GetCommonConfigBean
import com.yidian.bcommon.services.AppUpgradeService
import com.yidian.bcommon.utils.SystemSettingUtils
import com.yidian.bcommon.widget.CommonInfoDialog
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.ProtocolActivity
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>() {

  companion object {
    const val EXTRA_COMMON_CONFIG = "commonConfig"
  }

  /**
   * TowerPro通用配置
   */
  private val commonConfig by lazy {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { serializable ->
      (serializable as HashMap<*, *>)[EXTRA_COMMON_CONFIG]?.let {
        it as GetCommonConfigBean.Data
      }
    }
  }

  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)
    // 根据TowerPro配置展示设置项
    viewBind.clCheckUpgrade.isVisible = commonConfig?.isShowAppUpgrade == 1
    viewBind.clFeedback.isVisible = commonConfig?.isShowFeedback == 1
  }

  private fun initClick() {
    // 通知设置
    viewBind.clNotice.clickAntiShake {
      SystemSettingUtils.openNotifySetting(this)
    }
    // 退出登录
    viewBind.tvLogout.clickAntiShake {
      showConfirmDialog()
    }
    // 检查app更新
    viewBind.clCheckUpgrade.clickAntiShake {
      ZapTicket(AppUpgradeService.SERVICE_NAME).withAction(AppUpgradeService.ACTION_CHECK_APP_UPGRADE).onResult {
        if (it.code != 0) ToastUtil.showToast(this, it.tips)
      }.ship()
    }
    // 用户协议
    viewBind.clProtocol.clickAntiShake {
      val urlMap = HashMap<String, String>()
      urlMap[ProtocolActivity.ProtocolUrlKey] = AppConfig.userProtocol
      urlMap[ProtocolActivity.ProtocolTitleKey] = resources.getString(R.string.user_protocol)
      XPageManager.push(XRouterPathConstants.PROTOCOL, urlMap)
    }
    // 隐私政策
    viewBind.clPrivacy.clickAntiShake {
      val urlMap = HashMap<String, String>()
      urlMap[ProtocolActivity.ProtocolUrlKey] = AppConfig.privacyPolicyStatement
      urlMap[ProtocolActivity.ProtocolTitleKey] = resources.getString(R.string.privacy_policy_statement)
      XPageManager.push(XRouterPathConstants.PROTOCOL, urlMap)
    }
    // 用户反馈
    viewBind.clFeedback.clickAntiShake {
      XPageManager.push(XRouterPathConstants.FEEDBACK, null)
    }
    // 注销账号
    viewBind.clLogout.clickAntiShake {
      CommonInfoDialog(
        this,
        resources.getString(R.string.logout_account),
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
          Html.fromHtml(resources.getString(R.string.logout_description), Html.FROM_HTML_MODE_COMPACT)
        } else {
          Html.fromHtml(resources.getString(R.string.logout_description))
        },
        arrayListOf(resources.getString(R.string.close)), null, 335
      ).show()
    }
  }

  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.setLeftBtnColor(ContextCompat.getColor(this, R.color.color_666666))
    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)
    }
  }

}
