package com.zxhyis.toolss.ui.start

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.forEach
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.BarUtils
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.databinding.ActivityStartPageBinding
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.ui.home.MainActivity

class StartPageActivity : AppCompatActivity() {

    private lateinit var binding: ActivityStartPageBinding
    private val pages = arrayListOf<Fragment>()
    private var current = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityStartPageBinding.inflate(layoutInflater)
        setContentView(binding.root)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }


        pages.add(StartPageFragment(R.drawable.start_banner1))
//        pages.add(StartPageFragment(R.drawable.start_banner2))
//        pages.add(StartPageFragment(R.drawable.start_banner3))
        pages.add(StartPageFragment(R.drawable.start_banner4))

        val pagerAdapter = ScreenSlidePagerAdapter(this)
        binding.viewpage2.adapter = pagerAdapter

        switchIndicator(1)
        binding.viewpage2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                current = position
                val fragment = pages[position] as StartPageFragment
                switchIndicator(position + 1)
                changePage(false)
            }
        })
        binding.idTvSkip.setOnClick(javaClass.simpleName) {
            AdmobUtils.showInterstitialAd(this) {
                startActivity(Intent(this, MainActivity::class.java))
                finish()
            }
        }
        binding.tvNext.setOnClick(javaClass.simpleName) {
            if (binding.tvNext.text == "START") {
                AdmobUtils.showInterstitialAd(this) {
                    startActivity(Intent(this, MainActivity::class.java))
                    finish()
                }
//                MaxAdUtils.showMaxInterstitialAd(this) {
//
//                }
            } else {
                changePage(true)
            }

        }
    }


    private fun switchIndicator(position: Int) {
        val selectId = when (position) {
            1 -> R.id.iv_1
            2 -> R.id.iv_4
//            3 -> R.id.iv_3
//            4 -> R.id.iv_4
            else -> R.id.iv_1
        }

        binding.llIndicator.forEach {
            val imageView = it as ImageView
            if (imageView.id == selectId) {
                imageView.setImageResource(R.drawable.bg_banner_indicator_enable)
            } else {
                imageView.setImageResource(R.drawable.bg_banner_indicator_disable)
            }
        }
    }

    fun changePage(isNext: Boolean) {
        if (isNext) {
            current += 1
            binding.viewpage2.setCurrentItem(current, true)
        }
        binding.tvNext.text = if (current == 1) "START" else "NEXT"
    }

    inner class ScreenSlidePagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
        override fun getItemCount(): Int = pages.size
        override fun createFragment(position: Int): Fragment = pages[position]
    }
}