package com.foxx.androidcast.ui;

/*********************************************************************
 * SettingsSpinnerDropdownStyle.java
 * Created at: Sun 17 May 2026 22:49:49 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class SettingsSpinnerDropdownStyle
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 69 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	895262399af56553532cacac04bef4b10715604ec0521f3d3bda2e1102ea8f33
**********************************************************************/
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils;

import com.foxx.androidcast.R;

/** Dropdown row styling for global settings spinners. */
public final class SettingsSpinnerDropdownStyle {
    private SettingsSpinnerDropdownStyle() {}

    public static void apply(View row, boolean selected, boolean enabled) {
        if (row == null) {
            return;
        }
        TextView tv = row.findViewById(R.id.spinner_dropdown_text);
        if (tv == null && row instanceof TextView) {
            tv = (TextView) row;
        }
        if (tv == null) {
            return;
        }
        FrameLayout root = row.findViewById(R.id.spinner_dropdown_item_root);
        if (root != null) {
            root.setBackgroundResource(selected
                    ? R.drawable.bg_settings_spinner_item_selected
                    : R.drawable.bg_settings_spinner_item);
            root.setAlpha(enabled ? 1f : 0.45f);
        }
        int textColor = ContextCompat.getColor(tv.getContext(), R.color.settings_spinner_text);
        tv.setTextColor(textColor);
        tv.setAlpha(enabled ? 1f : 0.45f);
        tv.setTypeface(null, selected ? Typeface.BOLD : Typeface.NORMAL);
        int shadowColor = ContextCompat.getColor(tv.getContext(), R.color.settings_spinner_text_shadow);
        tv.setShadowLayer(selected ? 0f : 3f, 0f, 2f, shadowColor);
        tv.getPaint().setUnderlineText(false);
        if (selected) {
            int underline = contrastUnderlineColor(textColor);
            tv.setBackgroundResource(underlineBarDrawable(underline));
        } else {
            tv.setBackgroundResource(0);
        }
    }

    private static int contrastUnderlineColor(int textColor) {
        double lum = ColorUtils.calculateLuminance(textColor);
        return lum > 0.55 ? Color.BLACK : Color.WHITE;
    }

    private static int underlineBarDrawable(int color) {
        if (color == Color.BLACK) {
            return R.drawable.bg_spinner_item_underline_dark;
        }
        return R.drawable.bg_spinner_item_underline_light;
    }
}
