package com.kdanmobile.reader.screen.view

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.kdanmobile.kmpdfkit.pdfcommon.Bookmark
import com.kdanmobile.reader.R
import com.kdanmobile.reader.screen.adapter.BookmarkAdapter
import com.kdanmobile.reader.screen.handler.BookmarkHandler
import com.kdanmobile.reader.screen.handler.PdfInfoHandler
import com.kdanmobile.reader.screen.handler.ThumbnailHandler
import com.kdanmobile.reader.widget.drag.OnRecyclerItemClickListener
import kotlinx.android.synthetic.main.view_bookmark.view.*
import java.util.*

class BookmarkView: RelativeLayout, LeftToolbarView {
    private var adapter: BookmarkAdapter? = null
    private var pdfInfoHandler: PdfInfoHandler? = null
    private var bookmarkHandler: BookmarkHandler? = null

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
        initView()
    }

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
        initView()
    }

    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.view_bookmark, this)
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL

        id_km_bookmark_recycler.setHasFixedSize(true)
        id_km_bookmark_recycler.layoutManager = layoutManager
        id_km_bookmark_recycler.layoutManager = LinearLayoutManager(context)
        id_km_bookmark_recycler.addOnItemTouchListener(object : OnRecyclerItemClickListener(id_km_bookmark_recycler) {
            override fun onItemClick(viewHolder: RecyclerView.ViewHolder) {
                onScrollToPosition(viewHolder.adapterPosition, true)
                val bookmark = adapter?.getItem(viewHolder.adapterPosition)
                if (null != bookmark) {
                    pdfInfoHandler?.setCurrentPage(bookmark.pageNum)
                }
            }

            override fun onItemLongClick(viewHolder: RecyclerView.ViewHolder) {

            }
        })
    }

    fun setHandlers(pdfInfoHandler: PdfInfoHandler, thumbnailHandler: ThumbnailHandler, bookmarkHandler: BookmarkHandler) {
        this.pdfInfoHandler = pdfInfoHandler
        this.bookmarkHandler = bookmarkHandler
        adapter = BookmarkAdapter(pdfInfoHandler, thumbnailHandler, arrayOf())
        id_km_bookmark_recycler.adapter = adapter
        updateContent()
        onScrollToContent(pdfInfoHandler.getCurrentPage(), true)
    }

    override fun updateContent() {
        onRefreshBookmarkData(bookmarkHandler?.getBookmarks() ?: arrayOf())
    }

    override fun onScrollToContent(pageIndex: Int, notify: Boolean) {
        adapter?.setCurrentPage(pageIndex)
        val bookmarkIndex = adapter?.bookmarkIndex

        if (null != bookmarkIndex) {
            onScrollToPosition(bookmarkIndex, notify)
        }
    }

    private fun onRefreshBookmarkData(bookmarks: Array<Bookmark>) {
        try {
            if (bookmarks.isNotEmpty()) {
                Arrays.sort(bookmarks) { lhs, rhs -> if (lhs.pageNum > rhs.pageNum) 1 else -1 }
            }
            adapter?.setBookmarks(bookmarks)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun onScrollToPosition(position: Int, notify: Boolean) {
        if (notify) {
            adapter?.setBookIndex(position)
        }

        if (null != id_km_bookmark_recycler) {
            id_km_bookmark_recycler.scrollToPosition(position)
        }
    }
}