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.FansAdapter
import com.zxhl.main.page.contract.FansContract
import com.zxhl.main.page.presenter.FansPresenter
import com.zxhl.main.page.view.DialogUtils
import kotlinx.android.synthetic.main.activity_layout_my_fans.*
import kotlinx.android.synthetic.main.layout_empty_view.*

class MyFansActivity:BaseActivity() ,FansContract.View,OnRecycleItemClickListener<FollowEntity>{
    private var mPresenter: FansPresenter? = null
    private var mAdapter: FansAdapter? = 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_layout_my_fans
    }

    override fun init() {
        id_img_back?.setOnClickListener {
            finish()
        }
        mPresenter = FansPresenter(this)
        mAdapter = FansAdapter(this, this)
        id_rl_fans?.layoutManager = LinearLayoutManager(this)
        id_rl_fans?.adapter = mAdapter
        id_rl_fans.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?.getFansList(false)
                    }
                }
            }
        })
        id_refresh_layout.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_aa60ff))
        id_refresh_layout?.setOnRefreshListener {
            mPresenter?.getFansList(true)
        }
        mPresenter?.getFansList(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() {
        id_refresh_layout?.isRefreshing = false
        id_ll_empty?.visibility=View.VISIBLE
        id_tv_empty_text.text="暂无粉丝，快去看看其他用户～"
    }
    var follow:String?=""
    override fun followSuc() {

        if(!TextUtils.isEmpty(follow)){
            if(TextUtils.equals(follow,"0")){
                fansEntity?.hasFollows="1"
            }else{
                fansEntity?.hasFollows="0"
            }
            mAdapter?.notifyDataSetChanged()
        }

    }
    var fansEntity:FollowEntity?=null
    override fun onItemClick(view: View?, position: Int, data: FollowEntity?) {
        when (view?.id) {
            R.id.id_tv_if_follow->{
                fansEntity=data
                follow=fansEntity?.hasFollows
               if(TextUtils.equals(fansEntity?.hasFollows,"0")){
                   mPresenter?.followUser(data?.uid)
               }else{
                   DialogUtils.showCanleFollow(this, View.OnClickListener {
                       mPresenter?.followUser(data?.uid)
                   })
               }

            }



            R.id.id_rl_item->{
                JumpUtils.UserDetailJump(data?.uid,"0")
            }
        }

    }
}