package com.clement.karaokefrog.fsjkdgfhdsghhsgh

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request

fun parseNamign(
    jkdhkjf: String,
): Map<String?, String?> {
    return jkdhkjf.let {
        jkdhkjf.removePrefix("app//")
            .split("/")
            .mapIndexed { kjdshds, sdjhjd ->
                if (kjdshds == 0) {
                    "key" to sdjhjd
                } else {
                    "sub${kjdshds}" to sdjhjd
                }
            }.associate { it }
    } ?: mapOf()
}

fun extractAffiseAffcId(line: String): String? {
    val regex = Regex("affise_affc_id=([^&]+)")
    val matchResult = regex.find(line)

    return matchResult?.groupValues?.get(1)
}

fun deleteRemainingLine(url: String): String {
    val index = url.lastIndexOf('/')
    return if (index != -1) {
        url.substring(0, index + 1)
    } else {
        url
    }
}

var baseUrl = ""
fun generateUrl(params: Map<String?, String?>,baseUrl:String): String {
  //  val baseUrl = "https://trackyourtraff.com/key"
    val queryString = StringBuilder()
    var index = 0
    params.forEach { (key, value,) ->
       /* if (queryString.isNotEmpty()) {
            queryString.append('&')
        }*/++index
            if(key=="key") {
                queryString.append("$value?")
                com.clement.karaokefrog.fsjkdgfhdsghhsgh.baseUrl = baseUrl+queryString.toString()

            }else{
                queryString.append("${key}=$value")
                if(index!=params.size)
                queryString.append('&')

            }

    }

    return "$baseUrl$queryString"
}

suspend fun sendRequestAndGetResponse(url: String): String {
    return withContext(Dispatchers.IO) {
        val client = OkHttpClient()
        val request = Request.Builder()
            .url(url)
            .build()

        val response = client.newCall(request).execute()
        val responseBody = response.body?.string()

        responseBody ?: ""
    }
}










