package com.keyborad.theme.trendyborad.trendyutils

import android.app.Activity
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.provider.Settings
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import com.keyborad.theme.trendyborad.TrendyApp
import com.bumptech.glide.Glide
import com.bumptech.glide.integration.webp.decoder.WebpDrawable
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import java.io.File

object TrendyCommon {

      val transform = RequestOptions().transform(CenterCrop(), RoundedCorners(dpToPx(8f)))
    fun initFullScreen(activity: Activity, dark: Boolean? = true) {
        val window = activity.window
        val decorView = window.decorView
        val rootView = decorView.rootView
//
        if (dark == null) return

        if (dark) {
            decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            )
        } else {
            decorView.setSystemUiVisibility(
                (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            )
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }


    fun loadWepJif(mContext: Context, webpGifUrl: String, view: ImageView) {
        Glide.with(mContext)
            .load(webpGifUrl)
//            .apply(transform)
            .addListener(object : RequestListener<Drawable> {
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable>,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }

            override fun onResourceReady(
                resource: Drawable,
                model: Any,
                target: Target<Drawable>,
                dataSource: DataSource,
                isFirstResource: Boolean
            ): Boolean {
                if (resource is WebpDrawable) {
                    resource.loopCount = WebpDrawable.LOOP_FOREVER
                }
                return false
            }
        }).into(view)
    }


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


    private val systemService =
        TrendyApp.Companion.trendyAppInstance.getSystemService(Context.INPUT_METHOD_SERVICE)
    private val inputMethodManager = systemService as InputMethodManager
    fun checkSetDefault(con: Context): Boolean {
        val defaultId =
            Settings.Secure.getString(con.contentResolver, Settings.Secure.DEFAULT_INPUT_METHOD)
        return defaultId != null && defaultId.startsWith(con.packageName)
    }

    fun checkEnable(con: Context): Boolean {
        for (methodInfo in inputMethodManager.enabledInputMethodList) {
            if (methodInfo.id.startsWith(con.packageName)) {
                return true
            }
        }
        return false
    }

    fun getTextForImeAction(imeOptions: Int): Int {
        return imeOptions and EditorInfo.IME_MASK_ACTION
    }

    fun dpToPx(dpValue: Float): Int {
        val scale = TrendyApp.Companion.trendyAppInstance.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }
}