package com.quansu.heifengwuliu.activity

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.qmuiteam.qmui.widget.QMUIPagerAdapter
import com.qmuiteam.qmui.widget.QMUIViewPager
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.base.MBActivity
import com.quansu.heifengwuliu.config.ConfigMMKV
import com.quansu.heifengwuliu.databinding.MainGuidBinding
import com.quansu.heifengwuliu.widget.WidgetGuide
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.MMKVManager

class SplashActivity : MBActivity<BViewModel<BRepository>, MainGuidBinding>() {

    private val mItems: Array<Int> = arrayOf(R.drawable.img_bg, R.drawable.img_bg, R.drawable.img_bg, R.drawable.img_bg_n)

    override fun binding(): MainGuidBinding {

        return MainGuidBinding.inflate(layoutInflater)
    }


    override fun initCreate(savedInstanceState: Bundle?) {
        super.initCreate(savedInstanceState)
        initPagers()
    }


    private fun initPagers() {

        MMKVManager.instance().encode(ConfigMMKV.FIRST_OPEN, true)

        val pagerAdapter: QMUIPagerAdapter = object : QMUIPagerAdapter() {
            override fun isViewFromObject(view: View, `object`: Any): Boolean {
                return view === `object`
            }

            override fun getCount(): Int {
                return mItems.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return null
            }

            override fun hydrate(container: ViewGroup, position: Int): Any {
                return WidgetGuide(context())
            }


            override fun populate(container: ViewGroup, item: Any, position: Int) {
                val itemView = item as WidgetGuide
                itemView.setImageResource(mItems[position])
                container.addView(itemView)
            }

            override fun destroy(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(`object` as View)
            }
        }

        val mViewPager: QMUIViewPager = binding.root as QMUIViewPager

        //setPageTransformer默认采用ViewCompat.LAYER_TYPE_HARDWARE， 但它在某些4.x的国产机下会crash
//        mViewPager.setPageTransformer(false, CardTransformer(), View.LAYER_TYPE_HARDWARE)
        mViewPager.adapter = pagerAdapter
    }

    override fun transluent(): Boolean {
        return true
    }
}