package com.app.keyboard.colorful.afragment

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.app.keyboard.colorful.MyApp
import com.app.keyboard.colorful.activity.ActivityAll
import com.app.keyboard.colorful.ada.AdaMain
import com.app.keyboard.colorful.colorlistener.COnSeeAllListener
import com.app.keyboard.colorful.data.ClassBean
import com.app.keyboard.colorful.databinding.FragMainBinding

class FragmentMain : Fragment() {


    private lateinit var vb: FragMainBinding

    private var fragmentEnable: FragmentEnable? = null

    lateinit var viewAllList: MutableList<ClassBean>
    private lateinit var adaMain: AdaMain

    private var recommendDialog: FragmentRecommend? = null

    companion object {

        @JvmStatic
        fun newInstance() =
            FragmentMain()
    }

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


    private fun setTabRecycler() {
        viewAllList = MyApp.list.subList(1, MyApp.list.size)
        adaMain = AdaMain(
            requireContext(), viewAllList
        ).apply {
            setClickAction(object : COnSeeAllListener {
                override fun OnClickSeeAll(name: String) {
                    startActivity(Intent(
                        requireContext(),
                        ActivityAll::class.java
                    ).apply {
                        putExtra(ActivityAll.KEY_NAME, name)
                    })

                }

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

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

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

    private fun showEnableDialog() {
        if (fragmentEnable == null) {
            fragmentEnable = FragmentEnable {
                showRecommend()
            }
        }
        fragmentEnable?.show(childFragmentManager, "")
    }
}