package com.zxhl.cms.pay

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.R
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.model.chat.RecordEntity
import com.zxhl.cms.utils.RecyclerViewScrollListener
import com.zxhl.cms.utils.Utils
import kotlinx.android.synthetic.main.activity_layout_coin_record.*
import kotlinx.android.synthetic.main.layout_record_empty_view.*
//星币充值记录页面
class CoinPayRecordActivity:BaseActivity(), RecordContract.View  {
    private var mPresenter: RecordPresenter? = null
    private var mAdapter: CoinRecordAdapter? = null
    override fun onClick(v: View?) {

    }

    override fun layoutID(): Int {
        return R.layout.activity_layout_coin_record
    }

    override fun before() {
        super.before()
        setStatusBarBackground(resources.getColor(R.color.transparent))
    }
    override fun init() {
        id_img_back?.setOnClickListener {
            finish()
        }
        mPresenter = RecordPresenter(this)
        mAdapter = CoinRecordAdapter(this)
        id_rl_coin_record?.layoutManager = LinearLayoutManager(this)
        id_rl_coin_record?.adapter = mAdapter
        id_rl_coin_record.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?.getCoinRecord(false)
                    }
                }
            }
        })

//        id_rl_coin_record?.addOnScrollListener(object : RecyclerViewScrollListener() {
//            override fun onScrollToBottom() {
//                super.onScrollToBottom()
//                if ((mAdapter?.list?.size ?: 0) > 0) {
//                    mPresenter?.getCoinRecord(false)
//                }
//            }
//        })
        id_refresh_layout.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_aa60ff))
        id_refresh_layout?.setOnRefreshListener {
            mPresenter?.getCoinRecord(true)
        }
        mPresenter?.getCoinRecord(true)
    }

    override fun setData(boolean: Boolean, reslut: List<RecordEntity>?) {
        id_layout_empty?.visibility=View.GONE
        if (boolean) {
            mAdapter?.clear()
        }

        id_refresh_layout?.isRefreshing = false
        val start = mAdapter?.getItemCount() ?: 0
        mAdapter?.appendToList(reslut)
        if (boolean) {
            mAdapter?.notifyDataSetChanged()
        } else {
            val end = mAdapter?.getItemCount() ?: 0
            mAdapter?.notifyItemRangeChanged(start, end)
        }
    }

    override fun setEmptyView() {
        id_refresh_layout?.isRefreshing = false
        id_layout_empty?.visibility=View.VISIBLE
        id_tv_empty_trip?.text="暂无星币充值记录～"
    }
}