package com.rdiot.yx485.adapter

import android.content.Context
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.ckr.pageview.adapter.BasePageAdapter
import com.rdiot.yx485.R
import com.rdiot.yx485.bean.SceneBean
import com.rdiot.yx485.util.setClickLimitListener
import kotlin.math.max

/**
 * 场景列表
 */
class ScenesAdapter(
    context: Context?,
    private val space: Int,
    onItemClickListener: OnItemClickListener?
) : BasePageAdapter<SceneBean?, ScenesAdapter.MainHolder?>(context) {

    private var isShowDeleteIcon = false
    private var mOnItemClickListener: OnItemClickListener? = null

    override fun getItemCount(): Int {
        val itemCount = super.getItemCount()
        return if (mIsLooping) {
            if (itemCount == 0) 0 else max(itemCount * 2, MIN_VALUE)
        } else itemCount
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.item_scene
    }

    override fun getViewHolder(itemView: View, viewType: Int): MainHolder {
        return MainHolder(itemView)
    }

    override fun convert(
        holder: MainHolder?,
        position: Int,
        originItem: SceneBean?,
        adjustedPosition: Int,
        item: SceneBean?
    ) {
        holder?.let {
            if (item == null) {
                holder.linearLayout.visibility = View.INVISIBLE
                holder.itemView.setOnLongClickListener(null)
                holder.itemView.setOnClickListener(null)
            } else {

                holder.linearLayout.visibility = View.VISIBLE
                if (position % 3 == 0) {
                    holder.linearLayout.setPadding(space/2, 0, space / 2, 0)
                } else if (position % 3 == 1) {
                    holder.linearLayout.setPadding(space / 2, 0, space / 2, 0)
                } else if (position % 3 == 2) {
                    holder.linearLayout.setPadding(space / 2, 0, space/2, 0)

                }

                holder.textView.text = item.name

                holder.itemView.setClickLimitListener {
                    mOnItemClickListener?.onClick(
                        adjustedPosition,
                        item
                    )
                }
            }
        }

    }

    class MainHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView
        val constraintLayout: ConstraintLayout
        val linearLayout: LinearLayout

        init {
            textView = itemView.findViewById<View>(R.id.tv_name) as TextView
            constraintLayout = itemView.findViewById<View>(R.id.cl_item) as ConstraintLayout
            linearLayout = itemView.findViewById<View>(R.id.ll_item) as LinearLayout
        }
    }

    internal inner class OnItemLongClickListener(position: Int) : View.OnLongClickListener {
        override fun onLongClick(v: View): Boolean {
            isShowDeleteIcon = !isShowDeleteIcon
            notifyDataSetChanged()
            return true
        }
    }

    interface OnItemClickListener {
        fun onClick(position: Int, sceneBean: SceneBean?)
    }

    companion object {
        const val MIN_VALUE = 6
    }

    init {
        mOnItemClickListener = onItemClickListener
    }

}