package com.ym.chat.activity

import android.app.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.ym.chat.R
import com.ym.chat.adapter.VisitorApdater
import com.ym.chat.contract.VisitorContract
import com.ym.chat.presenter.VisitorPresenter
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.chat.ChatMessageChargeOrNotEntity
import com.zxhl.cms.net.model.chat.ChatStausEvent
import com.zxhl.cms.net.model.chat.VisitorEntity
import com.zxhl.cms.utils.*
import com.zxhl.cms.widget.ChatDialogUtils
import kotlinx.android.synthetic.main.activity_layout_visitor.*
import kotlinx.android.synthetic.main.layout_visitor_empty_view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VisitorActivity : BaseActivity(), VisitorContract.View,
    OnRecycleItemClickListener<VisitorEntity> {
    private var mPresenter: VisitorPresenter? = null
    private var mAdapter: VisitorApdater? = null
    private var isRefresh: Boolean = false
    var free: Int = 0
    var entity:ChatMessageChargeOrNotEntity?=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_visitor
    }

    fun initAdapter() {
        mAdapter = VisitorApdater(this, this, free)
        id_rl_visitor?.layoutManager = LinearLayoutManager(this)
        id_rl_visitor?.adapter = mAdapter

        id_rl_visitor.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 (isSlideToBottom(recyclerView)) {
                    if ((mAdapter?.list?.size ?: 0) >= 20) {
                        // mPresenter?.getVisitorList(false,free)
                        getData(false)
                    }
                }
            }
        })
        id_refresh_layout.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_aa60ff))
        id_refresh_layout?.setOnRefreshListener {
            // mPresenter?.getVisitorList(true, free)
            getData(true)
        }
    }
    override fun init() {
        EventBus.getDefault().register(this)
        id_img_back?.setOnClickListener {
            finish()

        }
        mPresenter = VisitorPresenter(this)
        getData(true)
    }


    fun getData(isRefresh: Boolean) {
        ChatMessageChargeUtils.getChatMessageChargeOrNot(
            "" + ChatMessageChargeUtils.MESSAGE_VISITOR,
            "",
            object : ChatCallback<ChatMessageChargeOrNotEntity> {
                override fun onResult(result: ChatMessageChargeOrNotEntity?) {
                    entity=result
                    if (result?.charge!! <= 0) {
                        free = 0
                        initAdapter()
                        mPresenter?.getVisitorList(isRefresh, free)
                    } else {

                        free = 2
                        initAdapter()
                        mPresenter?.getVisitorList(isRefresh, free)


                        return
                    }
                }


                override fun onError(code: String?, errorMsg: String?) {
                    if (TextUtils.isEmpty(errorMsg)) {
                        return
                    }
                    Utils.showToast(context, errorMsg)
                }
            })
    }
    fun showTrip(){
        ChatDialogUtils.showMemberChargeTip(
            mActivity as Activity,
            entity,
            "开启会员解锁无限查看访客权限",
            "星币解锁本次",
            object : ChatDialogUtils.ChatDialogCallBackParams {
                override fun confirm(type: Int) {
                    //兑换
                    if (type == 0) {
                        if (SettingPreference.getUserInfoData() != null
                            && SettingPreference.getUserInfoData().starBalance < entity!!.charge
                        ) {
                            ChatDialogUtils.showMemberTrip(
                                mActivity as Activity,
                                object : ChatDialogUtils.ChatDialogCallBack {
                                    override fun confirm() {
                                        JumpUtils.starCoinpayJump()
                                    }

                                    override fun cancel() {}
                                })
                        } else {
                            free = 1
                            initAdapter()
                            mPresenter?.getVisitorList(isRefresh, free)
                        }

                    } else {
                        JumpUtils.payJump()
                    }
                }

                override fun cancel() {}
            })
    }

    override fun setData(isRefresh: Boolean, reslut: List<VisitorEntity>?) {
        id_ll_empty?.visibility = View.GONE
        if(free==2){
            showTrip()
        }
        this.isRefresh = isRefresh
        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_ll_empty?.visibility = View.VISIBLE
        id_tv_visitor_empty?.text = "暂无访客，快去看看其他用户吧～"
    }
    var visitorEntity:VisitorEntity?=null
    override fun onItemClick(view: View?, position: Int, data: VisitorEntity?) {
        when (view?.id) {
            R.id.id_tv_chat->{
                visitorEntity=data
                JumpUtils.startC2CChatActivity(visitorEntity?.chatStatus!!.toInt(),data?.uid, data?.nickname)
            }
            R.id.id_rl_item->{
                if(free==2&&mAdapter!=null){
                   showTrip()
                }else {
                    JumpUtils.UserDetailJump(data?.uid, "0")
                }

            }
        }

    }

    fun isSlideToBottom(recyclerView: RecyclerView): Boolean {
        if (recyclerView == null) return false
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true
        return false
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(chatStausEvent: ChatStausEvent){
        visitorEntity?.chatStatus=chatStausEvent.chatstatus
        mAdapter?.notifyDataSetChanged()
    }
    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}