package com.photo.keyboard.themes.manager

import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.photo.keyboard.themes.App
import java.io.File

object Utils {

    fun dpToUnitPx(dp: Float): Int {
        val scale = App.instance.resources.displayMetrics.density
        return (dp * scale + 0.5f).toInt()
    }

    fun spToUnitPx(sp: Float, con: Context): Float {
        val scale = con.resources.displayMetrics.scaledDensity
        return sp * scale
    }

    fun getStatus(draw: Drawable, drawPress: Drawable): StateListDrawable {
        return StateListDrawable().apply {
            addState(intArrayOf(android.R.attr.state_pressed), drawPress)
            addState(intArrayOf(), draw)

        }

    }

    private val sp: SharedPreferences = App.instance.getSharedPreferences(
        ConstValue.SHARE_NAME,
        Context.MODE_PRIVATE
    )

    private val edit = sp.edit()


    fun putCurPath(path: String) {
        edit.run {
            putString(ConstValue.KEY_ALL_PATH, path)
            apply()
        }

    }

    fun getCurPath(): String? {
        return sp.getString(ConstValue.KEY_ALL_PATH, "")
    }

    fun putAllThemePath(title: String, path: String) {
        edit.run {
            putString(title, path)
            apply()
        }

    }

    fun getAllThemePath(zip: String): String? {
        return sp.getString(zip, "")
    }


    fun initFull(activity: Activity, light: Boolean) {
        val window = activity.window
        val decorView = window.decorView
        val rootView = decorView.rootView
        if (light) {
        } else {
            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }


    fun setFont(text: TextView) {
        text.typeface = App.font
    }

     fun getbgic(con: Context, filePath: String): Drawable? {
        if (!File(filePath).exists()) {
            return null
        }
        return BitmapDrawable(con.resources, BitmapFactory.decodeFile(filePath))
    }


}