package com.renrenliao.im.ui.activity


import com.afollestad.assent.Permission
import com.afollestad.assent.askForPermissions
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ktx.immersionBar
import com.renrenliao.im.R
import com.renrenliao.im.base.BaseVMActivity
import com.renrenliao.im.utils.*
import com.renrenliao.im.viewmodel.SplashViewModel
import kotlinx.android.synthetic.main.activity_splash.*
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 **@ClassName SplashActivity
 **@Description
 **@Author ganzhe
 **@Date 2022/5/19 15:51
 *                   1 有 轮播完跳转到   3
 *  先查询广告缓存数据
 *
 *                                                                    有更新  更新
 *                   2 无  说明没登陆   》 3 获取存储权限  》 检查版本更新配置
 *                                                                                                  有 连接IM 跳转到首页
 *                                                                    无更新  》头像  》 检查用户信息  》
 *                                                                                                  无  跳转到登陆
 **/
class SplashActivity : BaseVMActivity<SplashViewModel>() {


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

    override fun initVM(): SplashViewModel = getViewModel()

    override fun initView() {

        immersionBar {
            transparentStatusBar()
            hideBar(BarHide.FLAG_HIDE_BAR)
        }
        DataCacheUtils.isLogin = false
    }

    override fun initData() {
        //  mViewModel.getAd()
       // mViewModel.test()
        val cacheAds = DataCacheUtils.getAD()
        if (cacheAds?.isNotEmpty() == true) {
            playAds()
        } else {
            getConfig()
        }
    }


    private fun playAds() {
        val list = listOf<String>("https://cdn.pixabay.com/photo/2016/12/23/12/40/night-1927265_960_720.jpg","https://img.iplaysoft.com/wp-content/uploads/2019/free-images/free_stock_photo.jpg")
        RxTimerUtils.countDown(0L,list.size*3L+1,1000) {
            if (it.toInt()%3 == 0&&it<list.size*3L){
                GlideUtils.loadOpenAdImg(list[(it/3).toInt()],iv)
            }
            if (it == 6L){
                //展示完获取配置
                getConfig()
            }
        }
    }

    //获取配置
    private fun getConfig() = askForPermissions(Permission.WRITE_EXTERNAL_STORAGE) {
        if (it.isAllGranted(Permission.WRITE_EXTERNAL_STORAGE)) {
            mViewModel.getAppConfig()
        } else {
            //dialog提示
        }

    }

    private fun goto() {
        val userInfo = DataCacheUtils.getUserInfo()
        if (userInfo != null) {
            RouteUtils.gotoMain(this)
            finish()
        } else {
            RouteUtils.gotoLogin(this)
            finish()
        }
    }

    override fun startObserve() {
        mViewModel.run {
            versionStatusLiveData.observe(this@SplashActivity) {
                when (it) {
                    0 -> {
                        LogUtils.error("最新版本不更新")
                        goto()
                    }
                    1 -> {
                        LogUtils.error("强制更新")
                    }
                    2 -> {
                        LogUtils.error("建议更新")
                        //点击取消  goto()  点击确定 更新
                    }
                    else -> {
                        LogUtils.error("最新版本不更新")
                        goto()
                    }
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        RxTimerUtils.cancel()
    }


}