package com.keyboard.keyboardtheme.skin.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.keyboard.keyboardtheme.skin.MyApp
import com.keyboard.keyboardtheme.skin.act.ActViewAll
import com.keyboard.keyboardtheme.skin.adapter.AdaParent
import com.keyboard.keyboardtheme.skin.databinding.FragmentHomeBinding
import com.keyboard.keyboardtheme.skin.datamanager.localdata.ParentData
import com.keyboard.keyboardtheme.skin.utils.OnViewAll

class FragHome : Fragment() {


    private lateinit var vb: FragmentHomeBinding

    private var dialogActive: DialogActive? = null

    lateinit var viewAllList: List<ParentData>
    private lateinit var adaParent: AdaParent

    private var recommendDialog: DialogMore? = null

    companion object {

        @JvmStatic
        fun newInstance() =
            FragHome()
    }

    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 = MyApp.list.subList(1, MyApp.list.size)
        adaParent = AdaParent(
            requireContext(), viewAllList
        ).apply {
            setClickAction(object : OnViewAll {
                override fun OnClickSeeAll(name: String) {
                    startActivity(Intent(
                        requireContext(),
                        ActViewAll::class.java
                    ).apply {
                        putExtra(ActViewAll.KEY_NAME, name)
                    })

                }

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

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

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

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