package com.example.myapplication6;

import android.content.Context;
import androidx.annotation.ArrayRes;
import androidx.annotation.StringRes;

public final class CollectionTypeHelper {

    private CollectionTypeHelper() {}

    @ArrayRes
    public static int labelsArrayForShelf(String shelf) {
        if (bizimdb.SHELF_READING.equals(shelf)) {
            return R.array.reading_type_labels;
        }
        if (bizimdb.SHELF_WISHLIST.equals(shelf)) {
            return R.array.wishlist_type_labels;
        }
        if (bizimdb.SHELF_TRAVEL.equals(shelf)) {
            return R.array.travel_type_labels;
        }
        return R.array.content_type_labels;
    }

    @ArrayRes
    public static int keysArrayForShelf(String shelf) {
        if (bizimdb.SHELF_READING.equals(shelf)) {
            return R.array.reading_type_keys;
        }
        if (bizimdb.SHELF_WISHLIST.equals(shelf)) {
            return R.array.wishlist_type_keys;
        }
        if (bizimdb.SHELF_TRAVEL.equals(shelf)) {
            return R.array.travel_type_keys;
        }
        return R.array.content_type_keys;
    }

    @StringRes
    public static int categoryLabelForShelf(String shelf) {
        if (bizimdb.SHELF_READING.equals(shelf)) {
            return R.string.label_category_reading;
        }
        if (bizimdb.SHELF_WISHLIST.equals(shelf)) {
            return R.string.label_category_wishlist;
        }
        if (bizimdb.SHELF_TRAVEL.equals(shelf)) {
            return R.string.label_category_travel;
        }
        return R.string.label_category_media;
    }

    public static void bindSpinnerForShelf(Context context, android.widget.Spinner spinner, String shelf) {
        android.widget.ArrayAdapter<CharSequence> adapter =
                android.widget.ArrayAdapter.createFromResource(
                        context,
                        labelsArrayForShelf(shelf),
                        android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    public static int indexForStoredTypeKey(Context context, String shelf, String storedRaw) {
        String[] keys = context.getResources().getStringArray(keysArrayForShelf(shelf));
        String norm = bizimdb.normalizeStoredType(storedRaw);
        for (int i = 0; i < keys.length; i++) {
            if (keys[i].equals(norm)) {
                return i;
            }
        }
        return 0;
    }

    public static String typeKeyAtPosition(Context context, String shelf, int position) {
        String[] keys = context.getResources().getStringArray(keysArrayForShelf(shelf));
        if (keys.length == 0) {
            return "website";
        }
        int i = Math.max(0, Math.min(position, keys.length - 1));
        return keys[i];
    }
}
