package com.hmg.hmgDr.util

import android.content.Context
import android.util.DisplayMetrics
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import io.flutter.embedding.android.FlutterFragmentActivity
import kotlin.math.ceil

object ViewsUtil {

    /*  return status bar height on basis of device display metrics  */
    fun getStatusBarHeight(context: Context): Int {
        return ceil(
            (25 * context.resources.displayMetrics.density).toDouble()
        ).toInt()
    }

    /**
     * @param context
     * @return the Screen height in DP
     */
    fun getHeightDp(context: Context, isInDp: Boolean = false): Float {
        val displayMetrics: DisplayMetrics = context.resources.displayMetrics
        return if (isInDp) {
            displayMetrics.heightPixels / displayMetrics.density
        } else {
            displayMetrics.heightPixels.toFloat()
        }
    }

    /**
     * @param context
     * @return the screnn width in dp
     */
    fun getWidthDp(context: Context, isInDp: Boolean = false): Float {
        val displayMetrics: DisplayMetrics = context.resources.displayMetrics
        return if (isInDp) {
            displayMetrics.widthPixels / displayMetrics.density
        } else {
            displayMetrics.widthPixels.toFloat()
        }
    }

    // code to hide soft keyboard
    fun hideSoftKeyBoard(context: Context, editBox: EditText?) {
        val imm = context.getSystemService(FlutterFragmentActivity.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(editBox?.windowToken, 0)
    }


    // code to show soft keyboard
    private fun showSoftKeyBoard(context: Context, editBox: EditText?) {
        val inputMethodManager = context.getSystemService(FlutterFragmentActivity.INPUT_METHOD_SERVICE) as InputMethodManager
        editBox?.requestFocus()
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
    }
}