package com.base.scanqrclear.ui.guide

import android.graphics.Color
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.scanqrclear.R
import com.base.scanqrclear.base.BaseActivity
import com.base.scanqrclear.databinding.ActivityGuideBinding
import com.gyf.immersionbar.ktx.immersionBar
import com.zhpan.indicator.enums.IndicatorSlideMode.Companion.SMOOTH
import com.zhpan.indicator.enums.IndicatorStyle.Companion.ROUND_RECT

class GuideActivity : BaseActivity<ActivityGuideBinding>(ActivityGuideBinding::inflate) {

    private val guide1 by lazy {
        GuideFragment(0)
    }
    private val guide2 by lazy {
        GuideFragment(1)
    }
    private val guide3 by lazy {
        GuideFragment(2)
    }
    private val fragments by lazy {
        mutableListOf(guide1, guide2, guide3)
    }
    private var currentIndex = 0

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        val flag = changeLanguage()
        if (flag) return
    }

    override fun initView() {
        super.initView()
        initViewPager2()
        initIndicator()
    }

    private fun initViewPager2() {
        binding.viewPager2.run {
            adapter = object : FragmentStateAdapter(this@GuideActivity) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }

        binding.viewPager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                currentIndex = position
            }
        })
    }

    override fun initListener() {
        super.initListener()

    }

    override fun configSystemBar() {
        immersionBar {
            statusBarColor("#00FFFFFF")
            statusBarDarkFont(true)
        }
    }

    fun nextPage() {
        if (currentIndex <= 1) {
            currentIndex++
            binding.viewPager2.setCurrentItem(currentIndex, true)
        }
    }

    private fun initIndicator() {
        binding.indicator.setSliderColor(
            Color.parseColor("#DEDEDE"),
            Color.parseColor("#6473F8")
        )
        binding.indicator.setSliderHeight(resources.getDimension(R.dimen.dp_5))
        binding.indicator.setSliderWidth(
            resources.getDimension(R.dimen.dp_5),
            resources.getDimension(R.dimen.dp_15)
        )
        binding.indicator.setSlideMode(SMOOTH)
        binding.indicator.setIndicatorStyle(ROUND_RECT)
        binding.indicator.setupWithViewPager(binding.viewPager2)
    }

}