package com.kdanmobile.reader.test

import android.content.Context
import android.graphics.*
import android.view.View
import android.widget.RelativeLayout
import com.kdanmobile.kmpdfkit.manager.KMPDFFactory
import com.kdanmobile.kmpdfkit.pdfcommon.*
import com.kdanmobile.reader.utils.DensityUtil

class MyPageView(
        context: Context,
        filePickerSupport: FilePicker.FilePickerSupport,
        kmpdfFactory: KMPDFFactory,
        parentSize: Point,
        private val isAdPage: Boolean
) : ModifyKMPDFPageView(context, filePickerSupport, kmpdfFactory, parentSize) {

    companion object {
        const val DEFAULT_AD_HEIGHT = 450
        const val AD_PAGE_NUMBER_ID = -99
    }

    val adRealWidth = DensityUtil.getScreenWidthPx(context)
    var adRealHeight = 0
        private set
    var adHeight = 0
        set(value) {
            field = value
            adRealHeight = field
        }
    private val invisibleAdSize = PointF(adRealWidth.toFloat(), 1f)
    private val invisibleAdRect = RectF(0f, 0f, invisibleAdSize.x, invisibleAdSize.y)
    private var heightScale = -1f

    val layout = RelativeLayout(context).also {
        it.visibility = View.GONE
        addView(it)
    }

    var isAdShowing = false
        set(value) {
            if (!isAdPage) return
            if (field != value) {
                field = value
                val visibility = when (field) {
                    true -> View.VISIBLE
                    false -> View.INVISIBLE
                }
                layout.visibility = visibility
                this.visibility = visibility
                initPageSize = false
            }
        }
    private var initPageSize = false

    init {
        if (isAdPage) {
            mPageNumber = AD_PAGE_NUMBER_ID
            super.setPage(page, invisibleAdSize, invisibleAdRect)
            for (index in 0 until childCount) {
                getChildAt(index).visibility = View.INVISIBLE
            }
            setBackgroundColor(Color.TRANSPARENT)
        }
    }

    override fun openLongClickBlankContextMenu(view: View?) {
        if (isAdPage) return
        super.openLongClickBlankContextMenu(view)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        if (!isAdPage) return
        mPageNumber = AD_PAGE_NUMBER_ID

        if (isAdShowing) {
            layout.layout(-x.toInt(), 0, (adRealWidth - x).toInt(), adRealHeight)
            updateAdHeight()
            for (i in 0 until layout.childCount) {
                layout.getChildAt(i).apply {
                    layout(0, 0, adRealWidth, adRealHeight)
                }
            }
        } else if (!initPageSize) {
            super.setPage(page, invisibleAdSize, invisibleAdRect)
            initPageSize = true
        }
    }

    private fun getPageViewScale(): Float {
        return width / mSize.x.toFloat()
    }

    private fun updateAdHeight() {
        val scale = getPageViewScale()
        if (scale == heightScale) return
        heightScale = scale
        val modifyPageSize = PointF(adRealWidth.toFloat(), adHeight / heightScale)
        val cropPageSize = RectF(0f, 0f, modifyPageSize.x, modifyPageSize.y)
        setPage(page, modifyPageSize, cropPageSize)
        for (index in 0 until childCount) {
            getChildAt(index).also {
                if (it != layout) {
                    it.visibility = View.INVISIBLE
                }
            }
        }
    }
}