package com.duben.microtribe.ui.activitys

import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import androidx.viewpager.widget.ViewPager
import com.duben.microtribe.R
import com.duben.microtribe.manager.UserManager
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.ui.adapter.VPAdapter
import kotlinx.android.synthetic.main.activity_guide.*

/**
 * 欢迎页
 */
class GuideActivity : BaseActivity() {

    var count = 0
    var layouts = intArrayOf(
        R.layout.item_guide1,
        R.layout.item_guide2,
        R.layout.item_guide3
    )

    override fun getContentViewLayoutID(): Int {
        return R.layout.activity_guide
    }

    override fun initViewsAndEvents() {
        initView()
    }

    override fun isApplyKitKatTranslucency(): Boolean {
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        vp_guide_viewpager.setOnPageChangeListener(null)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            true
        } else super.onKeyDown(
            keyCode,
            event
        )
    }

    private fun initView() {
        pageIndicatorView.count = 4
        val viewPagerAdapter = VPAdapter(this, layouts)
        vp_guide_viewpager.adapter = viewPagerAdapter
        vp_guide_viewpager.setOnPageChangeListener(mOnPagerChangeListener)
        btn_guide_start.setOnClickListener {
            vp_guide_viewpager.currentItem = ++count
            if (count == layouts.size) {
                if (TextUtils.isEmpty(UserManager.getInstance().userID)) {
                    readyGoThenKill(MainActivity::class.java)
                } else {
                    val bundle = Bundle()
//                    bundle.putBoolean(VipActivity.IS_GUIDE, true)
                    readyGoThenKill(VipActivity::class.java, bundle)
                }
                //带点渐变动画
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }
        }
    }

    private val mOnPagerChangeListener: ViewPager.OnPageChangeListener =
        object : ViewPager.OnPageChangeListener {
            override fun onPageSelected(position: Int) {
                pageIndicatorView.selection = position
                count = position
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageScrollStateChanged(arg0: Int) {}
        }
}