package com.wall.photography.wallpaper.activivty

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.wall.photography.wallpaper.MyApp
import com.wall.photography.wallpaper.R
import com.wall.photography.wallpaper.databinding.ActivityMainBinding
import com.wall.photography.wallpaper.fragment.CategoryFragment
import com.wall.photography.wallpaper.fragment.HomeFragment
import com.wall.photography.wallpaper.fragment.LoveFragment
import com.wall.photography.wallpaper.manager.DealData
import com.wall.photography.wallpaper.topon.AdManager

class MainActivity : AppCompatActivity() {
    private lateinit var vb: ActivityMainBinding
    private lateinit var listOf: List<Fragment>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(MyApp.TAG, "------------MainActivity onCreate=$this")
        vb = ActivityMainBinding.inflate(layoutInflater)
        setContentView(vb.root)
        listOf = listOf<Fragment>(
            HomeFragment.newInstance(),
            CategoryFragment.newInstance(),
            LoveFragment.newInstance()
        )
        initVp2()

        vb.iconSet.setOnClickListener {
            AdManager.showTopOn(this@MainActivity) {
                startActivity(Intent(this@MainActivity, AboutActivity::class.java).apply {

                })
            }

        }
        vb.bottomNavigation.setOnItemSelectedListener { menu ->
            when (menu.itemId) {
                R.id.home -> vb.viewpager2.currentItem = 0
                R.id.category -> {
                    vb.viewpager2.currentItem = 1
                    val cateFragment = listOf[1] as CategoryFragment
                    cateFragment.refreshCate()
                }

                R.id.love -> {
                    vb.viewpager2.currentItem = 2
                    val loveFragment = listOf[2] as LoveFragment
                    loveFragment.refreshLoves()
                }
            }
            true
        }

    }

    private fun initVp2() {

        vb.viewpager2.run {
            adapter = object : FragmentStateAdapter(this@MainActivity) {
                override fun getItemCount(): Int = listOf.size

                override fun createFragment(position: Int): Fragment = listOf[position]

            }
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> vb.bottomNavigation.selectedItemId = R.id.home
                        1 -> {
                            vb.bottomNavigation.selectedItemId = R.id.category
                            val cateFragment = listOf[1] as CategoryFragment
                            cateFragment.refreshCate()
                        }

                        2 -> {
                            vb.bottomNavigation.selectedItemId = R.id.love
                            val loveFragment = listOf[2] as LoveFragment
                            loveFragment.refreshLoves()
                        }

                    }
                }
            })

        }
    }
}