package com.dahe.gldriver.ui

import android.os.Bundle
import android.view.WindowManager
import com.amap.api.location.AMapLocationClient
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.databinding.ActivityLauncherBinding
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.gldriver.ui.account.LoginActivity
import com.dahe.gldriver.ui.account.SelectRoleActivity
import com.dahe.gldriver.utils.CommonPopUtils
import com.dahe.gldriver.utils.SPUtils
import com.dahe.mylibrary.utils.BaseSPUtils
import com.dahe.mylibrary.utils.StringUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class LauncherActivity : BaseActivity<ActivityLauncherBinding>() {


    override fun initView(savedInstanceState: Bundle?) {
//        setStatusBarColorToLight2()
        val extras = intent.extras
        binding.llCount.setOnClickListener {
            mDisposable?.dispose()
            //已登录，直接跳转首页
//
            if (!SPUtils.instance.getUserToken(mContext).isNullOrEmpty()) {
                ActivityUtils.startActivity(mContext, HomeActivity::class.java, intent.extras)
            } else {
                ActivityUtils.startActivity(this@LauncherActivity, LoginActivity::class.java)
            }
//            ActivityUtils.startActivity(this@LauncherActivity, LoginActivity::class.java)
            finish()
        }

    }

    private var mDisposable: Disposable? = null

    override fun initDate() {
        val isFirstOpenApp = SPUtils.instance.getIsFirstOpenApp(mContext)
        if (isFirstOpenApp) {
            CommonPopUtils.getInstance().showCenterAgreement(mContext) {
                SPUtils.instance.setIsFirstOpenApp(mContext, false)
                AMapLocationClient.updatePrivacyShow(mContext, true, true)
                AMapLocationClient.updatePrivacyAgree(mContext, true)
                goNext()
            }
        } else {
            goNext()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mDisposable?.dispose()
    }

    private fun goNext() {
        val count = 3
        mDisposable = Observable.interval(0, 1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { aLong: Long ->
                binding.tvCount.setText((count - aLong).toString())
                if (count - aLong == 0L) {
                    mDisposable?.dispose()
                    //已登录，直接跳转首页
                    //已登录，直接跳转首页
                    if (!SPUtils.instance.getUserToken(mContext).isNullOrEmpty()) {
                        ActivityUtils.startActivity(
                            mContext,
                            HomeActivity::class.java
                        )
                    } else {
                        ActivityUtils.startActivity(
                            this@LauncherActivity,
                            LoginActivity::class.java
                        )
                        finish()
                    }
                }

            }
    }
}