package com.zxhl.uc.page.fragment

import android.text.TextUtils
import android.view.View
import com.tencent.imsdk.v2.V2TIMCallback
import com.ym.chat.utils.TUIUtils
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.NetConfig
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.down.UpdateVersionManager
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.uc.VersionEntity
import com.zxhl.cms.utils.*
import com.zxhl.cms.widget.CenterDialog
import com.zxhl.uc.R
import kotlinx.android.synthetic.main.activity_layout_setting.*

class SettingActivity : BaseActivity(), UpdateCallback {
    override fun onClick(v: View?) {

    }

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    override fun layoutID(): Int {
        return R.layout.activity_layout_setting
    }

    override fun init() {
        id_img_back?.setOnClickListener {
            finish()
        }
        id_rl_black_list?.setOnClickListener {
            JumpUtils.BlockListJump()
        }
        id_rl_yinsizhengce?.setOnClickListener {
            JumpUtils.webJump("Privacy policy", NetConfig.H5.WEB_URL_PRIVACY)
        }
        id_rl_yonghu_xieyi?.setOnClickListener {
            JumpUtils.webJump("Terms of service", NetConfig.H5.WEB_URL_USER)
        }
        id_rl_zenzhi_xieyi?.setOnClickListener {
            JumpUtils.webJump("增值服务协议", NetConfig.H5.WEB_URL_BUYER_KNOW)
        }
        id_rl_update?.setOnClickListener {
            //版本更新
            val updateVersionManager = this?.let { it1 -> UpdateVersionManager(this, this) }
            updateVersionManager?.checkUpdate(true)
        }
        id_rl_about_us?.setOnClickListener {
            //关于我们
            JumpUtils.AbuoutUsJump()
        }
        id_rl_account_out?.setOnClickListener {
            CenterDialog.showCancellationDialog(this,View.OnClickListener {
                ApiClient.homeApi.authLogout().compose(RxSchedulers.observableIO2Main())
                    .subscribe(object : BaseObserver<Any?>() {
                        override fun onSuccess(dataBean: Any?) {
                            SettingPreference.saveToken("")
                            SettingPreference.saveUserInfo(null)
                            ActivityManager.getInstance().finishAllActivity()
                            JumpUtils.loginBeforeJump()
                            TUIUtils.logout(object : V2TIMCallback {
                                override fun onSuccess() {

                                }

                                override fun onError(p0: Int, p1: String?) {

                                }

                            })
                            finish()
                        }

                        override fun onFailure(e: Throwable, code: String, errorMsg: String?) {

                        }
                    })
            })
        }
        id_ll_login_out?.setOnClickListener {
            SettingPreference.saveToken("")
            SettingPreference.saveUserInfo(null)
            ActivityManager.getInstance().finishAllActivity()
            JumpUtils.loginBeforeJump()
            TUIUtils.logout(object : V2TIMCallback {
                override fun onSuccess() {

                }

                override fun onError(p0: Int, p1: String?) {

                }

            })
            finish()
        }
    }

    override fun CheckUpate(boolean: Boolean) {
        if (boolean == false) {
            showToast("当前已是最新版本!")
        }
    }
}