package com.ym.userinfo.module.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.util.Log
import android.widget.CompoundButton
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
import com.qq.e.ads.nativ.NativeExpressADView
import com.umeng.analytics.MobclickAgent
import com.ym.admodule.config.AdManager
import com.ym.admodule.config.ZXADSizeConfig
import com.ym.library.Constant
import com.ym.library.down.UpdateVersionManager
import com.ym.library.module.NewsEntity
import com.ym.library.net.BaseActivity
import com.ym.library.utils.*
import com.ym.module.userinfo.R
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.layout_user_toolbar.*

class SettingActivity : BaseActivity() {
    override fun layoutID(): Int = R.layout.activity_setting

    override fun before() {
        super.before()
        StatusBarUtil.transportStatus(this, resources.getColor(R.color.transparent))
    }

    private var adView: Any? = null

    override fun init() {
        EventUtils.onEvent("show_setting", "设置页面曝光")
        adView = null
        if (SettingPreference.getUserInfoData() != null) {
            id_tv_id.text = "ID: ${SettingPreference.getUserInfoData().uid}"
        }
        if (Constant.qq != 0) {
            id_tv_qq.text = "官方QQ群: ${Constant.qq}"
        } else {
            id_tv_qq.text = ""
        }
        //复制
        id_iv_copy.setOnClickListener {
            copy()
        }

        id_notif_check.isChecked = SettingPreference.getNotice()
        //复选框
        id_notif_check.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { compoundButton, b ->
            SettingPreference.setNotice(b)
            if (b) {
                EventUtils.onEvent("notification_on", "开通知栏按钮点击")
                RxBus.get().post("refresh_login", "open_notif")
            } else {
                EventUtils.onEvent("notification_off", "关通知栏按钮点击")
                RxBus.get().post("refresh_login", "close_notif")
            }
        })

        rl_back.setOnClickListener { finish() }
        // id_iv_back.setOnClickListener { finish() }
        // tv_user_toolbar_title.text = "设置"
        //用户协议
        tv_setting_user_agreement.setOnClickListener {
            JumpUtils.h5Jump("用户协议", Constant.Param.USER, true, this)
        }
        //隐私条款
        tv_setting_privacy_policy.setOnClickListener {
            JumpUtils.h5Jump("隐私条款", Constant.Param.PRIVACY, true, this)
        }
        //关于我们
        tv_setting_about_us.setOnClickListener {
            JumpUtils.h5Jump("关于我们", Constant.Param.ABOUTUS, true, this)
        }
        //版本升级
        id_setting_update_version.setOnClickListener {
            val updateVersionManager = UpdateVersionManager(this)
            updateVersionManager?.checkUpdate(true)
        }

        AdUtils.showFeedAd(this,"setting",id_ad_frame_view)
    }

    private fun copy() {
        //获取剪贴板管理器：
        val cm: ClipboardManager =
            this?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建普通字符型ClipData
        var idDesc = "123456"
        if (SettingPreference.getUserInfoData() != null) {
            idDesc = SettingPreference.getUserInfoData()?.uid.toString()
        }
        val mClipData: ClipData =
            ClipData.newPlainText("Label", idDesc)
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData)
        Utils.showToast(this, "已复制到剪切板")
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)

        AdUtils.loadFeedCacheAdToCache(this)

    }

    override fun onDestroy() {
        super.onDestroy()
        if (adView != null) {
            if (adView is NativeExpressADView) {
                (adView as NativeExpressADView)?.destroy()
            } else if (adView is TTNativeExpressAd) {
                (adView as TTNativeExpressAd)?.destroy()
            }
        }
    }
}
