package com.rdiot.yx485.adapter

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.rdiot.yx485.R
import com.rdiot.yx485.base.LocalData
import com.rdiot.yx485.bean.RoomData
import com.rdiot.yx485.databinding.ItemRoomBinding
import com.rdiot.yx485.util.dp
import com.rdiot.yx485.util.setClickLimitListener

/**
 * 房间列表
 * @author mR2hao
 * @date 2021/10/14
 */
class RoomsAdapter(
    private val mListener: OnItemClickListener? = null
) :
    RecyclerView.Adapter<RoomsAdapter.RoomViewHolder>() {
    /** 变量 是否为编辑模式 */
    private var isEditMode = false
    private var mList: MutableList<RoomData> = mutableListOf()

    interface OnItemClickListener {
        fun onItemClicked(position: Int, roomInfo: RoomData)
        fun onSwitchClicked(position: Int, roomInfo: RoomData)
        fun onDelClicked(position: Int, roomInfo: RoomData)
    }

    inner class RoomViewHolder(val binding: ItemRoomBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(info: RoomData) {
            binding.vm = info
            binding.editMode = isEditMode
            binding.executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RoomViewHolder {
        val binding: ItemRoomBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_room,
            parent,
            false
        )

        return RoomViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RoomViewHolder, position: Int) {
        val bean = mList[position]
        holder.bind(bean)

        holder.binding.clItem.setClickLimitListener {
            mListener?.onItemClicked(position, bean)
        }

        holder.binding.ivSwitch.setClickLimitListener {
            mListener?.onSwitchClicked(position, bean)
        }

        holder.binding.ivDel.setClickLimitListener {
            mListener?.onDelClicked(position, bean)
        }

    }

    override fun getItemCount(): Int {
        return mList.size
    }

    /** 切换是否为编辑模式 */
    fun setEditMode(on: Boolean) {
        if (isEditMode != on) {
            isEditMode = on
        }
        notifyDataSetChanged()
    }

    fun isEditMode() = isEditMode

    @Synchronized
    fun update(list: List<RoomData>) {
        if (list.size < this.mList.size) {
            mList.clear()
            mList.addAll(list)
            notifyDataSetChanged()
        } else {
            for (i in list.indices) {
                if (i < this.mList.size) {
                    if (this.mList[i] != list[i]) {
                        this.mList[i] = list[i]
                        notifyItemChanged(i)
                    }
                } else {
                    this.mList.add(list[i])
                    notifyItemInserted(i)
                }
            }
        }
    }

    fun removeRoom(roomData: RoomData) {
        mList.remove(roomData)
        val tempList = mutableListOf<RoomData>()
        for (room in mList) {
            tempList.add(room)
        }
        LocalData.familyData.postValue(
            LocalData.familyData.value.apply {
                this?.room = mList
            }
        )
        notifyDataSetChanged()

//        update(tempList)
    }

}

class RoomItemDecoration(private val top: Int = 0, private val bottom: Int) :
    RecyclerView.ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)

        //获取当前要进行布局的item的position
        when (parent.getChildLayoutPosition(view)) {
            0, 1 -> {
                outRect.top = top
                outRect.bottom = bottom
            }
            else -> {
                outRect.bottom = bottom
            }
        }

        if (parent.getChildLayoutPosition(view) % 2 == 0) {
            outRect.left = 15.dp
            outRect.right = 5.dp
        } else {
            outRect.left = 5.dp
            outRect.right = 15.dp
        }

    }

}