package com.base.browserwhite.ui.activity.download

import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.browserwhite.databinding.ActivityWebDownloadGuideBinding
import com.base.browserwhite.ui.activity.BaseActivity
import com.base.browserwhite.utils.BarUtils

class WebDownloadGuideActivity : BaseActivity<ActivityWebDownloadGuideBinding>() {

    private val fragment1: DownloadGuideFragment by lazy {
        DownloadGuideFragment(0)
    }
    private val fragment2: DownloadGuideFragment by lazy {
        DownloadGuideFragment(1)
    }
    private val fragment3: DownloadGuideFragment by lazy {
        DownloadGuideFragment(2)
    }
    private val fragment4: DownloadGuideFragment by lazy {
        DownloadGuideFragment(3)
    }
    private var currentPosition: Int = 0

    private val fragments by lazy {
        mutableListOf(fragment1, fragment2, fragment3, fragment4)
    }


    override val binding: ActivityWebDownloadGuideBinding by lazy {
        ActivityWebDownloadGuideBinding.inflate(layoutInflater)
    }


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        binding.viewPager2.run {
            adapter = object : FragmentStateAdapter(this@WebDownloadGuideActivity) {
                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) {
                currentPosition = position
                binding.ivBefore.isVisible = currentPosition > 0
                binding.ivNext.isVisible = currentPosition < 3
            }
        })
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback { finishToMain() }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
    }

}