package com.hanwha.morakmorak.util

import android.content.res.Resources
import android.graphics.Bitmap
import android.opengl.GLES30
import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout


/**
 * Created by aaron on 2018-05-30
 */

class ViewUtils {

    companion object {
        @JvmStatic fun pxToDp(px: Float): Float {
            val densityDpi = Resources.getSystem().displayMetrics.densityDpi.toFloat()
            return px / (densityDpi / 160f)
        }

        @JvmStatic fun dpToPx(dp: Float): Int {
            val density = Resources.getSystem().displayMetrics.density
            return Math.round(dp * density)
        }

        /*@JvmStatic fun changeIconDrawableToGray(context: Context, drawable: Drawable?) =
                drawable?.run { mutate(); setColorFilter(ContextCompat.getColor(context, R.color.dark_gray), PorterDuff.Mode.SRC_ATOP) }*/

        @JvmStatic fun setTabPadding(tab: TabLayout) {
            val viewGroup = tab.getChildAt(0) as ViewGroup
            (0 until viewGroup.childCount)
                    .map { viewGroup.getChildAt(it) }
                    .forEach { it.setPadding(0, 0, 0, 0) }
        }

        @JvmStatic fun resizeBitmap(bitmap: Bitmap): Bitmap {
            if (bitmap.width > GLES30.GL_MAX_TEXTURE_SIZE
                    || bitmap.height > GLES30.GL_MAX_TEXTURE_SIZE) {
                val aspectRatio = bitmap.height as Float / bitmap.width as Float
                val resizedWidth = (GLES30.GL_MAX_TEXTURE_SIZE * 0.9).toInt()
                val resizedHeight = (GLES30.GL_MAX_TEXTURE_SIZE.toDouble() * 0.9 * aspectRatio.toDouble()).toInt()

                return Bitmap.createScaledBitmap(bitmap, resizedWidth, resizedHeight, false)
            }

            return bitmap

        }
    }

}// This class is not publicly instantiable
