package com.duben.microtribe.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.duben.microtribe.R
import com.duben.microtribe.common.AppConfig
import com.duben.microtribe.common.Constant
import com.duben.microtribe.manager.LocalVedioManager
import com.duben.microtribe.mvp.model.BannerList
import com.duben.microtribe.mvp.model.VedioBean
import com.duben.microtribe.mvp.presenters.VipEnjoyPresenter
import com.duben.microtribe.mvp.views.VipEnjoyView
import com.duben.microtribe.ui.adapter.VipEnjoyAdapter
import com.duben.microtribe.ui.fragment.base.LazyLoadBaseFragment
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.fragment_vip_enjoy.*

/**
 *  @author Assen
 *  @date 2023/12/5
 *  @desc VIP尊享页面
 */
class VipEnjoyFragment : LazyLoadBaseFragment(), OnLoadMoreListener, VipEnjoyView,
    OnRefreshListener {

    companion object {
        fun newInstance(): Fragment {
            val args = Bundle()
            val fragment = VipEnjoyFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private val vipEnjoyPresenter by lazy { VipEnjoyPresenter() }

    private lateinit var vipEnjoyAdapter: VipEnjoyAdapter
    private var page = 1 // 分页
    private var pageSize = Constant.PAGE_SIZE // 分页

    override fun initViewsAndEvents() {
        vipEnjoyPresenter.attachView(this)

        initView()
        initListener()
    }

    override fun onResume() {
        super.onResume()
        myRefresh()
    }

    override fun onDetach() {
        super.onDetach()
        vipEnjoyPresenter.detachView()
    }

    override fun getContentViewLayoutID() = R.layout.fragment_vip_enjoy

    private fun initView() {
        rv_enjoy.layoutManager = GridLayoutManager(context, 2)
        vipEnjoyAdapter = VipEnjoyAdapter()
        vipEnjoyAdapter.animationEnable = false
        rv_enjoy.adapter = vipEnjoyAdapter
        val emptyView =
            LayoutInflater.from(requireContext()).inflate(R.layout.item_empty_data, null)
        vipEnjoyAdapter.setEmptyView(emptyView)
        vipEnjoyAdapter.setOnCustomChildClickListener(object :
            VipEnjoyAdapter.OnCustomChildClickListener {
            override fun onCustomChildClick(view: View, vedioBean: VedioBean) {
                LocalVedioManager.startVedioDetailActivityForType(
                    requireActivity(),
                    vedioBean
                )
            }
        })
    }

    private fun initListener() {
        srl_enjoy.setEnableRefresh(true)
        srl_enjoy.setOnLoadMoreListener(this)
        srl_enjoy.setOnRefreshListener(this)
    }

    private fun myRefresh() {
        if (AppConfig.fragmentClickFlag == Constant.FRAGMENT_CLICK_THREE) {
            page = 1
            srl_enjoy.resetNoMoreData()
            vipEnjoyPresenter.getVedio4List(page, pageSize)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        myRefresh()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        page = ++page
        vipEnjoyPresenter.getVedio4List(page, pageSize)
    }

    override fun getVedio4ListSuc(data: BannerList) {
        if (activity == null || requireActivity().isFinishing) {
            srl_enjoy?.finishRefresh(true)
            return
        }

        if (::vipEnjoyAdapter.isInitialized) {
            if (page == 1) {
                srl_enjoy.finishRefresh(true)
                vipEnjoyAdapter.setNewInstance(data.list)
            } else {
                if (data.list.size < pageSize) {
                    srl_enjoy.finishLoadMoreWithNoMoreData()
                } else {
                    srl_enjoy.finishLoadMore()
                }
                vipEnjoyAdapter.addData(data.list)
            }
        }
    }

    override fun getVedio4ListFail() {

    }

}