package com.base.appzxhy.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.base.appzxhy.base.BaseActivity
import com.base.appzxhy.bean.config.AdConfigBean
import com.base.appzxhy.databinding.ActivityGuideBinding
import com.base.appzxhy.ui.main.MainActivity

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

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

    private val page3: GuideFragment by lazy {
        GuideFragment().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()
        }
    }

    override fun handleBackCallBack() {}
}