package com.swiftcleaner.chovey.view.activity

import android.content.Intent
import android.graphics.Color
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.swiftcleaner.chovey.business.AdIntervalTimeUtils
import com.swiftcleaner.chovey.business.newAdmob.AdmobHelps
import com.swiftcleaner.chovey.databinding.ActivityGuideBinding
import com.swiftcleaner.chovey.view.BaseActivity
import com.swiftcleaner.chovey.view.function.constellation.BarUtils

class GuideActivity : BaseActivity<ActivityGuideBinding>() {

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

    private var currentIndex = 0

    override fun getViewBinding(): ActivityGuideBinding {
        return ActivityGuideBinding.inflate(layoutInflater)
    }

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        val fragments = if (AdmobHelps.isShowGuideAd() && AdIntervalTimeUtils.getNativeIntervalTime() == 0) {
            mutableListOf(page1, pageNative, page2, pageNative2, page3)
        } else mutableListOf(page1, page2, page3)
        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 initData() {

    }

    fun next() {
        val nextPage = binding.viewpager2.currentItem + 1
        if (nextPage < (binding.viewpager2.adapter?.itemCount ?: 0)){
            binding.viewpager2.currentItem = nextPage
        } else {
            startActivity(Intent(this, CollectActivity::class.java))
            finish()
        }
    }
}