package com.bc_chat.account

import android.content.Intent
import android.support.v4.view.ViewPager
import android.view.View
import android.view.WindowManager

import com.bc_chat.account.adapter.GuideAdapter
import com.bc_chat.account.databinding.ActGuideBinding
import com.bc_chat.account.task.SplashTask
import com.bc_chat.bc_base.entity.SplashAndAdImage
import com.bc_chat.bc_base.entity.SplashImage
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.Preferences
import com.zhaohaoting.framework.utils.observer.Observer
import com.zhaohaoting.framework.utils.observer.ObserverManager
import com.zhaohaoting.framework.view.indicator.IndicatorViewPager


/**
 * Created by xxh on 2018-4-11.
 */
class GuideActivity : BaseNoActionBarActivity<BaseContract.BasePresenter>(), View.OnClickListener {

    private var actGuideBinding: ActGuideBinding? = null
    private var guideAdapter: GuideAdapter? = null

    private val splashAndAdImage by lazy {
        intent.getParcelableExtra<SplashAndAdImage>("splashImages")
    }

    override fun initPresenter(): BaseContract.BasePresenter? {
        return null
    }

    override fun getLayoutResId(): Int {
        return R.layout.act_guide
    }

    private fun initData() {
        setLightMode(resources.getColor(R.color.transparent), true, false)
        actGuideBinding = rootDataBinding as ActGuideBinding
        actGuideBinding!!.button.setOnClickListener(this)
        initViewPager()
        val guidList = splashAndAdImage.guidList
        if (guidList.isNullOrEmpty()) {
            enterAd()
        } else {
            guideAdapter!!.notifyDataSetChanged(guidList)
            showButton(guidList.size == 1)
        }
    }

    override fun onClick(v: View) {
        Preferences.saveVersionName(BuildConfig.VERSION_CODE)
        enterAd()
    }

    private fun initViewPager() {
        guideAdapter = GuideAdapter(this)
        val indicatorViewPager = IndicatorViewPager(actGuideBinding!!.guideIndicator, actGuideBinding!!.viewpager)
        indicatorViewPager.adapter = guideAdapter

        actGuideBinding!!.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {
                showButton(position == guideAdapter!!.count - 1)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
    }

    private fun showButton(isShow: Boolean) {
        if (isShow) {
            actGuideBinding!!.button.visibility = View.VISIBLE
        } else {
            actGuideBinding!!.button.visibility = View.GONE
        }
    }

    override fun onInit() {
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        val lastVersion = Preferences.getVersionName()
        //        enterSplash();
        if (BuildConfig.VERSION_CODE > lastVersion) {
            //            第一次启动将当前版本进行存储
            initData()
        } else {
            //            非第一次启动直接跳转
            enterAd()
        }
        ObserverManager.getInstance().registerObserver(Const.Action.NOTICE_START_MAIN, observer)
    }

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

    private fun enterAd() {
        val intent = Intent(this, AdActivity::class.java)
        intent.putParcelableArrayListExtra("adImageList", splashAndAdImage.adList)
        startActivity(intent)
        finish()
    }

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