package com.lemenzo.messenger.ui;

import android.app.Activity;
import android.graphics.Insets;
import android.graphics.Typeface;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;
import com.lemenzo.messenger.R;

public final class Ui {
    private Ui() {}

    public static int dp(View v, int dp) {
        return Math.round(dp * v.getResources().getDisplayMetrics().density);
    }

    public static TextView text(Activity a, String value, float sp, boolean strong) {
        TextView t = new TextView(a);
        t.setText(value);
        t.setTextSize(sp);
        t.setTextColor(a.getColor(R.color.lemenzo_text_primary));
        t.setGravity(Gravity.CENTER_VERTICAL);
        t.setIncludeFontPadding(false);
        t.setLetterSpacing(strong ? -0.015f : 0f);
        if (strong) t.setTypeface(Typeface.create("sans", Typeface.BOLD));
        else t.setTypeface(Typeface.create("sans", Typeface.NORMAL));
        return t;
    }

    public static void center(TextView v) { v.setGravity(Gravity.CENTER); }

    public static ImageButton iconButton(Activity a, int drawable, String description) {
        ImageButton b = new ImageButton(a);
        b.setImageResource(drawable);
        b.setBackgroundColor(0x00000000);
        b.setColorFilter(a.getColor(R.color.lemenzo_text_primary));
        b.setPadding(dp(b, 12), dp(b, 12), dp(b, 12), dp(b, 12));
        b.setContentDescription(description);
        return b;
    }

    public static void prepareEdgeToEdge(Activity a) {
        Window w = a.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            w.setDecorFitsSystemWindows(false);
        }
        w.setStatusBarColor(0x00000000);
        w.setNavigationBarColor(0x00000000);
    }

    /**
     * Insets for normal screens. The screen owns status/navigation bar padding while
     * retaining the app's base spacing.
     */
    public static void applySafeInsets(View root) {
        final int l0=root.getPaddingLeft(), t0=root.getPaddingTop();
        final int r0=root.getPaddingRight(), b0=root.getPaddingBottom();
        root.setOnApplyWindowInsetsListener((v, insets) -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                Insets top = insets.getInsets(WindowInsets.Type.statusBars() | WindowInsets.Type.displayCutout());
                Insets nav = insets.getInsets(WindowInsets.Type.navigationBars());
                v.setPadding(l0+top.left, t0+top.top, r0+top.right, b0+nav.bottom);
            } else {
                v.setPadding(l0+insets.getSystemWindowInsetLeft(), t0+insets.getSystemWindowInsetTop(),
                        r0+insets.getSystemWindowInsetRight(), b0+insets.getSystemWindowInsetBottom());
            }
            return insets;
        });
        root.requestApplyInsets();
    }

    /**
     * IME-aware inset handling for conversation/composer screens. On Android 16
     * edge-to-edge layouts can otherwise be covered by the keyboard even when
     * adjustResize is requested. The whole content receives the greater of the IME
     * or navigation-bar bottom inset, so the composer always remains visible.
     */
    public static void applyImeAwareInsets(Activity a, View root) {
        prepareEdgeToEdge(a);
        a.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        final int l0=root.getPaddingLeft(), t0=root.getPaddingTop();
        final int r0=root.getPaddingRight(), b0=root.getPaddingBottom();
        root.setOnApplyWindowInsetsListener((v, insets) -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                Insets top = insets.getInsets(WindowInsets.Type.statusBars() | WindowInsets.Type.displayCutout());
                Insets nav = insets.getInsets(WindowInsets.Type.navigationBars());
                Insets ime = insets.getInsets(WindowInsets.Type.ime());
                int bottom = Math.max(nav.bottom, ime.bottom);
                v.setPadding(l0+top.left, t0+top.top, r0+top.right, b0+bottom);
            } else {
                v.setPadding(l0+insets.getSystemWindowInsetLeft(), t0+insets.getSystemWindowInsetTop(),
                        r0+insets.getSystemWindowInsetRight(), b0+insets.getSystemWindowInsetBottom());
            }
            return insets;
        });
        root.requestApplyInsets();
    }
}
