package com.remax.visualnovel.widget.ui.lock

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import com.remax.visualnovel.R
import com.remax.visualnovel.manager.login.LoginManager
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.uitoken.changeBackground
import com.remax.visualnovel.widget.uitoken.changeTextFont
import com.remax.visualnovel.widget.uitoken.view.UITokenTextView

fun String?.getLockLabel() = if (LoginManager.isMyself(this)) LockTagView.PRIVATE_LABEL else LockTagView.PUBLIC_LABEL

/**
 * Created by HJW on 2022/8/31
 *
 */
class LockTagView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    UITokenTextView(context, attrs, defStyleAttr) {

    companion object {
        const val ALL = 1
        const val LTRB = 2

        const val PUBLIC_LABEL = 1
        const val PRIVATE_LABEL = 2
    }

    private var lockType = ALL
    private var lockLabel = PUBLIC_LABEL

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.LockTagView)
        val type = typedArray.getInt(R.styleable.LockTagView_lockTagType, ALL)
        val label = typedArray.getInt(R.styleable.LockTagView_lockTagLabel, PUBLIC_LABEL)
        typedArray.recycle()
        setRadiusType(type)
        setLockLabel(label)

        textSize = 12f
        val padding = 6.dp
        setPadding(padding, padding, padding, padding)
        gravity = Gravity.CENTER
        changeTextFont() {
            textUIColorToken = context.getString(R.string.color_txt_primary_normal)
            onlyIconFont = true
        }
    }

    fun setLockLabel(lockLabel: Int) {
        this.lockLabel = lockLabel
        when (this@LockTagView.lockLabel) {
            PUBLIC_LABEL -> {
                setText(R.string.icon_public)
                changeBackground {
                    backgroundUIColorToken = context.getString(R.string.color_primary_gradient_normal)
                }
            }

            PRIVATE_LABEL -> {
                setText(R.string.icon_private)
                changeBackground {
                    backgroundUIColorToken = context.getString(R.string.color_surface_element_dark_normal)
                }
            }
        }
    }

    private fun setRadiusType(lockType: Int) {
        this.lockType = lockType
        when (this@LockTagView.lockType) {
            ALL -> {
                changeBackground {
                    val radius = context.getString(R.string.radius_xs)
                    radiusToken = radius
                    topRightRadiusToken = radius
                    topLeftRadiusToken = radius
                    bottomRightRadiusToken = radius
                    bottomLeftRadiusToken = radius
                }
            }

            LTRB -> {
                changeBackground {
                    radiusToken = ""
                    val radius = context.getString(R.string.radius_l)
                    bottomRightRadiusToken = radius
                    topLeftRadiusToken = radius
                }
            }
        }
    }

}