package com.adealink.weparty.commonui.widget.pinedittext

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import com.adealink.frame.util.DisplayUtil
import com.adealink.weparty.R
import com.adealink.weparty.commonui.ext.dpf

/**
 * 下划线样式的输入框
 */
class UnderLinePinField : PinField {


    private var cornerRadius = 0f
        set(value) {
            field = value
            invalidate()
        }

    private val cursorPadding = DisplayUtil.dp2px(15f)


    constructor(context: Context) : super(context)

    constructor(context: Context, attr: AttributeSet) : super(context, attr) {
        initParams(attr)
    }

    constructor(context: Context, attr: AttributeSet, defStyle: Int) : super(
        context,
        attr,
        defStyle
    ) {
        initParams(attr)
    }


    private fun initParams(attr: AttributeSet) {
        val a = context.theme.obtainStyledAttributes(attr, R.styleable.SquarePinField, 0, 0)
        try {
            cornerRadius = a.getDimension(R.styleable.SquarePinField_cornerRadius, cornerRadius)
        } finally {
            a.recycle()
        }

        fieldBgPaint.strokeWidth = 2.dpf()//设置线条粗细
    }

    override fun onDraw(canvas: Canvas) {

        for (i in 0 until numberOfFields) {

            val x1 = (i * singleFieldWidth)
            val padding =
                (if (distanceInBetween != DEFAULT_DISTANCE_IN_BETWEEN) distanceInBetween else getDefaultDistanceInBetween()) / 2
            val paddedX1 = (x1 + padding)
            val paddedX2 = ((x1 + singleFieldWidth) - padding)
            val squareHeight = paddedX2 - paddedX1
            val paddedY1 = (height / 2) - (squareHeight / 2)
            val paddedY2 = (height / 2) + (squareHeight / 2)
            val textX = ((paddedX2 - paddedX1) / 2) + paddedX1
            val textY =
                ((paddedY2 - paddedY1) / 2 + paddedY1) + lineThickness + (textPaint.textSize / 4)
            val character: Char? = getCharAt(i)

            drawRect(canvas, paddedX1, paddedY1, paddedX2, paddedY2, fieldBgPaint)

//            if (highlightAllFields() && hasFocus()) {
//                drawRect(canvas, paddedX1, paddedY1, paddedX2, paddedY2, highlightPaint)
//            } else {
//                drawRect(canvas, paddedX1, paddedY1, paddedX2, paddedY2, fieldPaint)
//            }

            if (character != null) {
                canvas.drawText(character.toString(), textX, textY, textPaint)
            }

            if (shouldDrawHint()) {
                val hintChar = hint.getOrNull(i)
                if (hintChar != null) {
                    canvas.drawText(hintChar.toString(), textX, textY, hintPaint)
                }
            }

            if (hasFocus() && i == text?.length ?: 0) {
                if (isCursorEnabled) {
                    val cursorPadding = cursorPadding
                    val cursorY1 = (paddedY1 + paddedY2) / 2 - cursorPadding/2
                    val cursorY2 = (paddedY1 + paddedY2) / 2 + cursorPadding/2
                    drawCursor(canvas, textX, cursorY1, cursorY2, highlightPaint)
                }
            }
//            highlightLogic(i, text?.length) {
//                drawRect(canvas, paddedX1, paddedY1, paddedX2, paddedY2, highlightPaint)
//            }
        }
    }

    private fun drawRect(
        canvas: Canvas?,
        paddedX1: Float,
        paddedY1: Float,
        paddedX2: Float,
        paddedY2: Float,
        paint: Paint,
    ) {
        canvas ?: return
        canvas.drawLine(
            paddedX1,
            paddedY2,
            paddedX2,
            paddedY2,
            paint
        )
    }


}