package com.zxhl.main.page.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.zxhl.cms.common.Config
import com.zxhl.cms.common.Constant
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.SettingPreference
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
import com.zxhl.main.R
import com.zxhl.main.page.contract.SplashContract
import com.zxhl.main.page.presenter.SplashPresenter
import kotlinx.android.synthetic.main.activity_splash.*

/**
 * 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()) {
            if (TextUtils.equals(
                    Utils.getChannel(this),
                    "toutiao"
                ) || TextUtils.equals(Utils.getChannel(this), "toutiao_h5")
            ) {

                mPresenter = SplashPresenter(this@SplashActivity, this@SplashActivity)
                initListener()
            } else {
                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() {
        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), "toutiao_h5")){
                JumpUtils.loginJump()
            }else{
                mPresenter?.checkLogin()
            }

        } else {
            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)
    }
}