package com.themes.fancy.keyboard.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.themes.fancy.keyboard.keyboard.MyApplication
import com.themes.fancy.keyboard.keyboard.R
import com.themes.fancy.keyboard.keyboard.data.Class
import com.themes.fancy.keyboard.keyboard.myada.TabAdapter
import com.themes.fancy.keyboard.keyboard.helper.Common

class ViewActivity : AppCompatActivity() {
    private lateinit var tabRecycler: RecyclerView
    private lateinit var viewContent: ViewPager
    lateinit var  viewAllList:MutableList<Class>
    private lateinit var tabAdapter: TabAdapter

    private lateinit var imgBack: ImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_view_all)
        Common.initFullScreen(this)
        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 = MyApplication.list.filter {
            it.parentName == "for you"
        }
        val otherList =  MyApplication.list.filter {
            it.parentName != "for you"
        }
        val shuffled = otherList.shuffled()

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

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

    private fun setContent(){
        val fragList = mutableListOf<Fragment>()
        for(index in 0 until viewAllList.size){
            fragList.add(FragmentMain.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) {

            }

        })
    }
}