package com.various.funny.sounds.audio.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.various.funny.sounds.audio.databinding.ActivityHomeBinding
import com.various.funny.sounds.audio.ui.adapter.ViewPagerAdapter
import com.various.funny.sounds.audio.ui.fragment.CollectFragment
import com.various.funny.sounds.audio.ui.fragment.HomeFragment

class HomeActivity : AppCompatActivity(), OnPageChangeListener {

    private lateinit var homeBinding: ActivityHomeBinding

    private var data_fragment = mutableListOf<Fragment>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initStatusBar()
        homeBinding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(homeBinding.root)
        data_fragment.add(HomeFragment.newInstance())
        data_fragment.add(CollectFragment.newInstance())
        onInitView()
    }

    private fun onInitView() {
        homeBinding.viewPager.run {
            adapter = ViewPagerAdapter(data_fragment, supportFragmentManager)
            addOnPageChangeListener(this@HomeActivity)
        }

        onInitTab()
        setTabSelect(0)
    }
    private fun initStatusBar(){
        val flags = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        window.decorView.systemUiVisibility = flags xor View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }
    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {
        setTabSelect(position)
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    private fun onInitTab() {
        homeBinding.tabHome.setOnClickListener {
            setTabSelect(0)
        }
        homeBinding.tabCollect.setOnClickListener {
            setTabSelect(1)
        }
    }

    private fun setTabSelect(position: Int) {
        if (position == 0) {
            homeBinding.tabHomeIm.isSelected = true
            homeBinding.tabCollectIm.isSelected = false
            homeBinding.viewPager.currentItem = 0
        } else {
            homeBinding.tabCollectIm.isSelected = true
            homeBinding.tabHomeIm.isSelected = false
            homeBinding.viewPager.currentItem = 1
        }
    }
}