package com.kdanmobile.reader.screen.reader.mediabox.stamp

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.view_media_box_item_stamp_custom_viewholder.view.*

class CustomStampAdapter(private val context: Context, private val stampTypes: Array<CustomStampType>): RecyclerView.Adapter<CustomStampViewHolder>() {

    private var selectedIndex = 0
    private var onStampClickListener: OnStampClickListener? = null

    override fun onBindViewHolder(holder: CustomStampViewHolder, position: Int) {
        val stampType = stampTypes[position]
//            val stampTextView = holder.kmpdfStampTextView
//            stampTextView.content = "a"
//            stampTextView.shape = stampType.shape
//            stampTextView.bgColor = stampType.backgroundColor
//            stampTextView.textColor = stampType.textColor
//            stampTextView.lineColor = stampType.lineColor
//            stampTextView.lineWidth = 2f
        holder.view.setOnClickListener {
            if (position != selectedIndex) {
                val oldSelectedIndex = selectedIndex
                selectedIndex = position
                notifyItemChanged(selectedIndex)
                notifyItemChanged(oldSelectedIndex)
                onStampClickListener?.onStampClick(selectedIndex)
            }
        }
        holder.ivStamp.setImageResource(stampType.resourceId)
        holder.ivSelectedBorder.visibility = when (position == selectedIndex) {
            true -> View.VISIBLE
            false -> View.INVISIBLE
        }
        holder.ivSelectedArrow.visibility = when (position == selectedIndex) {
            true -> View.VISIBLE
            false -> View.INVISIBLE
        }
    }

    override fun getItemCount(): Int {
        return stampTypes.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomStampViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.view_media_box_item_stamp_custom_viewholder, null)
//            val view = ImageView(context)
        return CustomStampViewHolder(view)
    }

    fun setOnStampClickListener(onStampClickListener: OnStampClickListener?) {
        this.onStampClickListener = onStampClickListener
    }
}

class CustomStampViewHolder(val view: View): RecyclerView.ViewHolder(view) {
//        val kmpdfStampTextView: KMPDFStampTextView = view.findViewById(R.id.kmpdfStampTextView)
    val ivStamp: ImageView = view.iv_stamp
    val ivSelectedBorder: View = view.iv_selectedBorder
    val ivSelectedArrow: ImageView = view.iv_selectedArrow
}