package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CurrencyListActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private CurrencyAdapter currencyAdapter;
    private CurrencyRepository repository;
    private Set<String> favoriteCurrencies;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_currency_list);

        repository = new CurrencyRepository(this);
        favoriteCurrencies = getFavoriteCurrencies();

        recyclerView = findViewById(R.id.currency_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // Настраиваем кнопку "Назад"
        Button backButton = findViewById(R.id.button_back);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });

        new LoadCurrenciesTask().execute();
    }


    private void loadCurrencies(List<String> currencies) {
        currencyAdapter = new CurrencyAdapter(currencies, favoriteCurrencies, new CurrencyAdapter.OnFavoriteClickListener() {
            @Override
            public void onFavoriteClick(String currency, boolean isFavorite) {
                if (isFavorite) {
                    favoriteCurrencies.add(currency);
                } else {
                    favoriteCurrencies.remove(currency);
                }
                saveFavoriteCurrencies(favoriteCurrencies);
            }
        });
        recyclerView.setAdapter(currencyAdapter);
    }

    private Set<String> getFavoriteCurrencies() {
        SharedPreferences prefs = getSharedPreferences("favorites", MODE_PRIVATE);
        return new HashSet<>(prefs.getStringSet("favorite_currencies", new HashSet<>()));
    }

    private void saveFavoriteCurrencies(Set<String> favorites) {
        SharedPreferences prefs = getSharedPreferences("favorites", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putStringSet("favorite_currencies", favorites);
        editor.apply();
    }

    private class LoadCurrenciesTask extends AsyncTask<Void, Void, List<String>> {
        @Override
        protected List<String> doInBackground(Void... voids) {
            return repository.getAllUniqueCurrencies();
        }

        @Override
        protected void onPostExecute(List<String> currencies) {
            Log.d("LoadCurrenciesTask", "onPostExecute with currencies: " + currencies.toString());
            loadCurrencies(currencies);
        }
    }
}<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_currency_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CurrencyListActivity">

    <!-- Кнопка "Назад" -->
    <Button
        android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Назад"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <!-- RecyclerView, расположенный под кнопкой "Назад" -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/currency_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"

        android:padding="8dp"
        app:layout_constraintTop_toBottomOf="@id/button_back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout> сделай в это активности возможность выбирать не валюты для избранного а создавать избранные пары валют