package com.duben.happyplaylet.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.mints.wisdomclean.ui.adapter.HotStyleAdapter
import com.duben.happyplaylet.R
import com.duben.happyplaylet.common.Constant
import com.duben.happyplaylet.manager.LocalVedioManager
import com.duben.happyplaylet.mvp.model.*
import com.duben.happyplaylet.mvp.presenters.PicContentPresenter
import com.duben.happyplaylet.mvp.views.PicContentView
import com.duben.happyplaylet.ui.adapter.NewHotStyleAdapter
import com.duben.happyplaylet.ui.fragment.base.LazyLoadBaseFragment
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.duben.happyplaylet.common.AppConfig
import com.duben.happyplaylet.ui.widgets.MyGridLayoutManager
import kotlinx.android.synthetic.main.fragment_movie.*
import java.util.*

class MovieFragment : LazyLoadBaseFragment(), PicContentView, OnLoadMoreListener {

    private var mType = 0
    private var mPosition = 0 // 首页-第一个tab-第一个recyclerview Item

    companion object {
        private const val VIDEO_TYPE = "VIDEO_TYPE"
        private const val VIDEO_POSITION = "VIDEO_POSITION"

        fun newInstance(type: Int, position: Int): Fragment {
            val args = Bundle()
            args.putInt(VIDEO_TYPE, type)
            args.putInt(VIDEO_POSITION, position)
            val fragment = MovieFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private val picContentPresenter by lazy { PicContentPresenter() }

    private var hotPage = 1 // 分页
    private val hotList: ArrayList<VedioBean> = arrayListOf()
    private lateinit var hotStyleAdapter: NewHotStyleAdapter
//    private lateinit var hotStyleAdapter: HotStyleAdapter

    override fun getContentViewLayoutID() = R.layout.fragment_movie

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

        initView()
        initListener()

        arguments?.let {
            mType = it.getInt(VIDEO_TYPE, mType)
            mPosition = it.getInt(VIDEO_POSITION, mPosition)
        }
    }

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

    override fun onDestroy() {
        super.onDestroy()
        picContentPresenter.detachView()
    }

    fun onRefresh() {
        if (AppConfig.fragmentClickFlag == Constant.FRAGMENT_CLICK_ONE) {
            println("mcg _______")
            hotPage = 1
//            hotList.clear()
            srlMainPage.resetNoMoreData()
            picContentPresenter.getHomePageImageList(mType, hotPage, Constant.PAGE_SIZE)
        }

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        hotPage = ++hotPage
        picContentPresenter.getHomePageImageList(mType, hotPage, Constant.PAGE_SIZE)
    }

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

        if (::hotStyleAdapter.isInitialized) {
//            hotList.addAll(data.list)
            if (hotPage == 1) {
                srlMainPage.finishRefresh(true)
                hotStyleAdapter.setNewInstance(data.list)
//                hotStyleAdapter.notifyDataSetChanged()
            } else {
                if (data.list.size < Constant.PAGE_SIZE) {
                    srlMainPage.finishLoadMoreWithNoMoreData()
                } else {
                    srlMainPage.finishLoadMore()
                }
//                hotStyleAdapter.notifyItemChanged(data.list.size)
                hotStyleAdapter.addData(data.list)
            }
        }
    }

    override fun getHomePageImageListFail() {
    }

    private fun initView() {
        val gridLayoutManager = MyGridLayoutManager(context, 3)
        rv_pic_list.layoutManager = gridLayoutManager
        hotStyleAdapter = NewHotStyleAdapter()
        rv_pic_list.adapter = hotStyleAdapter
        val emptyView =
            LayoutInflater.from(requireContext()).inflate(R.layout.item_empty_data, null)
        hotStyleAdapter.setEmptyView(emptyView)
//        hotStyleAdapter.setNewInstance(hotList)
        hotStyleAdapter.setOnItemClickListener { adapter, view, position ->
//            if (hotList.size > 0) {
                LocalVedioManager.startVedioDetailActivityForType(
                    requireActivity(),
                    hotStyleAdapter.data.get(position)
                )
//            }
        }

//        val gridLayoutManager = GridLayoutManager(context, 3)
//        rv_pic_list.layoutManager = gridLayoutManager
//        hotStyleAdapter = HotStyleAdapter(requireContext(), hotList)
//        rv_pic_list.adapter = hotStyleAdapter
//        hotStyleAdapter.setOnItemClickListener(object : HotStyleAdapter.OnItemClickListener {
//            override fun onItemClick(position: Int) {
//                if (hotList!=null&&hotList!!.size > 0) {
//                    LocalVedioManager.startVedioDetailActivityForType(
//                        requireActivity(),
//                        hotList!!.get(position)
//                    )
//                }
//            }
//        })
    }

    private fun initListener() {
        srlMainPage.setEnableRefresh(false)
        srlMainPage.setOnLoadMoreListener(this)
    }

}