package com.zxbw.modulemain.fragment

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.zxbw.modulemain.R
import com.zxbw.modulemain.adapter.MyOrderAdapter
import com.zxbw.modulemain.contract.BoxOrderContract
import com.zxbw.modulemain.presenter.BoxOrderPresenter
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 kotlinx.android.synthetic.main.fragment_layout_magic_box_order.*
import kotlinx.android.synthetic.main.fragment_layout_magic_box_order.id_refresh_view
import kotlinx.android.synthetic.main.fragment_layout_recommend.*


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

    override fun init(view: View?) {
        mPresenter = BoxOrderPresenter(this)
        mAdapter = MyOrderAdapter(mActivity)
        box_order_recyclerView.layoutManager = LinearLayoutManager(mActivity)
        box_order_recyclerView.adapter = mAdapter
        mPresenter?.getOrderList(true)
        mPresenter?.getNotice()

        id_refresh_view.setRefreshHeader(ClassicsHeader(activity))
        id_refresh_view.setRefreshFooter(ClassicsFooter(activity))

        id_refresh_view.setOnRefreshListener {
            mPresenter?.getOrderList(true)
        }
        id_refresh_view.setOnLoadMoreListener {
            mPresenter?.getOrderList(false)
        }
    }

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

    override fun setOrderListData(isRefresh: Boolean, reslut: List<ShopOrderEntity>?) {
        if (isRefresh) {
            id_refresh_view.finishRefresh(!reslut.isNullOrEmpty());//传入false表示加载失败
        } else {
            id_refresh_view.finishLoadMore(!reslut.isNullOrEmpty());//传入false表示加载失败
        }
        if (isRefresh && !reslut.isNullOrEmpty()) {
            mAdapter?.clear()
        }
        if (!reslut.isNullOrEmpty()) {
            mAdapter?.appendToList(reslut)
        }
        setEmptyView(mAdapter?.list?.size ?: 0 == 0)
    }


    override fun setEmptyView(isEmpty: Boolean) {
        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
        }

    }
}