package com.auto.clicker.autoclicker.ui.adapter.floating;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.room.entity.Solution;

import java.util.List;


public class SolutionAdapter extends RecyclerView.Adapter<SolutionAdapter.SolutionViewHolder> {

    private List<Solution> solutionList;
    private OnSolutionClickListener listener;
    private OnOptionMenuClickListener optionMenuListener;

    // 新增接口定义
    public interface OnOptionMenuClickListener {
        void onRenameClick(Solution solution);

        void onDuplicateClick(Solution solution);

        void onDeleteClick(Solution solution);

        void onExportClick(Solution solution);
    }

    public interface OnSolutionClickListener {
        void onSolutionClick(Solution solution);
    }

    public SolutionAdapter(List<Solution> solutionList, OnSolutionClickListener listener, OnOptionMenuClickListener optionMenuListener) {
        this.solutionList = solutionList;
        this.listener = listener;
        this.optionMenuListener = optionMenuListener;
    }

    // 新增构造函数，用于不需要选项菜单的情况
    public SolutionAdapter(List<Solution> solutionList, OnSolutionClickListener listener) {
        this.solutionList = solutionList;
        this.listener = listener;
        this.optionMenuListener = null; // 设置为 null，表示不需要选项菜单
    }

    @NonNull
    @Override
    public SolutionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_solution, parent, false);
        return new SolutionViewHolder(view, parent.getContext()); // 传递 Context
    }

    @Override
    public void onBindViewHolder(@NonNull SolutionViewHolder holder, int position) {
        Solution solution = solutionList.get(position);
        holder.name.setText(solution.getSolutionName());
        if (solution.getMode() == 1) {
            holder.mode.setText("1-point Mode");
        } else {
            holder.mode.setText("Multi-Point Mode");
        }

        holder.play.setOnClickListener(v -> {
            if (listener != null) {
                listener.onSolutionClick(solution);
            }
        });

        if (optionMenuListener != null) {
            holder.option.setVisibility(View.VISIBLE);
            holder.option.setOnClickListener(v -> {
                holder.showOptionMenu(solution, optionMenuListener);
            });
        } else {
            holder.option.setVisibility(View.GONE);
        }
    }

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

    public void setSolutions(List<Solution> newSolutions) {
        this.solutionList.clear();
        if (newSolutions != null) {
            this.solutionList.addAll(newSolutions);
        }
        notifyDataSetChanged();
    }

    static class SolutionViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView mode;
        ImageView play;
        ImageView option;
        Context context; // 保存 Context

        SolutionViewHolder(View itemView, Context context) {
            super(itemView);
            this.context = context; // 初始化 Context
            name = itemView.findViewById(R.id.name);
            mode = itemView.findViewById(R.id.mode);
            play = itemView.findViewById(R.id.play);
            option = itemView.findViewById(R.id.option);
        }

        private void showOptionMenu(Solution solution, OnOptionMenuClickListener menuListener) {
            View popupView = LayoutInflater.from(context).inflate(R.layout.option_menu_layout, null);
            PopupWindow popupWindow = new PopupWindow(
                    popupView,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    true
            );
            popupWindow.setElevation(8f);
            popupWindow.setBackgroundDrawable(context.getDrawable(android.R.color.transparent));

            TextView tvRename = popupView.findViewById(R.id.tv_rename);
            TextView tvDuplicate = popupView.findViewById(R.id.tv_duplicate);
            TextView tvDelete = popupView.findViewById(R.id.tv_delete);
            TextView tvExport = popupView.findViewById(R.id.tv_export);

            tvRename.setOnClickListener(v -> {
                menuListener.onRenameClick(solution);
                popupWindow.dismiss();
            });

            tvDuplicate.setOnClickListener(v -> {
                menuListener.onDuplicateClick(solution);
                popupWindow.dismiss();
            });

            tvDelete.setOnClickListener(v -> {
                menuListener.onDeleteClick(solution);
                popupWindow.dismiss();
            });

            tvExport.setOnClickListener(v -> {
                menuListener.onExportClick(solution);
                popupWindow.dismiss();
            });

            popupWindow.showAsDropDown(option);
        }
    }
}