package com.aletheia.app.ui.reader.tts

import android.content.Context
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.SeekBar
import android.widget.TextView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.util.Locale
import kotlin.math.roundToInt

object ReaderSpeechSettingsDialog {
    fun show(context: Context, controller: ReaderSpeechController) {
        val density = context.resources.displayMetrics.density
        val content = LinearLayout(context).apply {
            orientation = LinearLayout.VERTICAL
            val horizontal = (24 * density).roundToInt()
            val vertical = (8 * density).roundToInt()
            setPadding(horizontal, vertical, horizontal, vertical)
        }
        val rateValue = TextView(context)
        val rate = slider(
            context = context,
            label = "Скорость",
            value = controller.rate,
            min = ReaderSpeechController.MIN_RATE,
            max = ReaderSpeechController.MAX_RATE,
            valueView = rateValue,
            parent = content
        )
        val pitchValue = TextView(context)
        val pitch = slider(
            context = context,
            label = "Интонация",
            value = controller.pitch,
            min = ReaderSpeechController.MIN_PITCH,
            max = ReaderSpeechController.MAX_PITCH,
            valueView = pitchValue,
            parent = content
        )

        MaterialAlertDialogBuilder(context)
            .setTitle("Озвучивание книги")
            .setMessage("Голос работает офлайн. Интонация регулирует высоту голоса.")
            .setView(content)
            .setNegativeButton("Отмена", null)
            .setPositiveButton("Сохранить") { _, _ ->
                controller.updateSettings(
                    progressToValue(rate.progress, ReaderSpeechController.MIN_RATE, ReaderSpeechController.MAX_RATE),
                    progressToValue(pitch.progress, ReaderSpeechController.MIN_PITCH, ReaderSpeechController.MAX_PITCH)
                )
            }
            .show()
    }

    private fun slider(
        context: Context,
        label: String,
        value: Float,
        min: Float,
        max: Float,
        valueView: TextView,
        parent: LinearLayout
    ): SeekBar {
        val title = TextView(context).apply {
            text = label
            textSize = 15f
            setPadding(0, 16, 0, 0)
        }
        val seekBar = SeekBar(context).apply {
            this.max = SLIDER_STEPS
            progress = valueToProgress(value, min, max)
            layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        fun render(progress: Int) {
            valueView.text = String.format(
                Locale.getDefault(),
                "%.2f×",
                progressToValue(progress, min, max)
            )
        }
        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) = render(progress)
            override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
            override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
        })
        render(seekBar.progress)
        parent.addView(title)
        parent.addView(valueView)
        parent.addView(seekBar)
        return seekBar
    }

    private fun valueToProgress(value: Float, min: Float, max: Float): Int =
        (((value.coerceIn(min, max) - min) / (max - min)) * SLIDER_STEPS).roundToInt()

    private fun progressToValue(progress: Int, min: Float, max: Float): Float =
        min + (max - min) * progress.coerceIn(0, SLIDER_STEPS) / SLIDER_STEPS

    private const val SLIDER_STEPS = 100
}
