package com.zxhl.main.page.activity

import android.text.TextUtils
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.JumpUtils
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.adapter.FollowAdapter
import com.zxhl.main.page.contract.FollowContract
import com.zxhl.main.page.presenter.FollowPresenter
import com.zxhl.main.page.view.DialogUtils
import kotlinx.android.synthetic.main.activity_layout_user_detail.*
import kotlinx.android.synthetic.main.activity_my_follow.*
import kotlinx.android.synthetic.main.activity_my_follow.id_img_back
import kotlinx.android.synthetic.main.layout_empty_view.*

class MyFollowActivity : BaseActivity(), FollowContract.View,
    OnRecycleItemClickListener<FollowEntity> {
    private var mPresenter: FollowPresenter? = null
    private var mAdapter: FollowAdapter? = null
    override fun onClick(v: View?) {

    }

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

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

    override fun init() {
        id_img_back?.setOnClickListener {
            finish()
        }
        mPresenter = FollowPresenter(this)
        mAdapter = FollowAdapter(this, this)
        id_rl_follow?.layoutManager = LinearLayoutManager(this)
        id_rl_follow?.adapter = mAdapter
        Utils.setOnScrollBottomListener(id_rl_follow) { _, _ ->
            mPresenter?.getFollowList(false)
        }
        id_rl_follow.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?.getFollowList(false)
                    }
                }
            }
        })
        id_refresh_layout.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_aa60ff))
        id_refresh_layout?.setOnRefreshListener {
            mPresenter?.getFollowList(true)
        }
        mPresenter?.getFollowList(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 = "暂无关注，快去看看其他用户～"
    }

    var flow: String? = ""
    override fun followSuc() {
        Log.e("MXL","成功"+flow)
        if(!TextUtils.isEmpty(flow)){
            if(TextUtils.equals(flow,"0")){
                followEntity?.hasFollows="1"
            }else{
                followEntity?.hasFollows="0"
            }
            mAdapter?.notifyDataSetChanged()
        }
    }
    var followEntity:FollowEntity?=null
    override fun onItemClick(view: View?, position: Int, data: FollowEntity?) {
        when (view?.id) {
            R.id.id_rl_follow_item -> {
                JumpUtils.UserDetailJump(data?.uid, "0")
            }
            R.id.id_tv_if_follow -> {
                followEntity=data
                flow=data?.hasFollows
                if(TextUtils.equals(followEntity?.hasFollows,"0")){
                    mPresenter?.followUser(data?.uid)
                }else{
                    DialogUtils.showCanleFollow(this, View.OnClickListener {
                        mPresenter?.followUser(data?.uid)
                    })

                }

            }

        }
    }

}