package com.ym.game.activity

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.app.ActivityCompat
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.umeng.analytics.MobclickAgent
import com.ym.admodule.config.AdManager
import com.ym.admodule.listener.ZXADSplashListener
import com.ym.xynt.R
import com.ym.game.GameActivity
import com.ym.game.utils.CenterDialog
import com.ym.library.MyConstant
import com.ym.library.module.ReportEntity
import com.ym.library.net.MyBaseActivity
import com.ym.library.net.PermissionsActivity
import com.ym.library.net.SplashContract
import com.ym.library.net.SplashPresenter
import com.ym.library.net.SplashPresenter.Companion.PERMISSIONS
import com.ym.library.utils.*
import com.ym.modulecommon.base.BaseActivity
import com.ym.modulecommon.utils.AdPreference

class SplashActivity : MyBaseActivity(), SplashContract.View {

    override fun layoutID(): Int = R.layout.splash_layout

    private var mPresenter: SplashPresenter? = null
    private var tv_skipview: TextView? = null
    private var splash_container: FrameLayout? = null

    override fun init() {
        if (!isTaskRoot) {
            finish()
            return
        }
        MyConstant.ReportSessionId =
            "" + System.currentTimeMillis() + ReportUtils.getRandom(10)//上报使用的sessionId
        EventUtils.onEvent("app_start")
        ImmersionBar.with(this)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .init()
        tv_skipview = findViewById(R.id.tv_skipview)
        splash_container = findViewById(R.id.splash_container)
        mPresenter = SplashPresenter(this, this)
        if (SettingPreference.getPrivacyProtocol()) {
            CenterDialog.showPrivacyProtocol(
                this,
                View.OnClickListener {
                    SettingPreference.setPrivacyProtocol(true)
                    this.finish()
                },
                View.OnClickListener {
                    SettingPreference.setPrivacyProtocol(false)
                    mPresenter?.checkPermissions()//检测权限

                })
        } else {
            mPresenter?.checkPermissions()//检测权限
        }

    }

    override fun jumpPage() {
        if (!SettingPreference.getIsShowGuide()) {
            EventUtils.onEvent("SplashActivity_loadSplash")
            AdManager.loadSplashAd(this,
                ADConfig.AD_SPLASH,
                "splash",
                tv_skipview,
                splash_container!!,
                object : ZXADSplashListener {
                    override fun onAdShow() {}

                    override fun onAdSkip() {
                        startMain()
                    }

                    override fun onError(errorMsg: String?) {
                        startMain()
                    }
                })
        } else {
            startMain()
        }
    }

    private var startTime = 0L

    private var isClick = true

    //防止两次点击
    private fun startMain() {
        EventUtils.onEvent("SplashActivity_startMain")
        //用户点击了 跳过按钮   .......
        if (System.currentTimeMillis() - startTime > 2000 && isClick) {
            isClick = false
            startTime = System.currentTimeMillis()
//            startActivity(Intent(this@SplashActivity, GameActivity::class.java))
            val token = AdPreference.getToken()
            if (token.isEmpty()) {
                EventUtils.onEvent("login_token_null", "未登录")
                startActivity(Intent(this@SplashActivity, WxLoginActivity::class.java))
                finish()
            } else {
                EventUtils.onEvent("login_token_not_null", "已登录")
                startActivity(Intent(this@SplashActivity, GameActivity::class.java))
                finish()
            }

//            UserApiClient.userApi.getIsBindWithdrawWx(token)
//                .compose(RxSchedulers.observableIO2Main())
//                .subscribe(object : BaseObserver<UserInfoEntity>() {
//                    override fun onSuccess(data: UserInfoEntity) {
//                        act?.runOnUiThread {
//                            if (data?.isBind == true) {
//                                EventUtils.onEvent("login_net", "微信已绑定")
//                                startActivity(Intent(this@SplashActivity, GameActivity::class.java))
//                                finish()
//                            } else {
//                                EventUtils.onEvent("login_net", "微信未绑定,跳转微信登录页面")
//                                startActivity(Intent(this@SplashActivity, WxLoginActivity::class.java))
//                                finish()
//                            }
//                        }
//                    }
//                    override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
//                        if (null != errorMsg) {
//                            EventUtils.onEvent("login_net", "接口失败,跳转微信登录页面")
//                            // mView.wxBindFailure(errorMsg?:"")
//                            startActivity(Intent(this@SplashActivity, WxLoginActivity::class.java))
//                            finish()
//                        }
//                    }
//                })
        }
    }

    override fun jumpPermissionsPage() {
        val intent =
            Intent(this, PermissionsActivity::class.java)
        intent.putExtra(
            PermissionsActivity.EXTRA_PERMISSIONS,
            PERMISSIONS
        )
        ActivityCompat.startActivityForResult(
            this,
            intent,
            MyConstant.REQUEST_CODE_ASK_PERMISSIONS,
            null
        )
    }

    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == MyConstant.REQUEST_CODE_ASK_PERMISSIONS && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            //TODO 有权限的时候
            Log.e("huang", "onActivityResult()")
            startMain()
        } else {
            mPresenter?.getAppConfig()
        }
    }

    override fun getDestroyed(): Boolean {
        return false
    }

    override fun onResume() {
        super.onResume()
        EventUtils.onEvent(
            "page_status",
            Utils.obj2Str(ReportEntity(MyConstant.ReportSessionId, "SplashActivity", "onResume"))
        )
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        EventUtils.onEvent(
            "page_status",
            Utils.obj2Str(ReportEntity(MyConstant.ReportSessionId, "SplashActivity", "onPause"))
        )
        MobclickAgent.onPause(this)
        AdUtils.loadNextCacheRewardVideoAd(this)
        AdUtils.loadFeedCacheAdToCache(this)
    }
}