package com.foxx.androidcast.ui;

/*********************************************************************
 * SettingsScoredArrayAdapter.java
 * Created at: Mon 18 May 2026 22:19:39 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class SettingsScoredArrayAdapter
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 77 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	fc7006cce093f741045f7b963b6373343de66a7be565ba0e64bff5eca7abd7be
**********************************************************************/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.foxx.androidcast.R;

/** Settings spinner row with label left and optional priority score right-aligned. */
public class SettingsScoredArrayAdapter extends SettingsStringArrayAdapter {
    /** Hide score column when negative. */
    public static final int NO_SCORE = -1;

    private final int[] scores;

    public SettingsScoredArrayAdapter(Context context, String[] labels, int[] scores) {
        super(context, labels, R.layout.spinner_dropdown_item_scored);
        this.scores = scores != null ? scores : new int[labels.length];
    }

    @Override
    @NonNull
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        View row = convertView;
        if (!isScoredRow(row)) {
            row = LayoutInflater.from(getContext()).inflate(R.layout.spinner_item_scored, parent, false);
        }
        bindRow(row, position);
        row.setAlpha(isEnabled(position) ? 1f : 0.45f);
        return row;
    }

    @Override
    public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
        View row = convertView;
        if (!isScoredRow(row)) {
            row = LayoutInflater.from(getContext())
                    .inflate(R.layout.spinner_dropdown_item_scored, parent, false);
        }
        bindRow(row, position);
        SettingsSpinnerDropdownStyle.apply(row, position == selectedPosition, isEnabled(position));
        return row;
    }

    private void bindRow(View row, int position) {
        TextView label = row.findViewById(R.id.spinner_item_label);
        TextView score = row.findViewById(R.id.spinner_item_score);
        if (label != null) {
            label.setText(getItem(position));
        }
        if (score != null) {
            int value = position >= 0 && position < scores.length ? scores[position] : NO_SCORE;
            if (value >= 0) {
                score.setVisibility(View.VISIBLE);
                score.setText(String.valueOf(value));
            } else {
                score.setVisibility(View.GONE);
            }
        }
    }

    private static boolean isScoredRow(View row) {
        return row != null && row.findViewById(R.id.spinner_item_label) != null;
    }

}
