package com.sinata.bc_chat

import android.content.Intent
import android.os.Handler
import android.os.Looper
import com.bc_chat.account.GuideActivity
import com.bc_chat.account.task.SplashTask
import com.bc_chat.bc_base.entity.SplashAndAdImage
import com.bc_chat.bc_base.utils.Const
import com.zhaohaoting.framework.abs.BaseNoActionBarActivity
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.requset.NetWorkCallBack
import com.zhaohaoting.framework.utils.observer.Observer
import com.zhaohaoting.framework.utils.observer.ObserverManager

/**
 * author：Created by zhaohaoting on 2019/3/19
 * email：526309416@qq.com
 * desc：
 */
class SplashActivity : BaseNoActionBarActivity<BaseContract.BasePresenter>() {

    private val handler: Handler = Handler(Looper.getMainLooper())

    override fun initPresenter(): BaseContract.BasePresenter? = null

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

    override fun onInit() {
        setLightMode(resources.getColor(com.bc_chat.account.R.color.transparent), false, true)
        taskHelper.execute<Any>(SplashTask(), object : NetWorkCallBack<SplashAndAdImage>() {
            override fun onPostExecuteFailure(task: Any, exception: Exception) {
                super.onPostExecuteFailure(task, exception)
                handler.postDelayed({ enterGuide(SplashAndAdImage()) }, 100)
            }

            override fun onPostExecuteSuccess(task: Any, splashImages: SplashAndAdImage?) {
                handler.postDelayed({ enterGuide(splashImages) }, 100)
            }
        })
    }

    private fun enterGuide(splashImages: SplashAndAdImage?) {
        val intent = Intent(this, GuideActivity::class.java)
        intent.putExtra("splashImages", splashImages)
        startActivity(intent)
        finish()
    }

    private val observer = Observer<Any> { key, var1 ->
        handler.removeCallbacksAndMessages(null)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)
        ObserverManager.getInstance().unregisterObserver(Const.Action.NOTICE_START_MAIN, observer)
    }
}