package com.kdanmobile.reader

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.RectF
import com.kdanmobile.kmpdfkit.manager.KMPDFFactory
import com.kdanmobile.kmpdfkit.manager.controller.KMPDFDocumentController
import com.kdanmobile.kmpdfkit.manager.controller.KMPDFSignatureController
import com.kdanmobile.kmpdfkit.pdfcommon.Bookmark
import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
import com.kdanmobile.kmpdfkit.pdfcommon.TextChar
import com.kdanmobile.kmpdfkit.pdfcommon.TextWord
import com.kdanmobile.reader.screen.handler.*

class ReaderModel {
    private var filename: String? = null
    var password: String = ""
        private set

    var kmpdfFactory: KMPDFFactory? = null
        private set

    @SuppressLint("StaticFieldLeak")
    var kmpdfDocumentController: KMPDFDocumentController? = null
        private set

    var kmpdfSignatureController: KMPDFSignatureController? = null
        private set

    var onPdfChangedListener: OnPdfChangedListener? = null

    @Synchronized
    fun initKMPDFFactory(kmpdfFactory: KMPDFFactory?, filename: String?, password: String = "") {

        this.kmpdfFactory?.run {
            destroy()
        }

        this.kmpdfFactory = kmpdfFactory
        this.filename = filename
        this.password = password
    }

    fun initKMPDFDocumentController() {
        kmpdfDocumentController = kmpdfFactory?.getController(KMPDFFactory.ControllerType.DOCUMENT) as KMPDFDocumentController
        kmpdfSignatureController = kmpdfFactory?.getController(KMPDFFactory.ControllerType.SIGNATURE) as KMPDFSignatureController
    }

    @Synchronized
    fun destroy() {
        filename = null
        password = ""
        kmpdfFactory?.onDestroy()
        kmpdfFactory = null
        kmpdfDocumentController = null
        kmpdfSignatureController = null
        onPdfChangedListener = null
    }

    val pdfInfoHandler = object : PdfInfoHandler {
        override fun getOpenPdfFilename(): String {
            return kmpdfFactory?.fileName ?: ""
        }

        override fun getPdfPageCount(isNativeRefresh: Boolean): Int {
            return kmpdfDocumentController?.getDocumentPageCount(isNativeRefresh) ?: 0
        }

        override fun getCurrentPage(): Int {
            return kmpdfDocumentController?.currentPageNum ?: 0
        }

        override fun goToCurrentPage(page: Int): Boolean {
            return kmpdfDocumentController?.gotoPage(page) ?: false
        }

        override fun getTextLines(page: Int): Array<Array<TextWord>> {
            return kmpdfDocumentController?.textLines(page) ?: arrayOf()
        }

        override fun getPageChars(page: Int): Array<Array<Array<Array<TextChar>>>> {
            return kmpdfDocumentController?.getPageChars(page) ?: arrayOf()
        }
    }

    val thumbnailHandler = object : ThumbnailHandler {
        override fun getPdfBitmap(position: Int, picWidth: Int, mode: Int, isDrawAnnot: Boolean): Bitmap {
            return kmpdfDocumentController?.covertPDFToBitmap(position, picWidth, mode, isDrawAnnot) ?: Bitmap.createBitmap(0, 0, Bitmap.Config.ARGB_4444)
        }
    }

    val bookmarkHandler = object : BookmarkHandler {
        override fun getBookmarks(): Array<Bookmark> {
            return kmpdfDocumentController?.bookmarks ?: arrayOf()
        }
    }

    val outlineHandler = object : OutlineHandler {
        override fun getOutline(): Array<OutlineItem> {
            return kmpdfDocumentController?.outline ?: arrayOf()
        }
    }

    val searchHandler = object : SearchHandler {
        override fun searchPage(page: Int, keyword: String): Array<RectF> {
            return kmpdfDocumentController?.searchPage(page, keyword) ?: arrayOf()
        }

        override fun cleanPreviousSearchResults(keyword: String): Boolean {
            return kmpdfDocumentController?.cleanPreviousSearchResults(keyword) ?: false
        }

        override fun setSearchResult(page: Int, keyword: String, rectArray: Array<RectF>): Boolean {
            return kmpdfDocumentController?.setSearchResult(keyword, page, rectArray) ?: false
        }

        override fun stopSearchKeyWord(): Boolean {
            return kmpdfDocumentController?.stopSearchKeyWord() ?: false
        }
    }

    val operateHandler = object : OperateHandler {
        override fun rotatePages(pages: IntArray, rotateDirection: KMPDFDocumentController.RotationalDirection): Boolean {
            return kmpdfDocumentController?.rotatePages(pages, rotateDirection) ?: false
        }

        override fun deletePages(pages: IntArray): Boolean {
            return kmpdfDocumentController?.deletePages(pages) ?: false
        }
        override fun save(): Boolean {
            return kmpdfDocumentController?.save() ?: false
        }

        override fun reorderPage(fromPage: Int, toPage: Int): Boolean {
            return kmpdfDocumentController?.reorderPages(fromPage,toPage) ?: false
        }

        override fun splitPDFWithPages(path: String, selectPage: IntArray): Boolean {
            return kmpdfDocumentController?.splitPDFWithPages(path, selectPage) ?: false
        }
    }

    val passwordHandler = object : PasswordHandler {
        override fun isNeedPassword(): Boolean {
            return kmpdfFactory?.needPassWord() ?: false
        }
    }
}