content: /*
   * MIT License
   *
   * Copyright (c) 2020 AmrDeveloper (Amr Hesham)
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in all
   * copies or substantial portions of the Software.
  
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   * SOFTWARE.
   */
  
  package com.amrdeveloper.codeview;
  
  import android.content.Context;
  
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Filter;
  import android.widget.Filterable;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Custom base adapter that to use it in CodeView auto complete and snippets feature
   * <p>
   * CodeViewAdapter supports to take a list of code which can include Keywords and snippets
   *
   * @since 1.1.0
   */
  public class CodeViewAdapter extends BaseAdapter implements Filterable {
  
      private List<Code> originalCodes;
      private List<Code> currentSuggestions;
      private final LayoutInflater layoutInflater;
      private final int codeViewLayoutId;
      private final int codeViewTextViewId;
  
      public CodeViewAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<Code> codes) {
          this.originalCodes = codes;
          this.currentSuggestions = new ArrayList<>();
          this.layoutInflater = LayoutInflater.from(context);
          this.codeViewLayoutId = resource;
          this.codeViewTextViewId = textViewResourceId;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
              convertView = layoutInflater.inflate(codeViewLayoutId, parent, false);
          }
  
          TextView textViewName = convertView.findViewById(codeViewTextViewId);
          Code currentCode = currentSuggestions.get(position);
          if (currentCode != null) {
              textViewName.setText(currentCode.getCodeTitle());
          }
          return convertView;
      }
  
      @Override
      public int getCount() {
          return currentSuggestions.size();
      }
  
      @Override
      public Object getItem(int position) {
          return currentSuggestions.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      /**
       * Update the current code list with new list
       *
       * @param newCodeList The new code list
       */
      public void updateCodes(List<Code> newCodeList) {
          currentSuggestions.clear();
          originalCodes.clear();
          originalCodes.addAll(newCodeList);
          notifyDataSetChanged();
      }
  
      /**
       * Clear the current code list and notify data set changed
       */
      public void clearCodes() {
          originalCodes.clear();
          currentSuggestions.clear();
          notifyDataSetChanged();
      }
  
      @Override
      public Filter getFilter() {
          return codeFilter;
      }
  
      private final Filter codeFilter = new Filter() {
          @Override
          protected FilterResults performFiltering(CharSequence constraint) {
              FilterResults results = new FilterResults();
              List<Code> suggestions = new ArrayList<>();
  
              // If no prefix text, show all codes
              if (constraint == null || constraint.length() == 0) {
                  results.values = originalCodes;
                  results.count = originalCodes.size();
                  return results;
              }
  
              // Calculate suggestions based on current text
              String filterPattern = constraint.toString().toLowerCase().trim();
              for (Code item : originalCodes) {
                  if (item.getCodePrefix().toLowerCase().contains(filterPattern)) {
                      suggestions.add(item);
                  }
              }
  
              results.values = suggestions;
              results.count = suggestions.size();
              return results;
          }
  
          @Override
          protected void publishResults(CharSequence constraint, FilterResults results) {
              currentSuggestions = (List<Code>) results.values;
              notifyDataSetChanged();
          }
  
          @Override
          public CharSequence convertResultToString(Object resultValue) {
              return ((Code) resultValue).getCodeBody();
          }
      };
  
  }
  