package com.zxbw.modulemain.activity

import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.core.app.ActivityCompat
import com.umeng.analytics.MobclickAgent
import com.zxbw.modulemain.R
import com.zxbw.modulemain.contract.SplashContract
import com.zxbw.modulemain.presenter.SplashPresenter
import com.zxhl.cms.common.Config
import com.zxhl.cms.common.Constant
import com.zxhl.cms.common.NetConfig
import com.zxhl.cms.common.base.BaseActivity
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.box.AppInEntity
import com.zxhl.cms.router.RounterApi
import com.zxhl.cms.router.RounterBus
import com.zxhl.cms.utils.EventUtils
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.CenterDialog

/**
 * Created by gaoleichao on 2018/12/26
 */
class SplashActivity : BaseActivity(), SplashContract.View {

    override fun onClick(v: View?) {
    }

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

    private var mPresenter: SplashContract.Presenter? = null


    override fun init() {
        if (!isTaskRoot) {
            finish()
            return
        }
        EventUtils.onEvent("app_start")
        if (SettingPreference.getFirstApp()) {

            CenterDialog.showPrivacyProtocol(this, object : View.OnClickListener {
                override fun onClick(p0: View?) {
                    SettingPreference.setFirstApp()
                    mPresenter = SplashPresenter(this@SplashActivity, this@SplashActivity)
                    initListener()
                }
            })


        } else {
            mPresenter = SplashPresenter(this@SplashActivity, this@SplashActivity)
            initListener()
        }

    }

    private fun initListener() {
        getAppInfo()
        jumpMainPage()
//        id_activity_splash_skip?.setOnClickListener {
//            jumpMainPage()
//        }
    }

    override fun jumpMainPage() {
        val user = SettingPreference.getUserInfoData()
        val token = SettingPreference.getUserToken()
        if (user == null && TextUtils.isEmpty(token)) {
//                      if (TextUtils.equals(Utils.getChannel(this), "baichuan")) {
//                JumpUtils.loginJump()
//            } else {
//                mPresenter?.checkLogin()
//            }
            JumpUtils.loginJump()
        } else {
            // openApp()
            JumpUtils.activityJump(
                RounterBus.getRounter(RounterApi::class.java).getIntentActivityMain()
            )
        }


        finish()
    }

//    override fun jumpPermissionsPage() {
//        val intent = Intent(this, PermissionsActivity::class.java)
//        intent.putExtra(PermissionsActivity.EXTRA_PERMISSIONS, SplashPresenter.PERMISSIONS)
//        ActivityCompat.startActivityForResult(
//            this,
//            intent,
//            Constant.REQUEST_CODE_ASK_PERMISSIONS,
//            null
//        )
//    }
//
//    override fun getDestroyed(): Boolean {
//        return isDestroyed
//    }

//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        if (requestCode == Constant.REQUEST_CODE_ASK_PERMISSIONS && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
////            finish()
//            mPresenter?.checkLogin()
//        } else {
//            Config.init(this)
//            mPresenter?.checkLogin()
//        }
//    }

    private var isPause = false

//    override fun setToutiaoView(view: View) {
//        isPause = true
//        id_activity_splash_ad_layout?.removeAllViews()
//        id_activity_splash_ad_layout?.addView(view)
//    }


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

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

    fun openApp() {
        ApiClient.homeApi.openApp()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    Log.e("MXL", "进入app上报")
                }

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

                }
            })
    }

    private fun getAppInfo() {
        ApiClient.homeApi.getAppInfo()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<AppInEntity>() {
                override fun onSuccess(result: AppInEntity?) {
                    Constant.Switch.isOpenLogin = result?.login.equals("1")
                    Constant.Switch.isOpenPayTips = result?.risk_open.equals("1")
                    NetConfig.H5.WEB_URL_H5_PAY = result?.pay_h5_url
                    SettingPreference.saveConfig(result)
                }

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

                }
            })
    }
}