package com.foxx.androidcast.ui;

/*********************************************************************
 * SettingsStringArrayAdapter.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 SettingsStringArrayAdapter
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (4 commits, 80 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	d38f982e34eb051df0f9748ee95bfa1a58e5ab45bf65b9a48573b64b23e2e1fb
**********************************************************************/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

import com.foxx.androidcast.R;

/** Settings spinner adapter: plain row when closed, styled card rows in the dropdown. */
public class SettingsStringArrayAdapter extends ArrayAdapter<String> {
    private final int dropdownLayout;
    protected int selectedPosition;

    public SettingsStringArrayAdapter(Context context, String[] labels) {
        this(context, labels, R.layout.spinner_dropdown_item_settings);
    }

    protected SettingsStringArrayAdapter(Context context, String[] labels, @LayoutRes int dropdownLayout) {
        super(context, R.layout.spinner_item_settings, labels);
        this.dropdownLayout = dropdownLayout;
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public static void wireSelectionRefresh(Spinner spinner, SettingsStringArrayAdapter adapter) {
        spinner.setOnTouchListener((v, event) -> {
            if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                adapter.setSelectedPosition(spinner.getSelectedItemPosition());
                adapter.notifyDataSetChanged();
            }
            return false;
        });
    }

    @Override
    @NonNull
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        // Spinner may recycle dropdown rows (FrameLayout) into the closed row — never cast blindly.
        TextView tv;
        if (convertView instanceof TextView) {
            tv = (TextView) convertView;
        } else {
            tv = (TextView) LayoutInflater.from(getContext())
                    .inflate(R.layout.spinner_item_settings, parent, false);
        }
        tv.setText(getItem(position));
        tv.setAlpha(isEnabled(position) ? 1f : 0.45f);
        return tv;
    }

    @Override
    public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            row = LayoutInflater.from(getContext()).inflate(dropdownLayout, parent, false);
        }
        TextView tv = row.findViewById(R.id.spinner_dropdown_text);
        if (tv != null) {
            tv.setText(getItem(position));
        }
        SettingsSpinnerDropdownStyle.apply(row, position == selectedPosition, isEnabled(position));
        return row;
    }
}
