package com.casic.br.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.casic.br.R
import com.casic.br.model.AddedDeviceModel
import com.pengxh.kt.lite.extensions.dp2px

class AddedDeviceAdapter(
    private val context: Context,
    private val dataRows: MutableList<AddedDeviceModel>
) : RecyclerView.Adapter<AddedDeviceAdapter.ItemViewHolder>() {

    private var layoutInflater: LayoutInflater = LayoutInflater.from(context)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        return ItemViewHolder(
            layoutInflater.inflate(R.layout.item_added_device_rv_g, parent, false)
        )
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        if (position == itemCount - 1) {
            holder.deviceNameView.text = "添加设备"
            holder.moreOperateButton.visibility = View.GONE

            holder.imageView.setImageResource(R.drawable.ic_add_device)
            if (holder.imageView.layoutParams is ViewGroup.MarginLayoutParams) {
                val params = holder.imageView.layoutParams as ViewGroup.MarginLayoutParams
                params.setMargins(0, 3f.dp2px(context), 0, 0)
                holder.imageView.requestLayout()
            }
            holder.imageView.layoutParams.width = 40f.dp2px(context)
            holder.imageView.layoutParams.height = 40f.dp2px(context)

            holder.tempLayout.visibility = View.GONE
            holder.imageView.setOnClickListener { //添加图片
                clickListener?.onAddDeviceClick()
            }
        } else {
            holder.deviceNameView.text = dataRows[position].deviceName
            Glide.with(context)
                .load(dataRows[position].deviceImage)
                .apply(RequestOptions.circleCropTransform())
                .placeholder(R.mipmap.load_image_error)
                .into(holder.imageView)
            holder.deviceModelView.text = dataRows[position].deviceModel
            holder.deviceUsedTimeView.text = dataRows[position].deviceUsedTime
            holder.rootView.setOnClickListener {
                clickListener?.onItemClick(position)
            }
            // 长按监听
            holder.moreOperateButton.setOnClickListener {
                clickListener?.onButtonClick(position)
            }
        }
    }

    override fun getItemCount(): Int = dataRows.size + 1

    private var clickListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onAddDeviceClick()

        fun onItemClick(position: Int)

        fun onButtonClick(position: Int)
    }

    fun setOnItemClickListener(clickListener: OnItemClickListener?) {
        this.clickListener = clickListener
    }

    class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val rootView: LinearLayout = itemView.findViewById(R.id.rootView)
        val deviceNameView: TextView = itemView.findViewById(R.id.deviceNameView)
        val moreOperateButton: Button = itemView.findViewById(R.id.moreOperateButton)
        val imageView: ImageView = itemView.findViewById(R.id.imageView)
        val tempLayout: LinearLayout = itemView.findViewById(R.id.tempLayout)
        val deviceModelView: TextView = itemView.findViewById(R.id.deviceModelView)
        val deviceUsedTimeView: TextView = itemView.findViewById(R.id.deviceUsedTimeView)
    }
}