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.box.ShopOrderEntity
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.adapter.ShopOrderAdapter
import com.zxhl.main.page.contract.ShopOrderListContract
import com.zxhl.main.page.presenter.ShopOrderListPresenter
import kotlinx.android.synthetic.main.fragment_shop_order.*

/**
 * @author (wangXuewei)
 * @datetime 2022-03-02 18:28 GMT+8
 * @detail :
 */
class ShopOrderListFragment : BaseFragment(), ShopOrderListContract.View {
    private var mPresenter: ShopOrderListPresenter? = null
    private var mAdapter:ShopOrderAdapter?=null
    override fun layoutID(): Int {
        return R.layout.fragment_shop_order
    }

    override fun init(view: View?) {
        mPresenter = ShopOrderListPresenter(this)
        mAdapter= ShopOrderAdapter(mActivity)
        recyclerView.layoutManager=LinearLayoutManager(mActivity)
        recyclerView.adapter=mAdapter
        mPresenter?.getShopOrderList(true)
        refreshLayout.setColorSchemeColors(resources.getColor(R.color.color_3A95FD))
        refreshLayout?.setOnRefreshListener {
            mPresenter?.getShopOrderList(true)
        }
        recyclerView.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) > 0) {
                        mPresenter?.getShopOrderList(false)
                    }
                }
            }
        })

    }

    companion object {
        fun newInstance() = ShopOrderListFragment()
    }

    override fun setShopOrderListData(isRefresh: Boolean, reslut: List<ShopOrderEntity>?) {
        if (isRefresh) {
            mAdapter?.clear()
        }
        setEmptyView(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 setLoadMore(isBoolean: Boolean) {

    }

    override fun setEmptyView(isEmpty: Boolean) {
        refreshLayout?.isRefreshing = false
        if (isEmpty) {
            id_order_empty.visibility = View.VISIBLE
        } else {
            id_order_empty.visibility = View.GONE
        }
    }
}