package com.zxhl.main.page.fragment

import android.text.TextUtils
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.NoticeEntity
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.BoxAdapter
import com.zxhl.main.page.adapter.BoxOrderAdapter
import com.zxhl.main.page.adapter.ShopOrderAdapter
import com.zxhl.main.page.contract.BoxOrderContract
import com.zxhl.main.page.presenter.BoxOrderPresenter

import kotlinx.android.synthetic.main.fragment_layout_magic_box_order.*


class MagicBoxOrderFragment:BaseFragment(),BoxOrderContract.View {
    private var mPresenter: BoxOrderPresenter? = null
    private var mAdapter: BoxOrderAdapter?=null
    override fun layoutID(): Int {
        return R.layout.fragment_layout_magic_box_order
    }

    override fun init(view: View?) {
        mPresenter = BoxOrderPresenter(this)
        mAdapter= BoxOrderAdapter(mActivity)
        box_order_recyclerView.layoutManager= LinearLayoutManager(mActivity)
        box_order_recyclerView.adapter=mAdapter
        mPresenter?.getOrderList(true)
        mPresenter?.getNotice()
        refreshLayout.setColorSchemeColors(resources.getColor(R.color.color_3A95FD))
        refreshLayout?.setOnRefreshListener {
            mPresenter?.getOrderList(true)
        }
        box_order_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?.getOrderList(false)
                    }
                }
            }
        })

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

    override fun setOrderListData(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 setEmptyView(isEmpty: Boolean) {
        refreshLayout?.isRefreshing = false
        if (isEmpty) {
            id_order_empty.visibility = View.VISIBLE
        } else {
            id_order_empty.visibility = View.GONE
        }
    }

    override fun setNotice(reslut: NoticeEntity?) {
        if(!TextUtils.isEmpty(reslut?.orderNotice)){
            id_ll_notice?.visibility=View.VISIBLE
            id_tv_order_notice?.text=reslut?.orderNotice
        }else{
            id_ll_notice?.visibility=View.GONE
        }

    }
}