package com.audio.record.screen.test.fragment

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.audio.record.screen.test.R
import com.audio.record.screen.test.adapter.ViewPager2Adapter
import com.audio.record.screen.test.adapter.VpFragmentAdapter
import com.audio.record.screen.test.base.BaseFragment
import com.audio.record.screen.test.databinding.FragmentMainBinding
import com.audio.record.screen.test.tool.Common
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class MainFragment : BaseFragment<FragmentMainBinding>() {

    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
//            param1 = it.getString(ARG_PARAM1)
//            param2 = it.getString(ARG_PARAM2)
        }
    }

    companion object {

        @JvmStatic
        fun newInstance() =
            MainFragment().apply {
                arguments = Bundle().apply {
//                    putString(ARG_PARAM1, param1)
//                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun initBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentMainBinding =
        FragmentMainBinding.inflate(inflater, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        bingVp()


    }


    private fun bingVp() {
        val tabString =
            arrayOf(R.string.Video, R.string.Image)
        binding.run {
            viewPager2.isUserInputEnabled = false
            viewPager2.adapter = VpFragmentAdapter(this@MainFragment)
            TabLayoutMediator(tabLayout, viewPager2) { tab, position ->
                val customView = LayoutInflater.from(this@MainFragment.context)
                    .inflate(R.layout.tab_child_item, null)
                val tabTv = customView.findViewById<TextView>(R.id.tab_tv)
                tabTv.text = getString(tabString[position])
                if(position == 0){
                    tabTv.isSelected = true
                    tabTv.background = Common.getIcon(R.drawable.tab_child_selected)
                }

                tab.customView = customView
            }.attach()
            tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab?.customView?.run {
                        findViewById<TextView>(R.id.tab_tv).run {
                            isSelected = true
                            background = Common.getIcon(R.drawable.tab_child_selected)
                        }
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.customView?.run {
                        findViewById<TextView>(R.id.tab_tv).run {
                            isSelected = false
                            background = null
                        }
                    }
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })

        }

    }
}