package com.apps.pixwall.util
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.apps.pixwall.data.Paper
import org.json.JSONArray
import org.json.JSONObject
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.Reader
import java.nio.charset.StandardCharsets

object ConvertUtil {
    fun getConvert(inputStream: InputStream): String {
        var stringStr = ""
        try {
            val stringBuilder: StringBuilder = StringBuilder()
            val buffer = CharArray(8192)
            val reader: Reader =
                BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8))
            var byteRead: Int
            while (reader.read(buffer).also { byteRead = it } != -1) {
                stringBuilder.appendRange(buffer, 0, byteRead)
            }
            stringStr = stringBuilder.toString()
        } catch (e: IOException) {
            return stringStr
        }
        return stringStr
    }

    fun getData(data: String, name: String):List<Paper> {
        val jsonArray = JSONArray(data)
        val paperList = mutableListOf<Paper>()
        var eachItem: JSONObject
        var _description: String

        var links: JSONObject
        var download: String

        var urls: JSONObject
        var regular: String
        var small: String
        var thumb: String

        val host = "https://unsplash.com/photos/"

        var paper: Paper
        var mid: String

        for (i in 0 until jsonArray.length()) {
            eachItem = jsonArray.getJSONObject(i)
            _description = eachItem.getString("alt_description")
            links = eachItem.getJSONObject("links")
            download = links.getString("download")
            urls = eachItem.getJSONObject("urls")
            regular = urls.getString("regular")
            small = urls.getString("small")
            thumb = urls.getString("thumb")
            mid = download.substring(host.length, download.indexOf("/download"))

            paper = Paper().apply {
                imId = mid
                categoryName = name
                description = _description
                fullUrl = download
                previewUrl1080 = regular
                previewUrl400 = small
                previewUrl200 = thumb
            }
            paperList.add(paper)
        }
        return paperList
    }
    fun getSaveFilePath(context: Context, imId: String): String {
        return "${context.cacheDir}/${imId}.jpg"
    }

    fun checkPermission(context: Context,permissions:Array<String>):Boolean{
       return permissions.all {
           ContextCompat.checkSelfPermission(context,it)==PackageManager.PERMISSION_GRANTED
       }
    }
    fun getStoragePermissions(): Array<String> {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            arrayOf(Manifest.permission.READ_MEDIA_IMAGES)
        } else {
            arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        }
    }
    fun hasStoragePermission(context: Context): Boolean {
        return getStoragePermissions().all { permission ->
            ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
        }
    }
}