package com.duben.microtribe.ui.activitys

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.bytedance.hume.readapk.HumeSDK
import com.bytedance.sdk.dp.DPDrama
import com.bytedance.sdk.dp.DPSdk
import com.bytedance.sdk.dp.IDPWidgetFactory
import com.duben.microtribe.BuildConfig
import com.duben.microtribe.MintsApplication
import com.duben.microtribe.R
import com.duben.microtribe.common.Constant
import com.duben.microtribe.manager.TrackManager
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.utils.MateUtils
import com.duben.microtribe.utils.ToastUtil
import com.duben.library.utils.ConstantUtil
import com.duben.library.utils.json.JsonUtil
import com.duben.library.utils.nodoubleclick.AntiShake
import kotlinx.android.synthetic.main.activity_aboutus.*
import kotlinx.android.synthetic.main.header_layout.*

/**
 * 描述：关于我们
 * 作者：孟崔广
 * 时间：2020/9/23 18:39
 */
@SuppressLint("SetTextI18n")
class AboutusActivity : BaseActivity(), View.OnClickListener {

    override fun getContentViewLayoutID() = R.layout.activity_aboutus

    override fun isApplyKitKatTranslucency() = false

    override fun initViewsAndEvents() {
        tvAboutasVersion.text =
            getString(R.string.app_name) + " v" + ConstantUtil.getVersionName(context)
        tv_title.text = if (BuildConfig.DEBUG) "测试-关于我们" else "关于我们"
        iv_left_icon.visibility = View.VISIBLE
        iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)

        ivAboutasIcon.setOnLongClickListener {
            ToastUtil.showLong(
                this, "自有渠道：" + MateUtils.getAppMetaData(
                    MintsApplication.getContext(), "CHANNEL_NAME"
                ) +
                        "\n 头条渠道：" + HumeSDK.getChannel(context)
            )
            true
        }

        tvAboutasVersion.setOnLongClickListener {
            ToastUtil.showLong(this, "包名：" + Constant.MINTS_PKG_NAME)

//            TTMediationTestTool.launchTestTools(
//                this@AboutusActivity,
//                object : TTMediationTestTool.ImageCallBack {
//                   override fun loadImage(imageView: ImageView?, s: String?) {}
//                })
//            readyGo(NinePayActivity::class.java)
//            VoiceRedEnvelopeManager.load(23,23)
            true
        }

        initListener()
    }

    override fun onClick(v: View?) {
        if (AntiShake.check(v?.id)) return

        when (v?.id) {
            R.id.iv_left_icon -> finish()
            R.id.tvAboutasService -> {
                val bundle = Bundle()
                bundle.putString(WebActivity.WEB_TITLE, getString(R.string.register_name))
                bundle.putString(WebActivity.WEB_URL, Constant.REGISTER_URL)
                readyGo(WebActivity::class.java, bundle)
            }
            R.id.tvAboutasPolicy -> {
                val bundle = Bundle()
                bundle.putString(WebActivity.WEB_TITLE, getString(R.string.privacy_name))
                bundle.putString(WebActivity.WEB_URL, Constant.PRIVACY_URL)
                readyGo(WebActivity::class.java, bundle)
            }
        }
    }

    private fun initListener() {
        iv_left_icon.setOnClickListener(this)
        tvAboutasService.setOnClickListener(this)
        tvAboutasPolicy.setOnClickListener(this)

        findViewById<TextView>(R.id.tv_about_address).setOnLongClickListener {
            if (BuildConfig.DEBUG) {
                if (DPSdk.isStartSuccess()) {
                    DPSdk.factory().requestAllDrama(
                        1,
                        Int.MAX_VALUE,
                        true,
                        object : IDPWidgetFactory.DramaCallback {
                            override fun onError(code: Int, msg: String?) {
                                Log.d("AboutusActivity", "request failed, code = $code, msg = $msg")
                                Toast.makeText(this@AboutusActivity, "请求失败", Toast.LENGTH_SHORT)
                                    .show()

                            }

                            override fun onSuccess(
                                dataList: MutableList<out DPDrama>?,
                                info: MutableMap<String, Any>?
                            ) {
                                Toast.makeText(this@AboutusActivity, "请求成功", Toast.LENGTH_SHORT)
                                    .show()
                                val json = JsonUtil.toJson(dataList)
                                Log.d("AboutusActivity", "request success, drama  = " + json)
                                Log.d("AboutusActivity", "request success, dataList size  = " + dataList!!.size)
                                TrackManager.getInstance().commitCsjShotInfo(json)
                            }
                        })
                } else {
                    Toast.makeText(this@AboutusActivity, "sdk还未初始化", Toast.LENGTH_SHORT).show()
                }
            }

            return@setOnLongClickListener true

        }
    }
}
