package com.zxhl.main.page.activity

import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.model.chat.FollowEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.adapter.BlockAdapter
import com.zxhl.main.page.contract.BlockContract
import com.zxhl.main.page.presenter.BlockPresenter
import kotlinx.android.synthetic.main.activity_layout_block_list.*
import kotlinx.android.synthetic.main.activity_layout_block_list.id_img_back
import kotlinx.android.synthetic.main.activity_layout_block_list.id_refresh_layout
import kotlinx.android.synthetic.main.layout_empty_view.*

class BlockListActivity : BaseActivity(), BlockContract.View,
    OnRecycleItemClickListener<FollowEntity> {
    private var mPresenter: BlockPresenter? = null
    private var mAdapter: BlockAdapter? = null
    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    override fun onClick(v: View?) {

    }

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

    override fun init() {
        mPresenter = BlockPresenter(this)
        id_img_back?.setOnClickListener {
            finish()
        }
        mAdapter = BlockAdapter(this, this)
        id_rl_black?.layoutManager = LinearLayoutManager(this)
        id_rl_black?.adapter = mAdapter
        Utils.setOnScrollBottomListener(id_rl_black) { _, _ ->
            mPresenter?.getBlackList(false)
        }
        id_rl_black.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?.getBlackList(false)
                    }
                }
            }
        })
        id_refresh_layout.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_aa60ff))
        id_refresh_layout?.setOnRefreshListener {
            mPresenter?.getBlackList(true)
        }
        mPresenter?.getBlackList(true)
    }


    override fun setData(isRefresh: Boolean, reslut: List<FollowEntity>?) {
        id_ll_empty?.visibility = View.GONE
        if (isRefresh) {
            mAdapter?.clear()
        }
        id_refresh_layout?.isRefreshing = 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() {
        if(mAdapter!=null){
            mAdapter?.clear()
        }
        id_refresh_layout?.isRefreshing = false
        id_ll_empty?.visibility = View.VISIBLE
        id_tv_empty_text.text = "There is no blacklist yet"
    }

    override fun blackSuc() {
        mPresenter?.getBlackList(true)
    }

    override fun onItemClick(view: View?, position: Int, data: FollowEntity?) {
        when (view?.id) {
            R.id.id_tv_remove_black -> {
                mPresenter?.blackUser(data?.uid)
            }
        }
    }
}