package com.sunny.app.soft.timberkeyboardnew.ui.fragment

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.sunny.app.soft.timberkeyboardnew.App
import com.sunny.app.soft.timberkeyboardnew.R
import com.sunny.app.soft.timberkeyboardnew.data.DataManager
import com.sunny.app.soft.timberkeyboardnew.data.entity.CategoryEntity
import com.sunny.app.soft.timberkeyboardnew.databinding.FragmentHomeBinding
import com.sunny.app.soft.timberkeyboardnew.tools.AppConstant
import com.sunny.app.soft.timberkeyboardnew.ui.activity.SelectActivity
import com.sunny.app.soft.timberkeyboardnew.ui.adapter.HomeViewAdapter
import com.sunny.app.soft.timberkeyboardnew.ui.listener.OnItemClickListener

class HomeFragment :
    BaseFragment() {

    private lateinit var binding: FragmentHomeBinding

    private lateinit var data: CategoryEntity
    override fun setRootView(): View {
        binding = FragmentHomeBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        super.initView()
        // TODO:  loadAd
        initTitle()
        initViewPager()
    }

    private fun initViewPager() {

        val categoryEntityList = DataManager().getModelList()

        binding.homeRecyclerView.apply {
            layoutManager = LinearLayoutManager(requireContext()).apply {
                orientation = LinearLayoutManager.VERTICAL
            }
            adapter =
                HomeViewAdapter(requireContext(), categoryEntityList, object : OnItemClickListener {
                    override fun onItemClick(position: Int, categoryEntity: CategoryEntity) {
                        data = categoryEntity
                        onShowAd()


                    }
                })
        }

    }

    private fun enterList() {
        val intent = Intent(requireContext(), SelectActivity::class.java)
        intent.putExtra(AppConstant.KEY_EXTRA, data)
        startActivity(intent)
    }


    fun onShowAd() {

//        val readyUnitAd = UnityAdManager.getReadyUnitAd()
//        if (readyUnitAd == null) {
//            enterList()
//        } else {
//            UnityAdManager.showUnityAd(requireActivity(), readyUnitAd, object : onShowCallBack {
//                override fun onShowFail() {
//                    enterList()
//                }
//
//                override fun onShowClose() {
//                    enterList()
//                }
//
//                override fun onShowSuccess() {
//
//                }
//
//            })
//        }


    }

    private fun initTitle() {
        binding.homeTitle.text = App.appContext.getString(R.string.app_name)
    }
}