package com.dumpster.cleaner.ui.photocompression

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.dumpster.cleaner.R
import androidx.core.content.withStyledAttributes

class TripleSeekBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
    }

    private val linePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
    }

    private val selectedLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
    }

    private var radius: Float = 0f
    private var strokeWidth: Float = 0f
    private var defaultColor = 0
    private var selectedColor = 0

    private var segments = listOf(true, true, false) // 默认第一个圆点选中

    private var _selection = MutableLiveData(1)
    val selection: LiveData<Int> = _selection

    init {
        context.withStyledAttributes(attrs, R.styleable.TripleSeekBar) {
            defaultColor = getColor(R.styleable.TripleSeekBar_default_color, 0)
            selectedColor = getColor(R.styleable.TripleSeekBar_selected_color, 0)
            radius = getDimension(R.styleable.TripleSeekBar_width, 0f)
            strokeWidth = getDimension(R.styleable.TripleSeekBar_line_height, 0f)
            linePaint.strokeWidth = strokeWidth
            linePaint.color = defaultColor
            selectedLinePaint.strokeWidth = strokeWidth
            selectedLinePaint.color = selectedColor
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val usableWidth = width - paddingLeft - paddingRight
        val segmentWidth = usableWidth / 3

        segments.forEachIndexed { index, selected ->
            val x = segmentWidth * index + segmentWidth / 2 + paddingLeft
            val y = height / 2f
            // 绘制线段
            if (index < 2) {
                val nextX = segmentWidth * (index + 1) + segmentWidth / 2 + paddingLeft
                val paint = if (segments[index + 1]) selectedLinePaint else linePaint
                canvas.drawLine(x.toFloat(), y, nextX.toFloat(), y, paint)
            }
            paint.color = if (selected) selectedColor else defaultColor
            canvas.drawCircle(x.toFloat(), y, radius, paint)
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val usableWidth = width - paddingLeft - paddingRight
        val segmentWidth = usableWidth / 3
        val x = event.x
        val y = event.y
        val index = ((x - paddingLeft) / segmentWidth).toInt().coerceIn(0, 2)
        when (event.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                if (event.action == MotionEvent.ACTION_DOWN) {
                    _selection.value = index
                }
                segments = List(segments.size) { i -> i <= index }
                invalidate()
            }

            MotionEvent.ACTION_UP -> {
                _selection.value = index
            }
        }
        return true
    }
}