package com.swago.home.innerhome

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.fragment.BaseListFragment
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.model.BlackListModel
import com.swago.baseswago.model.MomentModel
import com.swago.baseswago.model.home.BannerModel
import com.swago.baseswago.model.home.HomeModel
import com.swago.home.databinding.FragmentHotBinding
import com.swago.home.databinding.LayoutViewHeaderBinding
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.indicator.CircleIndicator

/**
 *@date 2021/11/23 21:43
 *description:
 */
class HotFragment : BaseListFragment<FragmentHotBinding, MomentModel>() {

    private val homeVm by viewModels<HomeVm>()

    private var isFirstLoadData = true

    override val smartRecyclerView: SwagoRecyclerView<MomentModel> by lazy {
        binding.rv as SwagoRecyclerView<MomentModel>
    }
    override val adapter: BaseQuickAdapter<MomentModel, *> by lazy {
        HomeAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        GridLayoutManager(context, 2)
    }
    override val loadData: (page: Int) -> Unit = {
        homeVm.getHotList(1, it + 1)
    }

    private val bannerList by lazy {
        ArrayList<BannerModel>()
    }

    private val bannerAdapter by lazy {
        ImageAdapter(bannerList)
    }


    private var headerView: LayoutViewHeaderBinding? = null


    override fun initViewData() {
        headerView =
            LayoutViewHeaderBinding.inflate(layoutInflater, smartRecyclerView.recyclerView, false)
        adapter.addHeaderView(headerView?.root)

        headerView?.banner?.addBannerLifecycleObserver(this)
            ?.setAdapter(bannerAdapter)
            ?.setIndicator(CircleIndicator(context))
            ?.setOnBannerListener { data, _ ->

            }

        smartRecyclerView.recyclerView?.addItemDecoration(HomeGridItemDecoration())
        adapter.setOnItemClickListener { _, _, position ->
            ARouter.getInstance()
                .build(ARouteConstant.Room.user)
                .withInt("position", position)
                .withParcelableArrayList("data", adapter.data as ArrayList<MomentModel>)
                .navigation()
        }
    }

    override fun initLiveData() {
        homeVm.hotListLiveData.observe(this) {
            if (it == null) {
                smartRecyclerView.onFetchDataError()
            } else {
                smartRecyclerView.onFetchDataFinish(it, true,true)
            }
        }

        homeVm.bannerListLiveData.observe(this) {
            if (it != null) {
                if (it.isEmpty()){
                    headerView?.banner?.visibility = View.GONE
                }else{
                    bannerList.clear()
                    bannerList.addAll(it)
                    headerView?.banner?.visibility = View.VISIBLE
                    bannerAdapter.notifyDataSetChanged()
                }

            }else{
                headerView?.banner?.visibility = View.GONE
            }
        }
    }

    fun loadData(){
        if (!isFirstLoadData){
            smartRecyclerView.startRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        if (!isFirstLoadData){
            smartRecyclerView.startRefresh()
        }
        isFirstLoadData = false
    }

    override fun loadMoreNeed(): Boolean {
        return false
    }
}