package com.soundapp.soundgags.setting

import android.content.Context
import android.media.AudioManager
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import com.soundapp.soundgags.datame.Reflection
import com.soundapp.soundgags.datame.Familiar
import org.json.JSONArray
import org.json.JSONException
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.StringWriter

object SetUtil {
    fun getString(input: InputStream): String {
        try {
            val charArray = CharArray(input.available())
            var count: Int
            val stringWriter = StringWriter()
            val inputStreamReader = InputStreamReader(input)
            val bufferedReader = BufferedReader(inputStreamReader)
            while ((bufferedReader.read(charArray).also { count = it }) != -1) {
                stringWriter.write(charArray, 0, count)
            }
            return stringWriter.toString()
        } catch (exception: IOException) {
            return ""
        }
    }
    fun getFamiliar(str: String?): List<Familiar>? {
        try {
            val data: MutableList<Familiar> = ArrayList()
            val jsonArray = JSONArray(str)

            for (i in 0..<jsonArray.length()) {
                val it = jsonArray.getJSONObject(i)
                val listArray = it.getJSONArray("list")
                val list: MutableList<Reflection> = ArrayList()
                for (k in 0..<listArray.length()) {
                    val listBean = listArray.getJSONObject(k)
                    val reflection = Reflection()
                    reflection.audioUrl=(listBean.getString("mp3Url"))
                    reflection.convert=(listBean.getString("preUrl"))
                    reflection.name=(listBean.getString("title"))
                    list.add(reflection)
                }

                val newBean = Familiar()
                newBean.convert=(it.getString("categoryUrl"))
                newBean.infolist=(list)
                newBean.name=(it.getString("categoryName"))
                data.add(newBean)
            }

            return data
        } catch (jo: JSONException) {
            return null
        }
    }
    fun setVolume(context: Context, seekBar: SeekBar) {
        val audioManager = context.getSystemService(AppCompatActivity.AUDIO_SERVICE) as AudioManager
        val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        seekBar.max = maxVolume
        seekBar.progress = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                // 可以留空或添加相应逻辑
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                // 可以留空或添加相应逻辑
            }
        })
    }
    fun getAudioDuration(context: Context, uri: Uri?): Long {
        val retriever = MediaMetadataRetriever()
        var duration: Long = 0

        try {
            val contentResolver = context.contentResolver
            retriever.setDataSource(context, uri)
            val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
            duration = time!!.toLong()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                retriever.release()
            } catch (e: IOException) {
                throw RuntimeException(e)
            }
        }

        return duration
    }
    fun dpToPx(dp: Int, context: Context): Int {
        return (dp * context.resources.displayMetrics.density).toInt()
    }
}