package com.assimilate.alltrans.adapters;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.assimilate.alltrans.databinding.LayoutItemTranslationBinding;
import com.assimilate.alltrans.mydb.Translations;

import java.util.ArrayList;

public class TranslationAdapter extends RecyclerView.Adapter<TranslationAdapter.TranslationHolder> {
    private final ArrayList<TranslationItem> items;
    private final TranslationItemCallback callback;

    public TranslationAdapter(@NonNull final ArrayList<Translations> list, @NonNull final TranslationItemCallback callback) {
        this.callback = callback;
        this.items = new ArrayList<>();

        if (!list.isEmpty()) {
            for (Translations translations : list) {
                items.add(new TranslationItem(
                        translations.getId(), translations.getSourceLanguage(), translations.getSourceTxt(),
                        translations.getTargetLanguage(), translations.getTargetTxt(), translations.getCurrentTimeMillis(),
                        translations.getExist(), translations.getCollection()
                ));
            }
        }
    }

    @NonNull
    @Override
    public TranslationHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TranslationHolder(LayoutItemTranslationBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TranslationHolder holder, int position) {
        final TranslationItem item = items.get(position);

        holder.mBinding.speakTarget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String speech = item.getTargetTxt();
                if (!TextUtils.isEmpty(speech)) {
                    callback.speech(speech);
                }
            }
        });

        // 为checkboxCurrent设置点击事件
        holder.mBinding.checkboxCurrent.setOnClickListener(v -> {
            boolean isChecked = holder.mBinding.checkboxCurrent.isChecked();
            item.currentChecked = isChecked;

            Log.d("fadsfdas", "Checkbox clicked");

            if (isChecked) {
                callback.updateList(Operation.ADD, item.getId(), holder.getAdapterPosition());
            } else {
                callback.updateList(Operation.REMOVE, item.getId(), holder.getAdapterPosition());
            }
        });

// 为整个item设置点击事件
        holder.itemView.setOnClickListener(v -> {
            holder.mBinding.checkboxCurrent.performClick();
        });

        final String itemSourceTxt = item.getSourceTxt();
        if (!TextUtils.isEmpty(itemSourceTxt)) {
            holder.mBinding.tvSource.setText(itemSourceTxt);
        }

        final String targetTxt = item.getTargetTxt();
        if (!TextUtils.isEmpty(targetTxt)) {
            holder.mBinding.tvTarget.setText(targetTxt);
        }

        holder.mBinding.checkboxCurrent.setChecked(item.currentChecked);
    }

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

    public void updateSet(ArrayList<Integer> indexes) {
        if (null != indexes && !indexes.isEmpty()) {
            for (Integer index : indexes) {
                items.remove(index.intValue());
                notifyItemRemoved(index);
            }
        }
    }


    static class TranslationHolder extends RecyclerView.ViewHolder {
        private LayoutItemTranslationBinding mBinding;

        public TranslationHolder(@NonNull LayoutItemTranslationBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }

    static class TranslationItem extends Translations {
        public boolean currentChecked = false;

        public TranslationItem(long id, String sourceLanguage, String sourceTxt, String targetLanguage, String targetTxt, long currentTimeMillis, int exist, int collection) {
            super(id, sourceLanguage, sourceTxt, targetLanguage, targetTxt, currentTimeMillis, exist, collection);
        }

        public TranslationItem(String sourceLanguage, String sourceTxt, String targetLanguage, String targetTxt) {
            super(sourceLanguage, sourceTxt, targetLanguage, targetTxt);
        }
    }

    public interface TranslationItemCallback {
        void updateList(Operation operation, long id, int position);

        void speech(String value);
    }

    public enum Operation {
        REMOVE, ADD
    }
}
