package com.zswc.creativity.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.Coil
import coil.ImageLoader
import coil.request.CachePolicy
import coil.util.CoilUtils
import com.hwangjr.rxbus.RxBus
import com.ysnows.base.utils.B
import com.ysnows.base.utils.UiSwitch
import com.zswc.creativity.App
import com.zswc.creativity.activity.TibetanDetailsActivity
import com.zswc.creativity.adapter.RecommedAdapter
import com.zswc.creativity.base.MBFragment
import com.zswc.creativity.config.ConfigRx
import com.zswc.creativity.databinding.FragmentRecommendBinding
import com.zswc.creativity.model.CollectionList
import com.zswc.creativity.vmodel.RecommendVModel


/**
 * 精选推荐
 *Created by shihuiyun
 *on 2020/9/11
 */
class RecommendFragment : MBFragment<RecommendVModel, FragmentRecommendBinding>() {


    var  page:Int=1
    var listTo=ArrayList<CollectionList>()//未加载到界面的剩余的数据
    var listTwo=ArrayList<CollectionList>()
    var adapter:RecommedAdapter? = null
    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        //禁止下拉刷新和下拉加载
        binding.recyclerRecommend.overScrollMode = View.OVER_SCROLL_NEVER
//        val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
//        binding.recyclerRecommend.layoutManager = linearLayoutManager

         adapter = RecommedAdapter()
        binding.recyclerRecommend.adapter = adapter

        vm.toGetCollectionList()

        vm.list.observe(this, androidx.lifecycle.Observer {
            if (null == it) {
                return@Observer
            }
//            if(it.size>10){
//                var list=ArrayList<CollectionList>()
//               var i=0
//                for(item in it){
//                   if(i<10){
//                       list.add(item)
//                   }else{
//                       listTo.add(item)
//                   }
//                    i++
//                }
//                setAdapter(list)
//                return@Observer
//            }
           setAdapter(it)

        })


    }



    override fun listeners() {
        super.listeners()

//        binding.recyclerRecommend.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
//                super.onScrolled(recyclerView, dx, dy)
//                val topRowVerticalPosition =
//                        if (recyclerView == null || recyclerView.childCount === 0)
//                            0 else recyclerView.getChildAt(0).top
//                // 大于0表示正在向上滑动，小于等于0表示停止或向下滑动
//
//                var flag= topRowVerticalPosition >= 0
//                //发送消息
//                RxBus.get().post(ConfigRx.STOP_REFRSH, flag)
//
//            }
//        })



//        binding.recyclerRecommend.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
//                super.onScrollStateChanged(recyclerView, newState)
//            }
//
//            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
//                super.onScrolled(recyclerView, dx, dy)
//                if (isSlideToBottom(recyclerView)) {
//                  if(page*10<vm.list.value!!.size ){
//                      //说明还有数据
//                      var i=0
//                      var list=ArrayList<CollectionList>()
//
//                      for(item in listTo){
//                          if(i<10){
//                              list.add(item)
//                          }else{
//                              listTwo.add(item)
//                          }
//                      }
//
//                      listTo.clear()
//                      listTo.addAll(listTwo)
//                      listTwo.clear()
//                      //设置adapter
//                      adapter?.addData(list)
//
//                      page++
//                  }
//                }
//            }
//        })

    }

    private fun isSlideToBottom(recyclerView: RecyclerView?): Boolean {
        if (recyclerView == null) return false
        return recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange()
    }

    fun setAdapter(list: List<CollectionList>) {



        adapter?.addData(list)
        adapter?.setOnItemClickListener { adapter, view, position ->


            vm.list.value?.get(position)?.id

            UiSwitch.bundle(context, TibetanDetailsActivity::class.java,
                    B().putString("id", vm.list.value?.get(position)?.id)
                            .ok())
        }


    }


    override val isRxbus: Boolean
        get() = true

    override fun vmClass(): Class<RecommendVModel> {
        return RecommendVModel::class.java
    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentRecommendBinding {
        return FragmentRecommendBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return null
    }
}
