package com.base.scanqrclear.luma

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.SimilarBean
import com.base.scanqrclear.databinding.ItemSimilarPhotosBinding
import com.base.scanqrclear.luma.AppHelps.dpToPx
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class SimilarPhotosAdapter(
    val data: List<SimilarBean> = emptyList()
) : BaseAdapter<SimilarBean, ItemSimilarPhotosBinding>(data) {
    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup
    ): ItemSimilarPhotosBinding {
        return ItemSimilarPhotosBinding.inflate(layoutInflater, parent, false)
    }

    override fun bind(
        holder: BaseViewHolder<ItemSimilarPhotosBinding>,
        position: Int,
        item: SimilarBean
    ) {
        val total = item.items.filter { it.isSelect }.sumOf { it.size }
        holder.binding.tvSize.text = Utils.getFormatSize(total)

        val width = holder.itemView.context.resources.displayMetrics.widthPixels
        val margin = holder.itemView.context.dpToPx(45).toInt()
        val imageWidth = (width - margin) / 4
        val largePhotoWidth = imageWidth * 2
        val largePhotoHeight = (largePhotoWidth * 0.92).toInt()
        holder.binding.ivFirstPhoto.layoutParams.width = largePhotoWidth
        holder.binding.ivFirstPhoto.layoutParams.height = largePhotoHeight

        val round = holder.itemView.context.dpToPx(8).toInt()
        val options = RequestOptions().transform(CenterCrop(), RoundedCorners(round))
        Glide.with(holder.itemView.context)
            .load(item.items[0].url)
            .override(largePhotoWidth, largePhotoHeight)
            .apply(options)
            .into(holder.binding.ivFirstPhoto)
        holder.binding.ivSelect.setImageResource(if (item.items[0].isSelect) R.mipmap.icon_xuanzhong_photo_on else R.mipmap.icon_weixuanzhong_photo_off)
        holder.binding.ivFirstPhoto.setOnClickListener {
            item.items[0].isSelect = !item.items[0].isSelect
            holder.binding.ivSelect.setImageResource(if (item.items[0].isSelect) R.mipmap.icon_xuanzhong_photo_on else R.mipmap.icon_weixuanzhong_photo_off)
            setItemClick(item, item.items[0], 0, holder)
        }

        val largePhotos = item.items.take(5)
        val largeAdapter = SimilarPhotosChildAdapter(largePhotos.subList(1, largePhotos.size))
        setItemClick(item, largeAdapter, holder)
        holder.binding.rvLargePhoto.layoutManager = GridLayoutManager(holder.itemView.context, 2, GridLayoutManager.VERTICAL, false)
        holder.binding.rvLargePhoto.adapter = largeAdapter

        val photos = if (item.items.size > 4) item.items.subList(5, item.items.size) else mutableListOf()
        val adapter = SimilarPhotosChildAdapter(photos)
        setItemClick(item, adapter, holder)
        holder.binding.rvPhoto.layoutManager = GridLayoutManager(holder.itemView.context, 4, GridLayoutManager.VERTICAL, false)
        holder.binding.rvPhoto.adapter = adapter
    }

    private fun setItemClick(
        item: SimilarBean,
        adapter: SimilarPhotosChildAdapter,
        holder: BaseViewHolder<ItemSimilarPhotosBinding>
    ) {
        adapter.callback = object : OnClickCallback<SimilarBean> {
            override fun onClicked(view: View, position: Int, data: SimilarBean) {
                setItemClick(item, data, position, holder)
            }
        }
    }

    private fun setItemClick(
        item: SimilarBean,
        data: SimilarBean,
        position: Int,
        holder: BaseViewHolder<ItemSimilarPhotosBinding>
    ) {
        val total = item.items.filter { it.isSelect }.sumOf { it.size }
        holder.binding.tvSize.text = Utils.getFormatSize(total)
        callback?.onClicked(holder.itemView, position, data)
    }
}

