package com.input.board.appskey.keyboard.viewUi

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.input.board.appskey.keyboard.KeyBoardApp
import com.input.board.appskey.keyboard.R
import com.input.board.appskey.keyboard.jsonBean.MainBean
import com.input.board.appskey.keyboard.recyAdapter.ViewAllTabAdapter

class ViewAllActivity : AppCompatActivity() {
    private lateinit var tabRecycler: RecyclerView
    private lateinit var viewContent: ViewPager
    lateinit var  viewAllList:MutableList<MainBean>
    private lateinit var tabAdapter:ViewAllTabAdapter

    private lateinit var imgBack: ImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_all)
        findViewId()
        setTabRecycler()
        setContent()
    }

    private fun findViewId(){
        tabRecycler = findViewById(R.id.tab_recycler)
        viewContent = findViewById(R.id.view_pager_content)
        imgBack = findViewById(R.id.back)
        imgBack.setOnClickListener {
            finish()
        }
    }

    private fun setTabRecycler(){
        val first = KeyBoardApp.list.filter {
            it.parentName == "for you"
        }
        val otherList =  KeyBoardApp.list.filter {
            it.parentName != "for you"
        }
        val shuffled = otherList.shuffled()

        viewAllList = mutableListOf<MainBean>().apply {
            add(first[0])
            addAll(shuffled)
        }
         tabAdapter = ViewAllTabAdapter(this@ViewAllActivity).apply {
            tabTitleName(viewAllList)
             setChangeListener (object : ViewAllTabAdapter.OnChangListener{
                 override fun onChange(posit: Int) {
                     viewContent.setCurrentItem(posit,false)
                 }

             })
        }
        tabRecycler.run {
            adapter = tabAdapter
            layoutManager = LinearLayoutManager(this@ViewAllActivity,RecyclerView.HORIZONTAL,false)
        }
    }

    private fun setContent(){
        val fragList = mutableListOf<Fragment>()
        for(index in 0 until viewAllList.size){
            fragList.add(FragmentContent.newInstance(index))
        }

        viewContent.adapter = object :FragmentStatePagerAdapter(supportFragmentManager,
            BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
            override fun getCount(): Int = fragList.size

            override fun getItem(position: Int): Fragment = fragList[position]

        }
        var curViewPager  = 0
        viewContent.addOnPageChangeListener(object :OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                if(curViewPager != position){
                    tabAdapter.setSelectedPos(position)
                    tabRecycler.scrollToPosition(position)
                    curViewPager = position
                }

            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }
}