package com.yidian.shenghuoquan.bsetting.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.constant.AppConfig
import com.yidian.bcommon.constant.HawkConfig
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.constant.XRouterPathConstants.Companion.SYSTEM_SETTING
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.bean.HintOptionBean
import com.yidian.bcommon.constant.IntentConstants
import com.yidian.bcommon.extensions.clickAntiShake
import com.yidian.bcommon.extensions.hide
import com.yidian.bcommon.extensions.initTitleBar
import com.yidian.bcommon.extensions.show
import com.yidian.bcommon.http.bean.GetCommonConfigBean
import com.yidian.bcommon.services.AppUpgradeService
import com.yidian.bcommon.utils.SensitiveInfoUtil
import com.yidian.bcommon.utils.SystemSettingUtils
import com.yidian.bcommon.webview.PublicWebViewActivity
import com.yidian.bcommon.widget.CommonInfoDialog
import com.yidian.bcommon.widget.dialog.HintDialog
import com.yidian.shenghuoquan.bsetting.BuildConfig
import com.yidian.shenghuoquan.bsetting.R
import com.yidian.shenghuoquan.bsetting.databinding.ActivitySystemSettingBinding
import com.yidian.shenghuoquan.bsetting.http.SettingRemoteDataSource
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>() {

  /**
   * TowerPro通用配置
   */
  private val commonConfig by lazy {
    intent.getSerializableExtra(XRouterPathConstants.ParamsKey)?.let { serializable ->
      (serializable as HashMap<*, *>)[IntentConstants.KEY_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() {
    if (BuildConfig.DEBUG) {
      viewBind.tvTest.show()
      viewBind.tvTest.clickAntiShake {
        XPageManager.push(XRouterPathConstants.DEMO_TEST_ACTIVITY, null)
      }
    } else {
      viewBind.tvTest.hide()
    }
    // 通知设置
    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 {
      PublicWebViewActivity.startWebViewActivity(this, AppConfig.userProtocol)
    }
    // 隐私政策
    viewBind.clPrivacy.clickAntiShake {
      PublicWebViewActivity.startWebViewActivity(this, AppConfig.privacyPolicy)
    }
    // 用户反馈
    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() {
          requestLogout()
        }
      })
    dialog.setLeftBtnColor(ContextCompat.getColor(this, R.color.color_666666))
    dialog.setRightBtnColor(ContextCompat.getColor(this, R.color.color_FF3A3A))
    dialog.show()
  }

  private fun requestLogout() {
    SettingRemoteDataSource.userLogout {
      if (it) {
        ToastUtil.showToast(this@SystemSettingActivity, "退出登录成功")
        XPageManager.push(XRouterPathConstants.LOGIN_LIFE_CIRCLE, null)
      }
    }
  }

}
