package com.base.scanqrclear.luma

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.FileBean
import com.base.scanqrclear.databinding.ItemChildScreenshotCleanBinding
import com.base.scanqrclear.databinding.ItemGroupScreenshotCleanBinding
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
import pokercc.android.expandablerecyclerview.ExpandableAdapter

class ScreenshotCleanAdapter(
    var group: List<String>,
    var child: List<List<FileBean>>
) : ExpandableAdapter<ExpandableAdapter.ViewHolder>() {

    constructor() : this(emptyList(), emptyList())

    private val _selectionData = MutableLiveData<List<FileBean>>()
    val selectionData: LiveData<List<FileBean>> = _selectionData

    fun setData(group: List<String>, child: List<List<FileBean>>) {
        this.group = group
        this.child = child
        notifyDataSetChanged()
    }

    override fun getChildCount(groupPosition: Int): Int {
        if (child.isEmpty() || child.size <= groupPosition) {
            return 0
        }
        return child[groupPosition].size
    }

    override fun getGroupCount(): Int {
        return group.size
    }

    override fun onCreateChildViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(viewGroup.context)
        val view = ItemChildScreenshotCleanBinding.inflate(inflater, viewGroup, false)
        return ChildViewHolder(view)
    }

    override fun onCreateGroupViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(viewGroup.context)
        val view = ItemGroupScreenshotCleanBinding.inflate(inflater, viewGroup, false)
        return GroupViewHolder(view)
    }

    override fun onGroupViewHolderExpandChange(
        holder: ViewHolder,
        groupPosition: Int,
        animDuration: Long,
        expand: Boolean
    ) {
    }

    override fun onBindGroupViewHolder(
        holder: ViewHolder,
        groupPosition: Int,
        expand: Boolean,
        payloads: List<Any>
    ) {
        holder as GroupViewHolder
        holder.binding.tvName.text = group[groupPosition]
        holder.binding.viewBg.visibility = if (groupPosition == 0) View.GONE else View.VISIBLE
        holder.itemView.setOnClickListener { }
    }

    override fun onBindChildViewHolder(
        holder: ViewHolder,
        groupPosition: Int,
        childPosition: Int,
        payloads: List<Any>
    ) {
        holder as ChildViewHolder
        val data = child[groupPosition][childPosition]
        val width = holder.itemView.context.resources.displayMetrics.widthPixels
        var margin = holder.itemView.context.dpToPx(44).toInt()
        val imageWidth = (width - margin) / 3
        holder.binding.ivImage.layoutParams.width = imageWidth
        holder.binding.ivImage.layoutParams.height = imageWidth

        margin = holder.itemView.context.dpToPx(15).toInt()
        if (childPosition % 3 == 0) {
            holder.itemView.setPadding(margin, 0, 0, 0)
        } else if (childPosition % 3 == 2) {
            holder.itemView.setPadding(0, 0, margin, 0)
        } else {
            holder.itemView.setPadding(margin / 2, 0, margin / 2, 0)
        }
        val round = holder.itemView.context.dpToPx(8).toInt()
        val options = RequestOptions().transform(CenterCrop(), RoundedCorners(round))
        Glide.with(holder.itemView.context)
            .load(data.path)
            .apply(options)
            .into(holder.binding.ivImage)
        holder.binding.tvSize.text = Utils.getSize(data.length)
        holder.binding.ivSelect.setImageResource(if (data.isSelected) R.mipmap.ic_check_s else R.mipmap.ic_check_n)
        holder.binding.clContainer.setOnClickListener {
            data.isSelected = !data.isSelected
            holder.binding.ivSelect.setImageResource(if (data.isSelected) R.mipmap.ic_check_s else R.mipmap.ic_check_n)
            _selectionData.value = child.flatten()
        }
    }
}

private class ChildViewHolder(val binding: ItemChildScreenshotCleanBinding) :
    ExpandableAdapter.ViewHolder(binding.root)

private class GroupViewHolder(val binding: ItemGroupScreenshotCleanBinding) :
    ExpandableAdapter.ViewHolder(binding.root)