package com.cooleshow.student.ui.splash

import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.cooleshow.base.bean.RouteBean
import com.cooleshow.base.common.ConstantKey
import com.cooleshow.base.router.RouterPath
import com.cooleshow.base.ui.activity.BaseActivity
import com.cooleshow.base.utils.GsonUtils
import com.cooleshow.base.utils.JumpUtils
import com.cooleshow.base.utils.SPUtils
import com.cooleshow.student.databinding.ActivitySplashLayoutBinding
import com.cooleshow.usercenter.constants.UserConstants
import com.cooleshow.usercenter.helper.UserHelper
import com.cooleshow.usercenter.ui.activity.VerifyCodeLoginActivity
import io.rong.imkit.utils.StatusBarUtil


/**
 * Author by pq, Date on 2022/4/19.
 */
class SplashActivity : BaseActivity<ActivitySplashLayoutBinding>() {
    override fun getLayoutView(): ActivitySplashLayoutBinding {
        return ActivitySplashLayoutBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.setStatusBarDarkTheme(this, true)
    }

    override fun initView() {
    }

    override fun initData() {
        super.initData()
        val isFirstLaunch = SPUtils.getInstance().getBoolean(ConstantKey.IS_FIRST_LAUNCH, false)
        if (isFirstLaunch) {
            if (UserHelper.isLogin()) {
                //接收h5跳转意图
                val uri: Uri? = intent.data
                if (uri != null && uri.isHierarchical) {
                    val params: String = uri.getQueryParameter("url").toString()
                    if (!TextUtils.isEmpty(params)) {
                        val routeBean = GsonUtils.fromJson(params, RouteBean::class.java)
                        if (JumpUtils.jump(routeBean)) {
                            return
                        }
                    }
                }
                jumpMain()
            } else {
                jumpLogin()
            }
        } else {
            goGuide();
        }

        finish()
    }

    private fun goGuide() {
        ARouter.getInstance().build(RouterPath.SplashCenter.SPLASH_GUIDE).navigation()
    }

    private fun jumpMain() {
        ARouter.getInstance().build(RouterPath.APPCenter.PATH_HOME).navigation()
    }

    private fun jumpLogin() {
        ARouter.getInstance().build(RouterPath.UserCenter.PATH_VERIFY_LOGIN)
            .withInt(UserConstants.LOGIN_ITEM, VerifyCodeLoginActivity.STUDENT_ITEM)
            .navigation()
    }

}