package com.themes.keyboard.black.fragment

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.themes.keyboard.black.MyApp
import com.themes.keyboard.black.recycler.AdapterMain
import com.themes.keyboard.black.databinding.FragmentMainBinding
import com.themes.keyboard.black.listener.OnSeeAllListener
import com.themes.keyboard.black.localdata.ClassData
import com.themes.keyboard.black.activity.ViewAllActivity

class MainFragment : Fragment() {


    private lateinit var vb: FragmentMainBinding

    private var dialogEnable: DialogEnable? = null

    lateinit var viewAllList: MutableList<ClassData>
    private lateinit var adapterMain: AdapterMain

    private var recommendDialog: DialogRecommend? = null

    companion object {

        @JvmStatic
        fun newInstance() =
            MainFragment()
    }

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


    private fun setTabRecycler() {
        viewAllList = MyApp.list.subList(1, MyApp.list.size)
        adapterMain = AdapterMain(
            requireContext(), viewAllList
        ).apply {
            setClickAction(object : OnSeeAllListener {
                override fun OnClickSeeAll(name: String) {
                    startActivity(Intent(
                        requireContext(),
                        ViewAllActivity::class.java
                    ).apply {
                        putExtra(ViewAllActivity.KEY_NAME, name)
                    })

                }

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

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

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

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