package com.zxhl.main.page.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.model.box.HomeBoxListEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.adapter.BoxAdapter
import com.zxhl.main.page.contract.HomeContract
import com.zxhl.main.page.presenter.HomePresenter
import kotlinx.android.synthetic.main.fragment_refresh_layout.*

class MemberCardFragment : BaseFragment(), HomeContract.View,
    OnRecycleItemClickListener<HomeBoxListEntity> {
    private var mPresenter: HomePresenter? = null
    private var mAdapter: BoxAdapter? = null
    override fun layoutID(): Int {
        return R.layout.fragment_refresh_layout
    }

    override fun init(view: View?) {
        mPresenter = HomePresenter(this)
        mAdapter = BoxAdapter(mActivity, this)
        recyclerView.layoutManager = LinearLayoutManager(mActivity)
        recyclerView?.adapter = mAdapter

        Utils.setOnScrollBottomListener(recyclerView) { _, _ ->
            if (mAdapter?.getLoadMore() == true) {
                mPresenter?.getHomeBoxList("会员卡",false)
            }
        }
        refreshLayout?.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_3A95FD))
        refreshLayout?.setOnRefreshListener {
            mPresenter?.clearPage()
            mPresenter?.getHomeBoxList("会员卡",true)
        }
    }

    override fun lazyLoad() {
        super.lazyLoad()
        if (isInitial && isVisible&&mAdapter?.itemCount==0) {
            mPresenter?.getHomeBoxList("会员卡",true)
        }
    }


    override fun setHomeData(boolean: Boolean, reslut: List<HomeBoxListEntity>?) {
        if (boolean) {
            refreshLayout?.isRefreshing = false
            if (mAdapter != null) {
                mAdapter?.clear()
            }
        }
        mAdapter?.appendToList(reslut)
        mAdapter?.notifyDataSetChanged()

    }

    override fun setLoadMore(isBoolean: Boolean) {
        mAdapter?.setLoadMore(isBoolean)
    }



    companion object {

        fun newInstance() = MemberCardFragment()
    }

    override fun onItemClick(view: View?, position: Int, data: HomeBoxListEntity?) {
        JumpUtils.goodsDetailJump(data?.boxId)
    }
}