package com.keypalette.theme

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.keypalette.theme.activity.PageSkinsAll
import com.keypalette.theme.dataabout.resource.WrapperData
import com.keypalette.theme.databinding.FragmentMainBinding
import com.keypalette.theme.other.listener.ListenerClickViewAll

class FragmentMain : Fragment() {


    private lateinit var vb: FragmentMainBinding

    private var dialogEnableKeyboard: DialogEnableKeyboard? = null

    lateinit var viewAllList: List<WrapperData>
    private lateinit var mainAdapter: MainAdapter

    private var recommendDialog: DialogRecommendation? = null

    companion object {

        @JvmStatic
        fun newInstance() =
            FragmentMain()
    }

    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 = KeyApp.Companion.list.subList(1, KeyApp.Companion.list.size)
        mainAdapter = MainAdapter(
            requireContext(), viewAllList
        ).apply {
            setClickAction(object : ListenerClickViewAll {
                override fun OnClickSeeAll(name: String) {
                    startActivity(Intent(
                        requireContext(),
                        PageSkinsAll::class.java
                    ).apply {
                        putExtra(PageSkinsAll.KEY_NAME, name)
                    })

                }

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

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

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

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