package com.easy.clean.ui.guide

import NativeFullFragment
import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.easy.clean.base.BaseActivity
import com.easy.clean.bean.config.AdConfigBean
import com.easy.clean.databinding.ActivityGuideBinding
import com.easy.clean.ui.main.MainActivity

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

    private val page1: Guide2Fragment by lazy {
        Guide2Fragment().apply { page = 0 }
    }
    private val page2: Guide2Fragment by lazy {
        Guide2Fragment().apply { page = 1 }
    }
    private val pageNative: NativeFullFragment by lazy {
        NativeFullFragment()
    }

    private val page3: Guide2Fragment by lazy {
        Guide2Fragment().apply { page = 3 }
    }
    private val fragments: MutableList<Fragment> by lazy {
        mutableListOf(page1, page2, page3)
    }

    private var currentIndex = 0

    override fun initView() {
        super.initView()
        if (AdConfigBean.adsConfigBean.fullNativeShow && AdConfigBean.adsConfigBean.isAdShow) {
            fragments.add(2, pageNative)
        }

        binding.viewpager2.run {
            offscreenPageLimit = 4
            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
            }
        })
    }


    fun next(page: Int) {

        if (page == 0) {
            binding.viewpager2.currentItem = 1
            return
        }

        if (page == 1) {
            binding.viewpager2.currentItem = 2
            return
        }
        if (page == 3) {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }
}