package com.remax.visualnovel.widget.indicator

import android.content.Context
import android.graphics.Rect
import android.text.TextUtils
import android.view.Gravity
import android.widget.FrameLayout
import androidx.appcompat.widget.AppCompatTextView
import com.remax.visualnovel.R
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.ui.IconFontTextView
import com.remax.visualnovel.widget.uitoken.changeTextStyle
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IMeasurablePagerTitleView

class SecondaryPagerMoreTitleView(context: Context) : FrameLayout(context, null), IMeasurablePagerTitleView {

    private lateinit var titleTextView: IconFontTextView

    init {
        init(context)
    }

    private fun init(context: Context) {
        titleTextView = IconFontTextView(context)
        addView(titleTextView)
        with(titleTextView) {
            setPaddingRelative(16.dp, 0, 16.dp, 0)
            setSingleLine()
            ellipsize = TextUtils.TruncateAt.END
            includeFontPadding = false
            height = 32.dp
            gravity = Gravity.CENTER
            setIconFontDrawable(
                endIconFont = context.getString(R.string.icon_arrow_down_fill),
                iconColorToken = context.getString(R.string.color_txt_primary_normal),
                iconSize = 16, iconPadding = 4
            )
            changeTextStyle {
                textUITextToken = context.getString(R.string.txt_label_m)
                textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
            }
        }
    }

    override fun onSelected(index: Int, totalCount: Int) {

    }

    override fun onDeselected(index: Int, totalCount: Int) {

    }

    override fun onLeave(index: Int, totalCount: Int, leavePercent: Float, leftToRight: Boolean) {}
    override fun onEnter(index: Int, totalCount: Int, enterPercent: Float, leftToRight: Boolean) {}
    override fun getContentLeft(): Int {
        val bound = Rect()
        var longestString = ""
        if (titleTextView.text.toString().contains("\n")) {
            val brokenStrings = titleTextView.text.toString().split("\\n".toRegex()).toTypedArray()
            for (each in brokenStrings) {
                if (each.length > longestString.length) longestString = each
            }
        } else {
            longestString = titleTextView.text.toString()
        }
        titleTextView.paint.getTextBounds(longestString, 0, longestString.length, bound)
        val contentWidth = bound.width()
        return left + width / 2 - contentWidth / 2
    }

    override fun getContentTop(): Int {
        val metrics = titleTextView.paint.fontMetrics
        val contentHeight = metrics.bottom - metrics.top
        return (height / 2 - contentHeight / 2).toInt()
    }

    override fun getContentRight(): Int {
        val bound = Rect()
        var longestString = ""
        if (titleTextView.text.toString().contains("\n")) {
            val brokenStrings = titleTextView.text.toString().split("\\n".toRegex()).toTypedArray()
            for (each in brokenStrings) {
                if (each.length > longestString.length) longestString = each
            }
        } else {
            longestString = titleTextView.text.toString()
        }
        titleTextView.paint.getTextBounds(longestString, 0, longestString.length, bound)
        val contentWidth = bound.width()
        return left + width / 2 + contentWidth / 2
    }

    override fun getContentBottom(): Int {
        val metrics = titleTextView.paint.fontMetrics
        val contentHeight = metrics.bottom - metrics.top
        return (height / 2 + contentHeight / 2).toInt()
    }

    fun getTitleTextView(): AppCompatTextView {
        return titleTextView
    }
}