package com.zxhl.main.page.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.model.chat.ChatStausEvent
import com.zxhl.cms.net.model.chat.FollowEntity
import com.zxhl.cms.utils.*
import com.zxhl.main.R
import com.zxhl.main.page.adapter.LikesAdpater
import com.zxhl.main.page.contract.MyLikesContract
import com.zxhl.main.page.presenter.MyLikesPresenter
import kotlinx.android.synthetic.main.activity_layout_my_likes.*
import kotlinx.android.synthetic.main.layout_empty_view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class LikeFragment : BaseFragment(), MyLikesContract.View,
    OnRecycleItemClickListener<FollowEntity> {
    private var mPresenter: MyLikesPresenter? = null
    private var mAdapter: LikesAdpater? = null
    override fun layoutID(): Int {
        return R.layout.fragment_layout_like
    }

    override fun init(view: View?) {
        mPresenter = MyLikesPresenter(this)
        initAdapter()
    }
    var mylikeEntity:FollowEntity?=null
    override fun onItemClick(view: View?, position: Int, data: FollowEntity?) {
        mylikeEntity=data
        JumpUtils.startC2CChatActivity(mylikeEntity?.chatStatus!!,data?.uid, data?.nickname)
    }

    override fun onResume() {
        super.onResume()
        mPresenter?.getLikesList(true)
    }
    fun initAdapter() {
        mAdapter = LikesAdpater(mActivity, this)
        id_rl_likes?.layoutManager = LinearLayoutManager(mActivity)
        id_rl_likes?.adapter = mAdapter
        id_rl_likes.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 (Utils.isSlideToBottom(recyclerView)) {
                    if ((mAdapter?.list?.size ?: 0) >= 20) {
                         mPresenter?.getLikesList(false)

                    }
                }
            }
        })
        id_refresh_layout.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_aa60ff))
        id_refresh_layout?.setOnRefreshListener {
            mPresenter?.getLikesList(true)
        }
    }



    override fun setData(isRefresh: Boolean, reslut: List<FollowEntity>?) {


        id_ll_empty?.visibility = View.GONE
        if (isRefresh) {
            mAdapter?.clear()
        }
        id_refresh_layout?.isRefreshing = false
        val start = mAdapter?.getItemCount() ?: 0
        mAdapter?.appendToList(reslut)
        if (isRefresh) {
            mAdapter?.notifyDataSetChanged()
        } else {
            val end = mAdapter?.getItemCount() ?: 0
            mAdapter?.notifyItemRangeChanged(start, end)
        }
    }

    override fun setEmptyView() {
        id_refresh_layout?.isRefreshing = false
        id_ll_empty?.visibility = View.VISIBLE
        id_tv_empty_text.text = "No Content"
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(chatStausEvent: ChatStausEvent){
        mylikeEntity?.chatStatus=chatStausEvent.chatstatus?.toInt()
        mAdapter?.notifyDataSetChanged()
        //chatStaus=chatStausEvent.chatstatus?.toInt()
    }
    override fun onDestroy() {
        super.onDestroy()
      //  EventBus.getDefault().unregister(this)
    }
    companion object {

        fun newInstance() = LikeFragment()
    }

}