package com.swago.user.black

import android.graphics.Rect
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.swago.baseswago.HandleVm
import com.swago.baseswago.UserVm
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.fragment.BaseListFragment
import com.swago.baseswago.model.mine.RoomBlack
import com.swago.baseswago.model.mine.RoomBlackItem
import com.swago.baseswago.util.DpPxUtil
import com.swago.user.R
import com.swago.user.adapter.BlackLiveRoomAdapter
import com.swago.user.adapter.RoomBlackAdapter
import com.swago.user.databinding.FragmentBlackBinding

/**
 *@date 2022/2/26 09:47
 *description:直播间黑名单
 */
class BlackLiveRoomFragment : BaseListFragment<FragmentBlackBinding, RoomBlackItem>() {

    private val userVm by viewModels<UserVm>()
    private val handleVm by viewModels<HandleVm>()

    private var position = -1

    override val smartRecyclerView: SwagoRecyclerView<RoomBlackItem> by lazy {
        binding.swagoRv as SwagoRecyclerView<RoomBlackItem>
    }
    override val adapter: BaseQuickAdapter<RoomBlackItem, *> by lazy {
        BlackLiveRoomAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(context)
    }
    override val loadData: (page: Int) -> Unit = {
        userVm.getRoomBlackList(it+1)
    }

    private lateinit var data:RoomBlackItem

    override fun initViewData() {
        smartRecyclerView.recyclerView?.addItemDecoration(VerticalItemDecoration())
        adapter.setOnItemChildClickListener { adapter, view, position ->
            data = adapter.data[position] as RoomBlackItem
            when(view.id){
                R.id.switch1 -> {
                    //禁言和取消禁言
                    if (adapter.data.size > position) {
                        this.position = position
                        handleVm.muteOrCancel(data.user_id,data.broadcast_user_id,type = if(data.is_black==1)0 else 1)
                    }
                }

                R.id.switch2 -> {
                    //拉黑和取消拉黑
                    if (adapter.data.size > position) {
                        /* * black_type  用户类型 1拉黑主播 2拉黑用户
                         * type 类型(0取消拉黑1拉黑)*/
                        this.position = position
                        handleVm.kickOutRoom(data.user_id,data.broadcast_user_id,type = if(data.is_black==1)0 else 1)
                    }
                }
            }

        }
    }

    override fun initLiveData() {
        userVm.roomBlackListLiveData.observe(this){
            if (it == null){
                smartRecyclerView.onFetchDataError()
            }else{
                smartRecyclerView.onFetchDataFinish(it,true)
            }
        }

        handleVm.blackTypeLiveData.observe(this) {
            if (it == 0) {
                adapter.data.removeAt(position)
                adapter.notifyItemRemoved(position)
            }
        }

        handleVm.kickSuccessLiveData.observe(this){
            if (data.is_black == 1){
                data.is_black = 0
            }else{
                data.is_black = 1
            }
            adapter.notifyDataSetChanged()
        }

        handleVm.muteOrCancelLiveData.observe(this){
            if (data.is_forbid == 1){
                data.is_forbid = 0
            }else{
                data.is_forbid = 1
            }
            adapter.notifyDataSetChanged()
        }
    }

    override fun loadMoreNeed(): Boolean {
        return false
    }


    inner class VerticalItemDecoration : RecyclerView.ItemDecoration() {

        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            val margin = DpPxUtil.dip2px(20f)
            outRect.set(0, margin, 0, 0)
        }
    }


}