package com.assimilate.alltrans.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;

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

import com.assimilate.alltrans.R;
import com.assimilate.alltrans.model.Language;
import com.assimilate.alltrans.databinding.LanguageItemLayoutBinding;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

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

public class LanguageAdapter extends RecyclerView.Adapter<LanguageAdapter.LanguageHolder> implements Filterable {

    private final Context mContext;
    private final ArrayList<Language> languages;
    private final ArrayList<Language> filteredLanguages;
    private final OnClickListener listener;
    private final RequestOptions options;

    public LanguageAdapter(@NonNull final Context mContext, @NonNull final ArrayList<Language> languageList, final OnClickListener onClickListener) {
        this.mContext = mContext;
        this.languages = new ArrayList<>(languageList);
        this.filteredLanguages = new ArrayList<>(languageList);
        this.listener = onClickListener;

        options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .fitCenter();
    }

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

    @Override
    public void onBindViewHolder(@NonNull LanguageHolder holder, int position) {
        final Language currentLanguage = filteredLanguages.get(position);

        if (currentLanguage != null) {
            drawImage(currentLanguage.mFlagUrl, holder.mBinding.languageFlag);
            if (!TextUtils.isEmpty(currentLanguage.getLanguage())) {
                holder.mBinding.language.setText(currentLanguage.getLanguage());
            } else {
                holder.mBinding.language.setText(mContext.getString(R.string.app_name));
            }
        }

        holder.mBinding.getRoot().setOnClickListener(view -> {
            final int pos = holder.getAdapterPosition();
            if (listener != null)
                listener.click(pos, currentLanguage);
        });
    }

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

    private void drawImage(@NonNull final String url, @NonNull final ImageView view) {
        if (TextUtils.isEmpty(url)) {
            Glide.with(mContext)
                    .applyDefaultRequestOptions(options)
                    .load(R.mipmap.ic_launcher)
                    .into(view);
        } else {
            Glide.with(mContext)
                    .applyDefaultRequestOptions(options)
                    .load(url)
                    .into(view);
        }
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                List<Language> filteredList = new ArrayList<>();
                if (TextUtils.isEmpty(constraint)) {
                    filteredList.addAll(languages);
                } else {
                    String filterPattern = constraint.toString().toLowerCase().trim();
                    for (Language language : languages) {
                        if (language.getLanguage().toLowerCase().contains(filterPattern)) {
                            filteredList.add(language);
                        }
                    }
                }
                FilterResults results = new FilterResults();
                results.values = filteredList;
                return results;
            }

            @Override
            @SuppressWarnings("unchecked")
            protected void publishResults(CharSequence constraint, FilterResults results) {
                filteredLanguages.clear();
                filteredLanguages.addAll((List<Language>) results.values);
                notifyDataSetChanged();
            }
        };
    }

    public static class LanguageHolder extends RecyclerView.ViewHolder {
        public LanguageItemLayoutBinding mBinding;

        public LanguageHolder(@NonNull LanguageItemLayoutBinding binding) {
            super(binding.getRoot());
            this.mBinding = binding;
        }
    }

    public interface OnClickListener {
        void click(int position, Language language);
    }
}
