package com.gh.gamecenter.qa.editor

import android.content.Context
import android.database.Cursor
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.common.util.*
import com.gh.gamecenter.R
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.toDrawable
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.databinding.LocalVideoItemBinding
import com.zhihu.matisse.internal.entity.Item
import com.zhihu.matisse.internal.ui.adapter.RecyclerViewCursorAdapter
import com.zhihu.matisse.internal.utils.PathUtils

class LocalMediaAdapter(
    val context: Context,
    val mChooseType: String,
    val maxChooseSize: Int,
    val entrance: String,
    val callback: (ArrayList<Item>) -> Unit
) : RecyclerViewCursorAdapter<LocalVideoPreviewViewHolder>(null) {
    private val mSelectedMediaList = arrayListOf<Item>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocalVideoPreviewViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.local_video_item, parent, false)
        return LocalVideoPreviewViewHolder(LocalVideoItemBinding.bind(view))
    }

    override fun onBindViewHolder(holder: LocalVideoPreviewViewHolder, cursor: Cursor?, position: Int) {
        val item = Item.valueOf(cursor)
        holder.binding.durationTv.goneIf(mChooseType == LocalMediaActivity.ChooseType.IMAGE.value)
        val path = "file:///${PathUtils.getPath(context, item.contentUri)}"
        ImageUtils.displayResizeMedia(holder.binding.preview, path, 200, 200)
        holder.binding.durationTv.text = TimeUtils.formatVideoDuration(item.duration / 1000)
        val drawable = if (mSelectedMediaList.contains(item)) {
            if (maxChooseSize == 1) {
                R.drawable.ic_choose_media_selected.toDrawable()
            } else {
                R.drawable.ic_choose_media_bg.toDrawable()
            }
        } else {
            R.drawable.ic_choose_media_normal.toDrawable()
        }
        holder.binding.checkImageView.setImageDrawable(drawable)
        if (mSelectedMediaList.contains(item) && maxChooseSize > 1) {
            holder.binding.chooseCountTv.visibility = View.VISIBLE
            holder.binding.chooseCountTv.text = (mSelectedMediaList.indexOf(item) + 1).toString()
        } else {
            holder.binding.chooseCountTv.visibility = View.GONE
        }
        holder.itemView.setOnClickListener {
            if (mSelectedMediaList.contains(item)) {
                mSelectedMediaList.remove(item)
                notifyDataSetChanged()
                callback.invoke(mSelectedMediaList)
            } else {
                if (maxChooseSize == 1) {
                    mSelectedMediaList.clear()
                }
                if (mSelectedMediaList.size < maxChooseSize) {
                    mSelectedMediaList.add(item)
                    notifyDataSetChanged()
                    callback.invoke(mSelectedMediaList)
                } else {
                    if (mChooseType == LocalMediaActivity.ChooseType.IMAGE.value) {
                        ToastUtils.showToast("至多选择${maxChooseSize}张图片")
                    } else {
                        ToastUtils.showToast("至多选择${maxChooseSize}条视频")
                    }
                }
            }
            if (entrance == "发帖子" || entrance == "发提问帖" || entrance == "发视频帖") {
                val publishContentType = if (entrance == "发帖子") "帖子" else if (entrance == "发提问帖") "提问帖" else "视频帖"
                val publishMediaType = if (mChooseType == LocalMediaActivity.ChooseType.IMAGE.value) "图片" else "视频"
                NewLogUtils.logChooseMedia("click_radio_button", publishContentType, publishMediaType)
            }
        }
    }

    override fun getItemViewType(position: Int, cursor: Cursor?): Int {
        return 0
    }

    fun getSelectedMediaList(): ArrayList<Item> {
        return mSelectedMediaList
    }
}

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