package com.kdanmobile.reader.annotationattribute

import android.content.Context
import android.graphics.PorterDuff
import android.os.Build
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.SeekBar
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.view_annotation_attribute_window_seekbar.view.*

class AnnotationAttributeWindowSeekBar : ConstraintLayout {

    var max = 100
        set(value) {
            field = value
            seekBar?.max = max - min
        }
    var min = 0
        set(value) {
            field = value
            seekBar?.max = max - min
        }
    var progress = 0
        set(value) {
            field = Math.max(Math.min(value, max), min)
            seekBar?.progress = field - min
        }

    override fun isEnabled(): Boolean {
        return seekBar?.isEnabled ?: false
    }

    override fun setEnabled(enabled: Boolean) {
        super.setEnabled(enabled)
        seekBar?.isEnabled = enabled

        if (enabled) {
            setSeekBarThumbColor(seekBar, ContextCompat.getColor(context, R.color.reader_annotation_attribute_seek_bar_thumb_tint))
        }
    }

    private var onSeekBarChangeListener: SeekBar.OnSeekBarChangeListener? = null

    fun setOnSeekBarChangeListener(onSeekBarChangeListener: SeekBar.OnSeekBarChangeListener) {
        this.onSeekBarChangeListener = onSeekBarChangeListener
    }

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initAttr(attrs)
        initView()
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        initAttr(attrs)
        initView()
    }

    private fun initAttr(attributeSet: AttributeSet) {
        val typedArray = context.theme.obtainStyledAttributes(attributeSet, R.styleable.AnnotationAttributeWindowSeekBar, 0, 0)
        max = typedArray.getInteger(R.styleable.AnnotationAttributeWindowSeekBar_maxValue, 100)
        min = typedArray.getInteger(R.styleable.AnnotationAttributeWindowSeekBar_minValue, 0)
        if (max < min)
            max = min
        progress = typedArray.getInteger(R.styleable.AnnotationAttributeWindowSeekBar_initValue, 0)
        progress = Math.max(Math.min(progress, max), min)
    }

    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.view_annotation_attribute_window_seekbar, this)

        seekBar.max = max - min
        seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                this@AnnotationAttributeWindowSeekBar.progress = progress + min
                onSeekBarChangeListener?.onProgressChanged(seekBar, this@AnnotationAttributeWindowSeekBar.progress, fromUser)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                onSeekBarChangeListener?.onStartTrackingTouch(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                onSeekBarChangeListener?.onStopTrackingTouch(seekBar)
            }
        })
        this.progress = progress

        setSeekBarThumbColor(seekBar, ContextCompat.getColor(context, R.color.reader_annotation_attribute_seek_bar_thumb_tint))
    }

    private fun setSeekBarThumbColor(seekBar: SeekBar?, color: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            seekBar?.thumb?.setTint(color)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            seekBar?.thumb?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
        }
    }
}