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.changeBackground
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.changeTextFont
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IMeasurablePagerTitleView

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

    private lateinit var titleTextView: IconFontTextView
    private var isActive = false

    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
            changeTextFont {
                textUITextToken = context.getString(R.string.txt_label_m)
            }
        }

        setPaddingRelative(0, 0, 12.dp, 0)
    }

    fun setIcon(isLock: Boolean) {
        titleTextView.setIconFontDrawable(
            endIconFont = if (isLock) context.getString(R.string.icon_private) else null,
            iconColorToken = context.getString(R.string.color_txt_primary_normal),
            iconSize = 16, iconPadding = 4
        )
    }

    private fun setActiveStatus(isActive: Boolean) {
        this.isActive = isActive
        with(titleTextView) {
            if (isActive) {
                changeTextColor {
                    textUIColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                    textUIPressedColorToken = context.getString(R.string.color_txt_primary_specialmap_normal)
                }

                changeBackground {
                    radiusToken = context.getString(R.string.radius_pill)

                    strokeUIWidthToken = context.getString(R.string.border_s)

                    backgroundUIColorToken = context.getString(R.string.color_primary_onpic_normal)
                    strokeUIColorToken = context.getString(R.string.color_primary_variant_normal)

                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)
                    strokeUIDisabledColorToken = context.getString(R.string.color_outline_disabled)

                    backgroundUIPressedColorToken = context.getString(R.string.color_primary_onpic_press)
                    strokeUIPressedColorToken = context.getString(R.string.color_primary_variant_normal)
                }

            } else {
                changeTextColor {
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIPressedColorToken = context.getString(R.string.color_txt_primary_normal)
                    textUIDisabledColorToken = context.getString(R.string.color_txt_disabled)
                }

                changeBackground {
                    radiusToken = context.getString(R.string.radius_pill)
                    //无筛选状态将描边恢复默认
                    strokeUIWidthToken = ""

                    backgroundUIColorToken = context.getString(R.string.color_surface_element_normal)

                    backgroundUIDisabledColorToken = context.getString(R.string.color_surface_element_disabled)

                    backgroundUIPressedColorToken = context.getString(R.string.color_surface_element_press)
                }
            }
        }

    }

    override fun onSelected(index: Int, totalCount: Int) {
        setActiveStatus(true)
    }

    override fun onDeselected(index: Int, totalCount: Int) {
        setActiveStatus(false)
    }

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