/*
 * Copyright (C) 2022 - Florindo Smilari (spherIC)
 */
package ch.spherIC.recurvebowsight.utilities;

import android.app.Application;
import android.util.DisplayMetrics;


/**
 * @author F. Smilari
 */
public class UnitConverter {


    public static Application app;

    public static void setApplication(final Application application) {
        app = application;
    }

    /**
     * Convert dp to pixels in horizontal direction.
     *
     * @param dp
     * @return Pixels
     */
    public static int dpToPxH(final int dp) {
        DisplayMetrics displayMetrics = app.getApplicationContext().getResources().getDisplayMetrics();
        int px = Math.round(dp * displayMetrics.density);
        return px;
    }


    /**
     * Convert pixel to dp in horizontal direction.
     *
     * @param px Pixels
     * @return Dp
     */
    public static int pxToDpH(final int px) {
        DisplayMetrics displayMetrics = app.getApplicationContext().getResources().getDisplayMetrics();
        int dp = Math.round(px / displayMetrics.density);
        return dp;
    }

    /**
     * Convert dp to pixels in vertical direction.
     *
     * @param dp
     * @return Pixels
     */
    public static int dpToPxV(final int dp) {
        DisplayMetrics displayMetrics = app.getApplicationContext().getResources().getDisplayMetrics();
        int px = Math.round(dp * displayMetrics.density);
        return px;
    }


    /**
     * Convert pixel to dp in vertical direction.
     *
     * @param px Pixels
     * @return Dp
     */
    public static int pxToDpV(final int px) {
        DisplayMetrics displayMetrics = app.getApplicationContext().getResources().getDisplayMetrics();
        int dp = Math.round(px / displayMetrics.density);
        return dp;
    }
}
