package com.bc_chat.circle.activity

import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import com.bc_chat.bc_base.entity.contact.UserCircleDetailModel
import com.bc_chat.bc_base.utils.AppConfig
import com.bc_chat.bc_base.utils.gone
import com.bc_chat.circle.adapetr.UserCircleAdapter
import com.bc_chat.circle.contract.TopicDetailsContract
import com.bc_chat.circle.dataSource.UserCircleDataSource
import com.bc_chat.circle.presenter.TopicDetailsPresenter
import com.bc_chat.contacts.R
import com.zhaohaoting.framework.abs.BaseRecyclerActivity
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter
import com.zhaohaoting.framework.abs.presenter.BasePresenter
import com.zhaohaoting.framework.mvchelper.mvc.IAsyncDataSource
import com.zhaohaoting.framework.utils.ActivityUtils
import com.zhaohaoting.framework.utils.glid.GlideUtils

/**
 * author：Created by zhaohaoting on 2019/8/29
 * email：526309416@qq.com
 * desc：
 */
class UserCircleDetailsActivity : BaseRecyclerActivity<UserCircleDetailModel.Topic, UserCircleDetailModel>(), TopicDetailsContract.View {
    override fun deleteSuccess(position: Int) {
    }

    override fun commentSuccess() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }


    private val cuid by lazy {
        intent.getStringExtra("cuid")
    }
    private var ivPhoto: ImageView? = null
    private var tvFollowCount: TextView? = null
    private var tvFansCount: TextView? = null
    private var ivBack: ImageView? = null
    private var tvTitle: TextView? = null
    private var tvFollow: TextView? = null
    private var rvTitle: TextView? = null
    private var userCircleModel: UserCircleDetailModel? = null

    override fun initPresenter(): BasePresenter<*> {
        return TopicDetailsPresenter(this)
    }

    override fun getAdapter(): AbsRecyclerAdapter<UserCircleDetailModel.Topic, UserCircleDetailModel> = UserCircleAdapter(this)

    override fun getDataSource(): IAsyncDataSource<UserCircleDetailModel> = UserCircleDataSource(cuid)

    override fun addHeadViews(adapter: AbsRecyclerAdapter<UserCircleDetailModel.Topic, UserCircleDetailModel>?) {
        val inflate = LayoutInflater.from(this).inflate(R.layout.header_user_circle_details, recyclerView, false)
        ivPhoto = inflate.findViewById(R.id.iv_photo)
        tvFollowCount = inflate.findViewById(R.id.tv_follow)
        tvFansCount = inflate.findViewById(R.id.tv_fans)
        rvTitle = inflate.findViewById(R.id.rv_title)
        adapter?.addHeaderView(inflate)
        if (cuid.equals(AppConfig.instance.getUser().userId)) {
            rvTitle.gone()
        }
    }

    override fun onInit() {
        appBarLayout.removeView(flToolbar)
        flContent.setBackgroundColor(resources.getColor(R.color.white))
        val inflate = LayoutInflater.from(this).inflate(R.layout.title_user_circle_details, flTitle, true)
        ivBack = inflate.findViewById(R.id.iv_back)
        tvTitle = inflate.findViewById(R.id.text_title)
        tvFollow = inflate.findViewById(R.id.tv_follow)
        if (cuid.equals(AppConfig.instance.getUser().userId)) {
            tvFollow.gone()
        }
        setOnStateChangeListener { adapter, result ->
            this.userCircleModel = result;
            GlideUtils.loadCircleImage(this, result.photo, ivPhoto)
            tvFollowCount?.text = "关注：%d".format(result.focusCount)
            tvFansCount?.text = "粉丝：%d".format(result.fansCount)
            tvTitle?.text = result.nickname
            tvFollow?.text = if (result.is_focus == 1) {
                "已关注"
            } else {
                "+关注"
            }
        }
        tvFollow?.setOnClickListener {
            userCircleModel?.let {
                (presenter as TopicDetailsPresenter).like(cuid, if (it.is_focus == 1) 2 else 1)
            }
        }
        ivBack?.setOnClickListener {
            closeActivity()
        }
    }

    override fun likeSuccess(type: Int) {
        when (type) {
            1 -> {
                userCircleModel?.is_focus = 1
                //关注成功
                tvFollow?.text = "已关注"
            }
            2 -> {
                userCircleModel?.is_focus = 0
                //取消关注成功
                tvFollow?.text = "+关注"
            }
        }
    }

    override fun itemClicked(vh: AbsRecyclerAdapter.BaseViewHolder<*>?, position: Int) {
        super.itemClicked(vh, position)
        val topic = adapter.data.list!![position]
        ActivityUtils.newInstance(this).putExtra("cid", topic.id).navigation(TopicDetailsActivity::class.java)
    }
}