package com.cheng.blzb.ui.fragment.bid.publish

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.cheng.blzb.R
import com.cheng.blzb.bean.UploadFileEntity
import com.example.base.utils.DensityUtils
import org.jetbrains.anko.find
import org.jetbrains.anko.sdk27.listeners.onClick

class AddBidImageAdapter(private val context: Context, val data: MutableList<UploadFileEntity>) : RecyclerView.Adapter<AddBidImageAdapter.ViewHolder>() {

    var selectMax = 10
    private val TYPE_ADD_IMAGE = 1001
    private val TYPE_PICTURE = 1002

    companion object {
        val TYPE_ADD = 10001
        val TYPE_RELOAD = 10002
        val TYPE_DELETE = 10003
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ivImg = view.find<ImageView>(R.id.iv_image)
        val ivAddImg = view.find<ImageView>(R.id.iv_add_image)
        val ivDeleteImg = view.find<ImageView>(R.id.iv_delete)
    }

    override fun getItemCount(): Int {
        return if (data.size < selectMax) {
            data.size + 1
        } else {
            data.size
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (isAddItem(position)) {
            TYPE_ADD_IMAGE
        } else {
            TYPE_PICTURE
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeAll() {
        data.clear()
        notifyDataSetChanged()
    }

    /**
     * 删除
     */
    fun remove(position: Int) {
        try {
            if (position != RecyclerView.NO_POSITION && data.size > position) {
                data.removeAt(position)
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, data.size)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 创建ViewHolder
     */
    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.listitem_add_bid_img, viewGroup, false)
        return ViewHolder(view)
    }

    private fun isAddItem(position: Int): Boolean {
        return position == data.size
    }

    /**
     * 设置值
     */
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        if (viewHolder.itemViewType == TYPE_ADD_IMAGE) {
            viewHolder.ivDeleteImg.visibility = View.GONE
            viewHolder.ivAddImg.visibility = View.VISIBLE
        } else {
            viewHolder.ivAddImg.visibility = View.GONE
            viewHolder.ivDeleteImg.visibility = View.VISIBLE
            viewHolder.ivImg.load(data[position].url) {
                transformations(RoundedCornersTransformation(DensityUtils.dp2px(8f).toFloat()))
            }
        }
        viewHolder.ivAddImg.onClick {
            onItemClick?.invoke(TYPE_ADD, position)
        }

        viewHolder.ivDeleteImg.onClick {
            onItemClick?.invoke(TYPE_DELETE, position)
        }
        viewHolder.ivImg.onClick {
            if (viewHolder.itemViewType == TYPE_PICTURE) {
                onItemClick?.invoke(TYPE_RELOAD, position)
            }
        }
    }

    private var onItemClick: ((Int, Int) -> Unit)? = null

    fun setOnItemClick(onClick: ((Int, Int) -> Unit)?) {
        onItemClick = onClick
    }
}