package pxds.lmmd.zmwf

import android.view.View
import com.test.basd.supercleanermax.R
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.Locale

object Khko {

    private val aesMap = mutableMapOf<Int, String>()

    fun String.decode() = Rdkj.decrypt(this)
        .replace("\\r", "\r")
        .replace("\\n", "\n")
        .replace("\\'", "'")
        .replace("\\\"", "\"")
        .replace("\\?", "?")
        .replace("&amp;", "&")

    fun Long.toFormatTime(): String {
        return SimpleDateFormat(R.string.ޒހޡގޝ_D.string(), Locale.getDefault()).format(this)
    }

    fun View.setTrackedOnClickListener(action: (view: View) -> Unit) {
        setOnClickListener {
            action(this)

            var view: View? = this
            while (view != null) {
                try {
                    val obj = JSONObject()
                    obj.put(R.string.ސޟޥގޑ_D.string(), resources.getResourceEntryName(view.id))
                    Wspd.event(R.string.ޤޣޘކޚ_D.string(), ext = obj)
                    break
                } catch (_: Exception) {
                    view = view.parent as? View
                }
            }
        }
    }

    fun Number.toFormatSize(count: Int = 1): String {
        var suffix = "B"
        var fSize = this.toDouble()
        if (fSize > 1024) {
            suffix = "KB"
            fSize /= 1024.0
        }
        if (fSize > 1024) {
            suffix = "MB"
            fSize /= 1024.0
        }
        if (fSize > 1024) {
            suffix = "GB"
            fSize /= 1024.0
        }
        return String.format(R.string.ކދބރތ_D.string((count).toString()), fSize, suffix)
    }

    fun Collection<View>.setTrackedOnClickListener(listener: (View) -> Unit) {
        this.forEach {
            it.setTrackedOnClickListener(listener)
        }
    }

    fun Int.string(vararg arg: Any) = try {
        (aesMap[this] ?: Lwfm.context.getString(this).decode()).run {
            aesMap[this@string] = this
            String.format(this, *arg)
        }
    } catch (_: Exception) {
        ""
    }
}