package com.sound.prankparty.SecondFragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.sound.prankparty.R;
import com.sound.prankparty.Room.FavoriteSounds;
import com.sound.prankparty.Room.FavoriteSoundsDao;
import com.sound.prankparty.Room.SaveSounds;

import java.util.List;
import java.util.concurrent.Executors;

public class RadioRecyclerViewAdapter extends RecyclerView.Adapter<RadioRecyclerViewAdapter.RadioViewHolder> {

    private List<SaveSounds> saveSoundsList;
    private Context context;
    private Fragment fragment;
    private FavoriteSoundsDao favoriteSoundsDao;

    public RadioRecyclerViewAdapter(Context context, Fragment fragment, FavoriteSoundsDao favoriteSoundsDao) {
        this.context = context;
        this.fragment = fragment;
        this.favoriteSoundsDao = favoriteSoundsDao;
    }

    @NonNull
    @Override
    public RadioViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_radio, parent, false);
        return new RadioViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RadioViewHolder holder, int position) {
        SaveSounds saveSound = saveSoundsList.get(position);
        holder.title.setText(saveSound.getName());
        holder.date.setText(saveSound.getDate());
        holder.time.setText(saveSound.getTime());
        holder.duration.setText(saveSound.getDuration());

        Executors.newSingleThreadExecutor().execute(() -> {
            final boolean[] isFavorite = {checkFavoriteStatus(saveSound)};
            // 更新 UI 需要在主线程中执行
            fragment.requireActivity().runOnUiThread(() -> {
                updateFavoriteIcon(holder, isFavorite[0]);
                holder.favorite.setOnClickListener(v -> {
                    isFavorite[0] = !isFavorite[0];
                    updateFavoriteIcon(holder, isFavorite[0]);
                    updateDatabase(saveSound, isFavorite[0]);
                });
            });
        });
    }

    @Override
    public int getItemCount() {
        return saveSoundsList != null ? saveSoundsList.size() : 0;
    }

    public void updateData(List<SaveSounds> newSaveSoundsList) {
        this.saveSoundsList = newSaveSoundsList;
        notifyDataSetChanged();
    }

    private boolean checkFavoriteStatus(SaveSounds saveSound) {
        FavoriteSounds favoriteSound = favoriteSoundsDao.getByPath(saveSound.getPath());
        return favoriteSound != null;
    }

    private void updateDatabase(SaveSounds saveSound, boolean isFavorite) {
        Executors.newSingleThreadExecutor().execute(() -> {
            if (isFavorite) {
                favoriteSoundsDao.insertSound(new FavoriteSounds(saveSound.getName(), saveSound.getPath(), ""));
            } else {
                favoriteSoundsDao.delete(favoriteSoundsDao.getByPath(saveSound.getPath()));
            }
        });
    }

    private void updateFavoriteIcon(RadioViewHolder holder, boolean isFavorite) {
        holder.favorite.setImageResource(isFavorite ? R.drawable.favorite : R.drawable.unfavorite);
    }

    static class RadioViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        TextView date;
        TextView time;
        TextView duration;
        ImageView favorite;

        public RadioViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            date = itemView.findViewById(R.id.date);
            time = itemView.findViewById(R.id.time);
            duration = itemView.findViewById(R.id.duration);
            favorite = itemView.findViewById(R.id.favorite);
        }
    }
}
