package com.dahe.pass.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.WindowManager
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.StringUtils
import com.dahe.pass.R
import com.dahe.pass.bean.UserBean
import com.dahe.pass.ui.account.LoginActivity
import com.dahe.pass.utils.SPUtils
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 kotlinx.android.synthetic.main.activity_launcher.*
import java.util.concurrent.TimeUnit

class LauncherActivity : BaseActivity() {


    override fun getLayout(): Int {
        return R.layout.activity_launcher
    }


    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarCancelLightColor()
        val extras = intent.extras
        llCount.setOnClickListener {
            mDisposable?.dispose()
            //已登录，直接跳转首页
            val userInfo = SPUtils.getUserInfo(mContext)

            if (userInfo != null && StringUtils.isNotEmpty(userInfo.token)) {
                ActivityUtils.startActivity(mContext, HomeActivity::class.java,intent.extras)
            } else {
                ActivityUtils.startActivity(this@LauncherActivity, LoginActivity::class.java)
            }
            window.setFlags(
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            finish()
        }
        val count = 3
        mDisposable = Observable.interval(0, 1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { aLong: Long ->

                tv_count.setText((count - aLong).toString())
                if (count - aLong == 0L) {
                    mDisposable?.dispose()
                    //已登录，直接跳转首页
                    //已登录，直接跳转首页
                    val userInfo: UserBean? = SPUtils.getUserInfo(mContext)

                    if (userInfo != null && !TextUtils.isEmpty(userInfo.getToken())) {
                        ActivityUtils.startActivity(mContext, HomeActivity::class.java,intent.extras)
                    } else {
                        ActivityUtils.startActivity(
                            this@LauncherActivity,
                            LoginActivity::class.java
                        )

                    }
//                    window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
                    window.setFlags(
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

                    finish()
                }

            }
    }

    private var mDisposable: Disposable? = null

    override fun initDate() {
    }

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