package com.shiny.keyboard.frag

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.shiny.keyboard.MyApplication
import com.shiny.keyboard.databinding.FragMainBinding
import com.shiny.keyboard.mylistener.ClickSeeAllListener
import com.shiny.keyboard.recycler.AdapterHome
import com.shiny.keyboard.ui.SeeAllActivity

class FragmentHome : Fragment() {


    private lateinit var vb: FragMainBinding



    lateinit var viewAllList: MutableList<com.shiny.keyboard.bean.ClassData>
    private lateinit var adapterHome: AdapterHome

    private var recommendDialog: DialogRecommend? = null

    companion object {

        @JvmStatic
        fun newInstance() =
            FragmentHome()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        vb = FragMainBinding.inflate(layoutInflater)
//        viewContent = vb.viewPager
        setTabRecycler()
        showRecommend()
        return vb.root
    }


    private fun setTabRecycler() {
        viewAllList = MyApplication.list.subList(1, MyApplication.list.size)
        adapterHome = AdapterHome(
            requireContext(), viewAllList
        ).apply {
            setClickAction(object : ClickSeeAllListener {
                override fun OnClickSeeAll(name: String) {
                    startActivity(Intent(requireContext(),
                        SeeAllActivity::class.java).apply {
                        putExtra(SeeAllActivity.KEY_NAME,name)
                    })

                }

            })
        }
        vb.tabRecycler.run {
            adapter = adapterHome
            layoutManager = LinearLayoutManager(requireContext())
        }
    }

    private fun showRecommend() {
        if (recommendDialog == null) {
            recommendDialog = DialogRecommend.newInstance() {

            }
        }
        recommendDialog?.show(childFragmentManager, "")
    }


}