package com.apps.sketchmaster.adapter

import android.content.Context
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.apps.sketchmaster.data.Base
import com.apps.sketchmaster.data.Customize
import com.apps.sketchmaster.event.DeleteClickListener
import com.apps.sketchmaster.utils.AllUtils
import com.apps.sketchmaster.R
import com.apps.sketchmaster.databinding.ItemAllItemBinding

class ThreeTypeAdapter : RecyclerView.Adapter<ThreeTypeAdapter.ThreeViewHolder>() {
    private lateinit var context: Context
    private var imgList: List<String> = arrayListOf()
    private var uploadList: List<Customize> = arrayListOf()
    private var localList: List<Base> = arrayListOf()
    private var type = 0
    private lateinit var listener: DeleteClickListener

    class ThreeViewHolder(val binding: ItemAllItemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ThreeViewHolder {
        context = parent.context
        val binding = ItemAllItemBinding.inflate(LayoutInflater.from(context), parent, false)
        return ThreeViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return when (type) {
            0 -> imgList.size
            1 -> uploadList.size
            2 -> localList.size
            else -> 0
        }
    }

    override fun onBindViewHolder(holder: ThreeViewHolder, position: Int) {
        var path = ""
        when (type) {
            0 -> path = imgList[position]
            1 -> path = uploadList[position].url
            2 -> path = localList[position].url
        }
        println(path)
        holder.binding.image.setOnClickListener {
            listener.onClick(path)
        }
        if(type>0&&position==itemCount-1){
            holder.binding.bottomView.visibility=View.VISIBLE
        }else{
            holder.binding.bottomView.visibility=View.GONE
        }
        if (type == 1) {
            holder.binding.imgDelete.visibility = View.VISIBLE
            holder.binding.imgDelete.setOnClickListener {
                listener.onDelete(path,position)
            }
            Thread {
                val bitmap = BitmapFactory.decodeFile(path)
                holder.binding.image.post {
                    if (bitmap != null) {
                        holder.binding.image.setImageBitmap(bitmap)
                    } else {
                        holder.binding.image.setImageResource(R.mipmap.upload_true)
                    }
                }
            }.start()
            return
        }
        val bitmap = AllUtils.switchTypeFromAssets(context, path)
        if(bitmap==null){
            val bitmap1=BitmapFactory.decodeFile(path)
            holder.binding.image.setImageBitmap(bitmap1)
        }else{
            holder.binding.image.setImageBitmap(bitmap)
        }
        holder.binding.imgDelete.visibility = View.GONE
    }

    fun setListener(listener: DeleteClickListener) {
        this.listener = listener
    }

    fun setCategoryList(list: List<String>, type: Int) {
        this.imgList = list
        this.type = type
    }

    fun setUploadList(list: List<Customize>, type: Int) {
        this.uploadList = list
        this.type = type
    }

    fun setLocalList(list: List<Base>, type: Int) {
        this.localList = list
        this.type = type
    }
}