package com.bc_chat.account

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.widget.ImageView
import com.alibaba.android.arouter.launcher.ARouter
import com.bc_chat.account.databinding.ActivityAdBinding
import com.bc_chat.account.databinding.ActivitySplashBinding
import com.bc_chat.account.task.SplashTask
import com.bc_chat.bc_base.WebViewActivity
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.entity.SplashImage
import com.bc_chat.bc_base.utils.Const
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.zhaohaoting.framework.abs.BaseNoActionBarActivity
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.application.ZhtApplication
import com.zhaohaoting.framework.requset.NetWorkCallBack
import com.zhaohaoting.framework.utils.Logger
import com.zhaohaoting.framework.utils.Preferences
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 AdActivity : BaseNoActionBarActivity<BaseContract.BasePresenter>(), Runnable {

    private val binding by lazy {
        rootDataBinding as ActivityAdBinding
    }
    private val adImageList by lazy {
        intent.getParcelableArrayListExtra<SplashImage>("adImageList")
    }

    private var time = 4
    private var lastLoginUserId: String = "0"
    private var splashImage: SplashImage? = null
    private val handler: Handler = Handler(Looper.getMainLooper())

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

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

    override fun onInit() {
        setLightMode(resources.getColor(R.color.transparent), false, true)
        lastLoginUserId = Preferences.getString(Const.LAST_LOGIN_USER_ID, "0")
        if (adImageList.isNullOrEmpty()) {
            enterPage()
        } else {
            splashImage = adImageList[0]
            loadImageSplash(this@AdActivity, splashImage?.img, binding.ivCover)
        }
        binding.llSkip.setOnClickListener {
            enterPage()
        }
        binding.ivCover.setOnClickListener {
            if (splashImage == null) return@setOnClickListener
            WebViewActivity.startActivity(this, splashImage?.url, "详情", 0)
        }
        ObserverManager.getInstance().registerObserver(Const.Action.NOTICE_START_MAIN, observer)
    }

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

    private fun loadImageSplash(context: Context, url: String?, imageView: ImageView) {
        var url = url
        val options = RequestOptions()
                .error(R.mipmap.splash_)       //错误图
                // .priority(Priority.HIGH)
                .override(imageView.width, imageView.height)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        if (TextUtils.isEmpty(url)) url = null
        Glide.with(context).asDrawable().load(url).apply(options).into(imageView)
    }

    override fun run() {
        if (time > 0) {
            binding.tvTime.text = (--time).toString()
            handler.postDelayed(this, 1000)
        } else {
            enterPage()
        }
    }

    private fun enterPage() {
        if (!"0".equals(lastLoginUserId)) {
            ARouter.getInstance().build(RouteConfig.MAIN_ACTIVITY).navigation()
        } else {
            ARouter.getInstance().build(RouteConfig.LOGIN_ACTIVITY).navigation()
        }
        finish()
    }

    override fun onResume() {
        super.onResume()
        handler.postDelayed(this, 1000)
    }

    override fun onPause() {
        super.onPause()
        handler.removeCallbacksAndMessages(null)
    }

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

    }
}