package com.rich.keyboard.skin.uifragment

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.rich.keyboard.skin.MyApp
import com.rich.keyboard.skin.callback.OnSeeAllCallback
import com.rich.keyboard.skin.databinding.FragMainBinding
import com.rich.keyboard.skin.listadapter.MainList
import com.rich.keyboard.skin.localdata.ParentData
import com.rich.keyboard.skin.uiactivity.ViewAllUi

class MainFragmentUi : Fragment() {


    private lateinit var vb: FragMainBinding

    private var diaEnable: DiaEnable? = null

    lateinit var viewAllList: List<ParentData>
    private lateinit var mainList: MainList

    private var recommendDialog: DiaRecommend? = null

    companion object {

        @JvmStatic
        fun newInstance() =
            MainFragmentUi()
    }

    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)
        mainList = MainList(
            requireContext(), viewAllList
        ).apply {
            setClickAction(object : OnSeeAllCallback {
                override fun OnClickSeeAll(name: String) {
                    startActivity(Intent(
                        requireContext(),
                        ViewAllUi::class.java
                    ).apply {
                        putExtra(ViewAllUi.KEY_NAME, name)
                    })

                }

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

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

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

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