package com.kdanmobile.reader.screen.reader.mediabox.common

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.SeekBar
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.view_media_box_seekbar.view.*

class MediaBoxSeekBar : ConstraintLayout {

    private var title = ""
    private var unit = ""
    private var maxValue = 100
    private var minValue = 0
    private var initValue = 0

    private val defaultFormatter = object : SeekBarFormatter {
        override fun format(value: Int): String {
            return value.toString()
        }
    }

    var formatter = defaultFormatter
        set(value) {
            field = value
            tv_mediaBoxSeekBar_value?.text = "${formatter.format(currentValue)}$unit"
        }

    interface SeekBarFormatter {
        fun format(value: Int): String
    }

    var currentValue = 0
        set(value) {
            field = Math.max(Math.min(value, maxValue), minValue)
            tv_mediaBoxSeekBar_value?.text = "${formatter.format(currentValue)}$unit"
            seekBar_mediaBoxSeekBar?.progress = field - minValue
        }
    var onSeekBarChangeListener: SeekBar.OnSeekBarChangeListener? = null

    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.MediaBoxSeekBar, 0, 0)
        title = typedArray.getString(R.styleable.MediaBoxSeekBar_title) ?: ""
        unit = typedArray.getString(R.styleable.MediaBoxSeekBar_unit) ?: ""
        maxValue = typedArray.getInteger(R.styleable.MediaBoxSeekBar_maxValue, 100)
        minValue = typedArray.getInteger(R.styleable.MediaBoxSeekBar_minValue, 0)
        if (maxValue < minValue)
            maxValue = minValue
        initValue = typedArray.getInteger(R.styleable.MediaBoxSeekBar_initValue, 0)
        initValue = Math.max(Math.min(initValue, maxValue), minValue)
    }

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

        tv_mediaBoxSeekBar_title.text = title
        seekBar_mediaBoxSeekBar.max = maxValue - minValue
        seekBar_mediaBoxSeekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                currentValue = progress + minValue
                onSeekBarChangeListener?.onProgressChanged(seekBar, currentValue, fromUser)
            }

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

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                onSeekBarChangeListener?.onStopTrackingTouch(seekBar)
            }
        })
        currentValue = initValue
    }
}