package ch.pizzalemon.android.helper;

import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import ch.pizzalemon.android.R;
import ch.pizzalemon.android.activity.BaseActivity;
import ch.pizzalemon.android.activity.NavMode;

public class DisplayHelper {

    public static int getScreenWidthInPixel(){
        DisplayMetrics displayMetrics = BaseActivity.currentActivity.getResources().getDisplayMetrics();
        return displayMetrics.widthPixels;
    }

    public static int getScreenHeightInPixel(){
        DisplayMetrics displayMetrics = BaseActivity.currentActivity.getResources().getDisplayMetrics();
        return displayMetrics.heightPixels;
    }

    public static int dpToPx(int dp) {
        DisplayMetrics displayMetrics = BaseActivity.currentActivity.getResources().getDisplayMetrics();
        return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }

    public static int pxToDp(int px) {
        DisplayMetrics displayMetrics = BaseActivity.currentActivity.getResources().getDisplayMetrics();
        return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }

    public static NavMode getNavMode() {
        // 1) Settings.Secure (Android 10+ çoğu OEM’de doğru)
        try {
            int s = android.provider.Settings.Secure.getInt(
                    BaseActivity.currentActivity.getContentResolver(), "navigation_mode");
            if (s == 0) return NavMode.THREE_BUTTON;
            if (s == 2) return NavMode.GESTURE;
        } catch (android.provider.Settings.SettingNotFoundException ignore) { }

        // 2) Framework config (cihaz resource üzerinden dene)
        int id = BaseActivity.currentActivity.getResources()
                .getIdentifier("config_navBarInteractionMode", "integer", "android");
        if (id != 0) {
            int mode = BaseActivity.currentActivity.getResources().getInteger(id);
            if (mode == 0) return NavMode.THREE_BUTTON;
            if (mode == 2) return NavMode.GESTURE;
        }

        // 3) Fallback: WindowInsets (bunu view attached olduktan sonra çağır)
        View root = BaseActivity.currentActivity.getWindow().getDecorView();
        WindowInsetsCompat wi = ViewCompat.getRootWindowInsets(root);
        if (wi == null) return NavMode.UNKNOWN;

        Insets nav = wi.getInsets(WindowInsetsCompat.Type.navigationBars());
        Insets gest = wi.getInsets(WindowInsetsCompat.Type.systemGestures());
        // Heuristik: gesture'da nav bar yok ya da çok küçük; systemGestures > 0 olur
        if (nav.bottom > 0 && gest.bottom == 0) return NavMode.THREE_BUTTON;
        if (nav.bottom == 0 && gest.bottom > 0) return NavMode.GESTURE;
        if (nav.bottom > 0 && gest.bottom > 0) {
            // Bazı OEM'lerde küçük nav + gesture birlikte gelebilir
            return (nav.bottom >= gest.bottom) ? NavMode.THREE_BUTTON : NavMode.GESTURE;
        }
        return NavMode.UNKNOWN;
    }

}
