package com.ym.game.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.widget.CompoundButton
import com.umeng.analytics.MobclickAgent
import com.ym.admodule.config.AdManager
import com.ym.admodule.config.ZXADSizeConfig
import com.ym.xynt.R
import com.ym.library.MyConstant
import com.ym.library.down.UpdateVersionManager
import com.ym.library.net.MyBaseActivity
import com.ym.library.utils.*
import com.ym.modulecommon.utils.PhoneUtils
import kotlinx.android.synthetic.main.activity_setting.*

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

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

    override fun init() {
        EventUtils.onEvent("setting_on", "设置页面曝光")
        if (SettingPreference.getUserInfoData() != null) {
            id_tv_id.text = "ID: ${SettingPreference.getUserInfoData().uid}"
        }
        if (MyConstant.qq != 0) {
            id_tv_qq.text = "官方QQ群: ${MyConstant.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() }
        iv_setting_back.setOnClickListener { finish() }
        // tv_user_toolbar_title.text = "设置"
        //提现记录
        ll_setting_withdraw_record.setOnClickListener {
            JumpUtils.jumpWithdrawalsRecordActivity()
        }
        //用户协议
        ll_setting_user_agreement.setOnClickListener {
            JumpUtils.h5Jump("用户协议", MyConstant.Param.USER, true, this)
        }
        //隐私条款
       ll_setting_privacy_policy.setOnClickListener {
            JumpUtils.h5Jump("隐私条款", MyConstant.Param.PRIVACY, true, this)
        }
        //关于我们
        ll_setting_about_us.setOnClickListener {
            JumpUtils.h5Jump("关于我们", MyConstant.Param.ABOUTUS, true, this)
        }
        //版本升级
        ll_setting_update_version.setOnClickListener {
            val updateVersionManager = UpdateVersionManager(this)
            updateVersionManager?.checkUpdate(true)
        }

        AdUtils.showFeedAd(this, "", id_ad_frame_view)

        AdManager.showFeedAd(
            ADConfig.AD_IMG_FADE,
            "setting",
            ZXADSizeConfig(Utils.px2dip(PhoneUtils.getScreenWidth(this).toFloat()) - 45, 280),
            this,
            id_ad_frame_view)

        id_ad_frame_view?.postDelayed({
            AdManager.loadFeedAdToCache(
                ADConfig.AD_IMG_FADE, this, ZXADSizeConfig(
                    Utils.px2dip(PhoneUtils.getScreenWidth(this).toFloat()) - 45, 280)
            )
        },1500)
    }

    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)

    }

}
