package com.zxhl.main.page.fragment

import android.util.Log
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.AppInEntity
import com.zxhl.cms.net.model.box.DepotEntity
import com.zxhl.cms.net.model.box.NoticeEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.adapter.PickUpAdapter
import com.zxhl.main.page.adapter.ReceiveAdapter
import com.zxhl.main.page.adapter.WareHouseAdapter
import com.zxhl.main.page.contract.WareHouseContract
import com.zxhl.main.page.presenter.WareHousePresenter
import kotlinx.android.synthetic.main.fragment_warehouse_layout.*
import kotlinx.android.synthetic.main.layout_empty.*

//待收货
class ReceivingFragment : BaseFragment(), WareHouseContract.View,
    OnRecycleItemClickListener<DepotEntity> {
    private var mPresenter: WareHousePresenter? = null
    private var mAdapter: ReceiveAdapter? = null
    private var status: Int? = 1
    private var type: Int? = 0
    override fun layoutID(): Int {
        return R.layout.fragment_layout_other
    }

    override fun init(view: View?) {
//        id_tv_warehouse_pick_up_goods?.setOnClickListener {
//            id_tv_warehouse_pick_up_goods?.setBackgroundResource(R.drawable.shape_ff2e31_r12_bg)
//            id_tv_warehouse_pick_up_goods.setTextColor(getResources().getColor(R.color.color_ff2e31))
//            id_tv_warehouse_card?.setBackgroundResource(R.drawable.shape_ffffff_r12_bg)
//            id_tv_warehouse_card.setTextColor(getResources().getColor(R.color.color_666666))
//            type = 0
//            mPresenter?.getDepot(status, type, true)
//        }
//        id_tv_warehouse_card?.setOnClickListener {
//            id_tv_warehouse_pick_up_goods?.setBackgroundResource(R.drawable.shape_ffffff_r12_bg)
//            id_tv_warehouse_pick_up_goods.setTextColor(getResources().getColor(R.color.color_666666))
//            id_tv_warehouse_card?.setBackgroundResource(R.drawable.shape_ff2e31_r12_bg)
//            id_tv_warehouse_card.setTextColor(getResources().getColor(R.color.color_ff2e31))
//            type = 1
//            mPresenter?.getDepot(status, type, true)
//        }
        id_tv_huishou?.setOnClickListener {

        }
        id_tv_fahuo?.setOnClickListener {

        }

        mPresenter = WareHousePresenter(this)
        mPresenter?.getDepot(status, type, true)
        mAdapter = ReceiveAdapter(mActivity, this)
        recyclerView.layoutManager = LinearLayoutManager(mActivity)
        recyclerView?.adapter = mAdapter
        //      recyclerView?.setNestedScrollingEnabled(true)
        Utils.setOnScrollBottomListener(recyclerView) { _, _ ->
            if (mAdapter?.getLoadMore() == true) {
                mPresenter?.getDepot(status, type, false)
            }
        }
        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 (isSlideToBottom(recyclerView)) {
                    if (mAdapter?.getLoadMore() == true) {
                        mPresenter?.getDepot(status, type, false)
                    }
                }
            }
        })
        refreshLayout?.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_3A95FD))
        refreshLayout?.setOnRefreshListener {
            mPresenter?.clearPage()
            mPresenter?.getDepot(status, type, true)
        }
    }

    override fun lazyLoad() {
        super.lazyLoad()
        if (isInitial && isVisible) {
            mPresenter?.getDepot(status, type, true)
        }

    }

    fun isSlideToBottom(recyclerView: RecyclerView): Boolean {
        if (recyclerView == null) return false
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true
        return false
    }

    companion object {

        fun newInstance() = ReceivingFragment()
    }

    override fun setWareHouseData(boolean: Boolean, reslut: List<DepotEntity>?) {
        if (boolean) {
            mAdapter?.clear()
            refreshLayout?.isRefreshing = false
        }
        if (reslut?.isEmpty() == true) {
            id_ll_empty?.visibility = View.VISIBLE
            id_tv_dixian?.visibility = View.GONE
        } else {
            id_ll_empty?.visibility = View.GONE
            id_tv_dixian?.visibility = View.VISIBLE
        }
        mAdapter?.appendToList(reslut)
        mAdapter?.notifyDataSetChanged()
    }

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

    override fun recoverSuccess() {

    }

    override fun loadError() {
        refreshLayout?.isRefreshing = false
        id_tv_dixian?.visibility = View.GONE
    }

    override fun setAppInfo(reslut: AppInEntity?) {

    }

    override fun setNotice(reslut: NoticeEntity?) {

    }

    override fun onItemClick(view: View?, position: Int, data: DepotEntity?) {

    }

}