package com.remax.visualnovel.widget.indicator

import android.content.Context
import android.graphics.Rect
import android.widget.FrameLayout
import androidx.appcompat.widget.AppCompatTextView
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.ui.chips.FilterChipView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IMeasurablePagerTitleView

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

    private lateinit var titleTextView: FilterChipView

    init {
        init(context)
    }

    private fun init(context: Context) {
        titleTextView = FilterChipView(context)
        addView(titleTextView)
        setPaddingRelative(0, 0, 12.dp, 0)
    }

    override fun onSelected(index: Int, totalCount: Int) {
        titleTextView.setActive()
    }

    override fun onDeselected(index: Int, totalCount: Int) {
        titleTextView.setInActive()
    }

    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
    }
}