package com.key.board.journey.inputmethod.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.key.board.journey.inputmethod.MyKeyboard
import com.key.board.journey.inputmethod.activity.ListActivity
import com.key.board.journey.inputmethod.myInter.InterfaceList
import com.key.board.journey.inputmethod.data.ClassBean
import com.key.board.journey.inputmethod.databinding.FragmentHomeBinding
import com.key.board.journey.inputmethod.list.ItemMain

class FragmentHome : Fragment() {


    private lateinit var vb: FragmentHomeBinding

    private var dialogEnable: DialogEnable? = null

    lateinit var viewAllList: MutableList<ClassBean>
    private lateinit var itemMain: ItemMain

    private var recommendDialog: DialogOpen? = null

    companion object {

        @JvmStatic
        fun newInstance() =
            FragmentHome()
    }

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


    private fun setTabRecycler() {
        viewAllList = MyKeyboard.list.subList(1, MyKeyboard.list.size)
        itemMain = ItemMain(
            requireContext(), viewAllList
        ).apply {
            setClickAction(object : InterfaceList {
                override fun OnClickSeeAll(name: String) {
                    startActivity(Intent(
                        requireContext(),
                        ListActivity::class.java
                    ).apply {
                        putExtra(ListActivity.KEY_NAME, name)
                    })

                }

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

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

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

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