package com.zxhl.main.page.adapter

import android.content.Context
import android.util.Log
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.zxhl.cms.AppContext
import com.zxhl.cms.R
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.utils.OnRecycleItemClickListener

import java.util.*

class PublishPhotoAdapter : BaseRecyclerAdapter<String, PublishPhotoAdapter.ViewHolder> ,
    ItemTouchHelperAdapter {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<String>
    private val moveListener:PhotoMoveListener

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<String>,
        moveListener:PhotoMoveListener
    ) : super() {
        mContext = content
        this.listener = listener
        this.moveListener=moveListener
    }


    override fun getItemCount(): Int {
        val size = mList.size
        if (size < 6) {
            return size + 1
        } else {
            return size
        }


    }
    private val mImageViews: SparseArray<ImageView?>? = null

    fun getImageViews(): SparseArray<ImageView?>? {
        return mImageViews
    }

    override fun getItem(position: Int): String {
        return super.getItem(position)
    }

//    override fun getItem(position: Int): LocalMedia {
//        return super.getItem(position)
//    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        if (position < list.size) {
            var str = mList[position]
            Glide.with(AppContext.mAppContext).load(str)
                .into(holder.item_img_show!!)
                holder.item_img_del?.visibility = View.VISIBLE
            // holder?.item_img_show?.setLoadImageUrl(str, false)
           // holder?.item_img_show?.setLoadImageUrl(Uri.parse(bean.compressPath))

            holder.item_img_add?.visibility = View.GONE
        } else {
            holder.item_img_show?.setImageResource(R.color.color_ededed)
            holder.item_img_del?.visibility = View.GONE
            holder.item_img_add?.visibility = View.VISIBLE
        }


    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        return ViewHolder(
            LayoutInflater.from(AppContext.get())
                .inflate(R.layout.item_layout_photo, parent, false)
        )
    }

    inner class ViewHolder : RecyclerView.ViewHolder {

        var item_img_add: ImageView?
        var item_img_show: ImageView?
        var item_img_del: ImageView?

        constructor(
            itemView: View
        ) : super(itemView) {

            item_img_add = itemView.findViewById<ImageView>(R.id.id_img_add)
            item_img_show = itemView.findViewById<ImageView>(R.id.id_img_show)
            item_img_del = itemView.findViewById<ImageView>(R.id.id_img_del)
            item_img_show?.setOnClickListener {
                val pos = adapterPosition
                if (pos < mList.size) {
                    listener.onItemClick(it, position, null)
                }
            }
            item_img_add?.setOnClickListener {
                val pos = adapterPosition
                if (pos < mList.size) {

                } else {
                    listener.onItemClick(it, position, null)
                }

            }
            item_img_del?.setOnClickListener {
                val pos = adapterPosition
                if (pos < mList.size) {
                    listener.onItemClick(it, position, null)
                } else {

                }

            }
        }
    }

    override fun onItemMove(source: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?) {
        var fromPosition=source?.adapterPosition
        var toPosition=target?.adapterPosition
        if (fromPosition != null) {
            if (toPosition != null) {
                if(fromPosition<mList.size&&toPosition<mList.size){
                    //交换数据位置
                    Collections.swap(mList, fromPosition, toPosition)
                    //刷新位置交换
                    notifyItemMoved(fromPosition, toPosition)
                    onItemClear(source)
                    moveListener.moveFinish()
                }
            }
        }
    }

    override fun onItemDissmiss(source: RecyclerView.ViewHolder?) {
        var position = source?.adapterPosition
        mList.remove(position) //移除数据
        if (position != null) {
            notifyItemRemoved(position)
        }//刷新数据移除
    }

    override fun onItemSelect(source: RecyclerView.ViewHolder?) {
        //当拖拽选中时放大选中的view
        source?.itemView?.setScaleX(1.2f)
        source?.itemView?.setScaleY(1.2f)
    }

    override fun onItemClear(source: RecyclerView.ViewHolder?) {
        //拖拽结束后恢复view的状态
        source?.itemView?.setScaleX(1.0f)
        source?.itemView?.setScaleY(1.0f)
    }

}