package com.kdanmobile.reader.thumb

import android.graphics.Bitmap
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
import android.support.v7.widget.helper.ItemTouchHelper.Callback.makeMovementFlags
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.pdf_thumb_gv_item.view.*

class PdfThumbAdapter(thumbData: ThumbData, pageMoveHandler: PageMoveHandler): RecyclerView.Adapter<PdfThumbAdapter.ViewHolder>(), ItemMoveSwipeListener {
    var data = thumbData
    var handler = pageMoveHandler

    override fun getItemCount(): Int {
        var size = 0
        data.pages?.let {
            size = it.size
        }
        return size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(data, position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v1 = LayoutInflater.from(parent.context).inflate(R.layout.pdf_thumb_gv_item, parent, false)
        var holder = ViewHolder(v1)
        holder?.setSize(data.viewWidth, data.viewHeight, data.viewPadding, ScreenUtil.getDensity(parent.context))
        return ViewHolder(v1)
    }

    class ViewHolder (view : View) : RecyclerView.ViewHolder(view) {
        fun bind(thumbData: ThumbData, position: Int) {
            var pages = thumbData.pages as List<Bitmap>
            itemView.iv_pdfThumbGvItem_.setImageBitmap(pages[position])
            itemView.tv_pdfThumbGvItem_page.text = (position + 1).toString()
            itemView.cb_pdfThumbGvItem_.isChecked = thumbData.pageSelect[position] as Boolean
            thumbData.hasBookMarks?.let {
                when (it[position]){
                    true -> {
                        itemView.iv_pdfThumbGvItem_bookmark.visibility = View.VISIBLE
                    }
                    false -> {
                        itemView.iv_pdfThumbGvItem_bookmark.visibility = View.GONE
                    }
                }
            }
            when(thumbData.currentPage == position) {
                true -> {
                    itemView.view_pdfThumbGvItem_.visibility = View.VISIBLE
                }
                false -> {
                    itemView.view_pdfThumbGvItem_.visibility = View.GONE
                }
            }
            when(thumbData.isEditMode){
                true -> {
                    itemView.cb_pdfThumbGvItem_.visibility = View.VISIBLE
                }
                false -> {
                    itemView.cb_pdfThumbGvItem_.visibility = View.GONE
                }
            }
        }

        fun setSize(width: Int, height: Int, padding: Int, density: Float) {
            if (width != itemView.rl_pdfThumbGvItem_.getWidth()) {
                val params = LinearLayout.LayoutParams(width, height)
                params.topMargin = (density * 4).toInt()
                params.leftMargin = padding
                params.rightMargin = padding
                itemView.rl_pdfThumbGvItem_.layoutParams = params
                itemView.iv_pdfThumbGvItem_.setPadding(1, 1, 1, 1)
            }
        }
    }

    fun setThumbData(thumbData: ThumbData){
        data = thumbData
        notifyDataSetChanged()
    }

    override fun isLongPressDragEnabled(): Boolean {
        return data.isEditMode
    }
    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        val dragFlags = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT or ItemTouchHelper.UP or ItemTouchHelper.DOWN
        val swipeFlags = 0
        return makeMovementFlags(dragFlags, swipeFlags)
    }
    override fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
        notifyItemMoved(fromPosition, toPosition)
        handler.onPageMove(fromPosition,toPosition)
        return true
    }

    override fun onItemSwipe(position: Int) {
    }

    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        viewHolder.itemView.scaleX = 1.0f
        viewHolder.itemView.scaleY = 1.0f
        notifyDataSetChanged()
    }

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE && null != viewHolder) {
            viewHolder.itemView.scaleX = 0.7f
            viewHolder.itemView.scaleY = 0.7f
        }
    }

    interface PageMoveHandler{
        fun onPageMove(fromPage: Int, toPage: Int)
    }
}