package com.offline.music.player.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
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.recyclerview.widget.RecyclerView;

import com.offline.music.player.R;
import com.offline.music.player.javabean.A_data.AudioItem;
import com.offline.music.player.max.MaxManager;
import com.offline.music.player.max.onAdAfterAction;
import com.offline.music.player.ui.activity.A_PlayActivity;

import java.util.ArrayList;
import java.util.List;


public class A_ImportFragmentAdapter extends RecyclerView.Adapter<A_ImportFragmentAdapter.ViewHolder> {

    private Activity context;
    private List<AudioItem> audioFiles = new ArrayList<>();
    private AudioItem audioItem;
    private OnOptionClickListener onOptionClickListener;;
    private String newName;

    public A_ImportFragmentAdapter(Activity context, String newName) {
        this.context = context;
        this.newName = newName;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_a_import, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        audioItem = audioFiles.get(position);

        holder.title.setText(audioItem.getName());
        holder.time.setText(audioItem.getDuration());
        Log.d("Adapter", "onBindViewHolder: " + audioItem.getDuration());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaxManager.startShowMaxAd(context, new onAdAfterAction() {
                    @Override
                    public void onAction() {
                        Intent intent = new Intent(context, A_PlayActivity.class);
                        intent.putExtra("Path", audioItem);
                        intent.putExtra("newName",newName);
                        context.startActivity(intent);
                    }
                });
            }
        });

    }

    @Override
    public int getItemCount() {
        return audioFiles.size();
    }

    public void setAudioFiles(List<AudioItem> audioFiles) {
        this.audioFiles = audioFiles;
        notifyDataSetChanged();  // Update UI when the data changes
    }

    // 假设你的 adapter 类中有这个方法来返回某个位置的 AudioItem 对象
    public AudioItem getAudioFile(int position) {
        return audioFiles.get(position); // 返回位置上的 AudioItem 对象
    }


    public void updateTitle(int position, String newTitle) {
        if (position >= 0 && position < audioFiles.size()) {
            audioFiles.get(position).setName(newTitle); // 假设 AudioItem 有一个 setName 方法
            notifyItemChanged(position); // 通知 RecyclerView 更新该项
        }
    }

    // 添加方法获取音频文件列表
    public List<AudioItem> getAudioFiles() {
        return audioFiles;
    }

    public void setOnOptionClickListener(OnOptionClickListener listener) {
        this.onOptionClickListener = listener;
    }

    public interface OnOptionClickListener {
        void onOptionClick(int position, String filePath, View anchorView);
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView option;
        TextView title;
        TextView time;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            option = itemView.findViewById(R.id.options);
            title = itemView.findViewById(R.id.title);
            time = itemView.findViewById(R.id.time);

            option.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && onOptionClickListener != null) {
                    onOptionClickListener.onOptionClick(position, audioItem.getFile(), option);
                }
            });
        }
    }

}


