package com.gh.gamecenter.feature.selector

import android.content.Context
import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.ImageUtils

import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.Constants

import com.gh.gamecenter.common.databinding.LocalVideoItemBinding
import com.gh.gamecenter.common.utils.FIFOMap
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.enlargeTouchArea
import com.gh.gamecenter.common.utils.toBinding

import com.zhihu.matisse.internal.entity.Item

class LocalMediaAdapter(
    private val context: Context,
    private val chooseType: String,
    private val isQuickChoose: Boolean,
    private val viewModel: LocalMediaViewModel
) : RecyclerView.Adapter<LocalVideoPreviewViewHolder>() {

    val fifoMap = FIFOMap<String, View>(21)
    var dataList = mutableListOf<Item>()

    fun submitList(updateData: List<Item>?) {
        dataList = updateData as ArrayList<Item>
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = LocalVideoPreviewViewHolder(parent.toBinding())

    override fun onBindViewHolder(holder: LocalVideoPreviewViewHolder, position: Int) {
        val item = dataList[position]
        holder.binding.durationTv.goneIf(chooseType == ChooseType.IMAGE.value)
        ImageUtils.displayResizeMedia(holder.binding.preview, item.contentUri, 200, 200)
        holder.binding.durationTv.text = TimeUtils.formatVideoDuration(item.duration / 1000)

        var selectedResource: Int

        if (isQuickChoose) {
            holder.binding.previewMask.visibility = View.GONE
            holder.binding.chooseCountTv.visibility = View.GONE
            holder.binding.checkImageView.visibility = View.GONE
        }

        if (viewModel.isSelected(item)) {
            if (viewModel.maxSelectionSize == 1) {
                holder.binding.chooseCountTv.visibility = View.GONE
                selectedResource = R.drawable.ic_choose_media_selected
            } else {
                holder.binding.chooseCountTv.visibility = View.VISIBLE
                holder.binding.chooseCountTv.typeface =
                    Typeface.createFromAsset(context.assets, Constants.DIN_FONT_PATH)
                holder.binding.chooseCountTv.text = (viewModel.indexOfSelected(item) + 1).toString()
                selectedResource = R.drawable.background_shape_theme_radius_999
            }
            holder.binding.previewMask.visibility = View.VISIBLE
        } else {
            holder.binding.chooseCountTv.visibility = View.GONE
            holder.binding.previewMask.visibility = View.GONE
            selectedResource = R.drawable.ic_choose_media_normal
        }
        holder.binding.checkImageView.setImageResource(selectedResource)
        holder.binding.checkImageView.enlargeTouchArea(8F.dip2px())
        holder.binding.checkImageView.setOnClickListener {
            if (viewModel.isSelected(item)) {
                viewModel.removeSelection(item)
            } else {
                val addSuccess = viewModel.addSelection(item)
                if (!addSuccess) {
                    if (chooseType == ChooseType.IMAGE.value) {
                        ToastUtils.showToast("至多选择${viewModel.maxSelectionSize}张图片")
                    } else {
                        ToastUtils.showToast("至多选择${viewModel.maxSelectionSize}条视频")
                    }
                }
            }
        }

        if (isQuickChoose) {
            holder.binding.previewMask.visibility = View.GONE
            holder.binding.chooseCountTv.visibility = View.GONE
            holder.binding.checkImageView.visibility = View.GONE

            holder.itemView.setOnClickListener {
                viewModel.setSelection(item)
                viewModel.postSelectedResult()
            }
        } else {
            holder.itemView.setOnClickListener {
                viewModel.previewImage(
                    onlySelected = false,
                    positionInAlbum = position,
                    previewItem = item,
                    urlAndViewMap = fifoMap
                )
            }
        }

        fifoMap.put(item.uri.toString(), holder.binding.preview)
    }

    override fun getItemCount(): Int = dataList.size

}

class LocalVideoPreviewViewHolder(val binding: LocalVideoItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
