package com.github.gfx.hankei_n.model;

import com.google.android.gms.location.places.AutocompletePrediction;

import com.github.gfx.hankei_n.R;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.widget.ArrayAdapter;
import android.widget.Filter;

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

import javax.annotation.ParametersAreNonnullByDefault;

import rx.Subscriber;
import timber.log.Timber;

// FIXME: Use SupportPlaceAutocompleteFragment https://developers.google.com/places/android-api/autocomplete
@ParametersAreNonnullByDefault
public class AddressAutocompleAdapter extends ArrayAdapter<Spanned> {

    final PlaceEngine placeEngine;

    final StyleSpan bold = new StyleSpan(Typeface.BOLD);

    public AddressAutocompleAdapter(Context context, PlaceEngine placeEngine) {
        super(context, R.layout.widget_autocomplete, android.R.id.text1);
        this.placeEngine = placeEngine;
    }

    List<Spanned> convertToSpannedList(Iterable<AutocompletePrediction> predictions) {
        List<Spanned> list = new ArrayList<>();

        for (AutocompletePrediction prediction : predictions) {
            list.add(convertToSpanned(prediction));
        }
        return list;
    }

    Spanned convertToSpanned(AutocompletePrediction prediction) {
        SpannableString ss = new SpannableString(prediction.getDescription());

        for (AutocompletePrediction.Substring substring : prediction.getMatchedSubstrings()) {
            ss.setSpan(bold, substring.getOffset(),
                    substring.getOffset() + substring.getLength(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    boolean isCompleteInput(@Nullable CharSequence s) {
        if (TextUtils.isEmpty(s)) {
            return false;
        }
        for (int i = 0, length = s.length(); i < length; i++) {
            char c = s.charAt(i);
            if (c >= 'ａ' && c <= 'ｚ') { // zenkaku alphabet
                return false;
            }
        }
        return true;
    }

    void performQuery(final String query) {
        Timber.d("performQuery: %s", query);

        placeEngine.queryAutocompletion(query)
                .subscribe(new Subscriber<Iterable<AutocompletePrediction>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.w(e, "failed to queryAutocompletion queryAutocompletion for: %s", query);
                    }

                    @Override
                    public void onNext(Iterable<AutocompletePrediction> predictions) {
                        clear();
                        addAll(convertToSpannedList(predictions));
                    }
                });

    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(@Nullable CharSequence constraint) {
                if (isCompleteInput(constraint)) {
                    assert constraint != null;
                    performQuery(constraint.toString());
                }
                return new FilterResults();
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                notifyDataSetChanged();
            }
        };
    }
}

@@ -253,74 +253,8 @@
 le;%0A
-import android.text.SpannableString;%0Aimport android.text.Spanned;%0A
 impo
@@ -740,23 +740,28 @@
 Adapter%3C
-Spanned
+CharSequence
 %3E %7B%0A%0A   
@@ -1061,23 +1061,28 @@
    List%3C
-Spanned
+CharSequence
 %3E conver
@@ -1157,23 +1157,28 @@
    List%3C
-Spanned
+CharSequence
 %3E list =
@@ -1287,489 +1287,36 @@
 add(
-convertToSpanned(prediction));%0A        %7D%0A        return list;%0A    %7D%0A%0A    Spanned convertToSpanned(AutocompletePrediction prediction) %7B%0A        SpannableString ss = new SpannableString(prediction.getDescription());%0A%0A        for (AutocompletePrediction.Substring substring : prediction.getMatchedSubstrings()) %7B%0A            ss.setSpan(bold, substring.getOffset(),%0A                    substring.getOffset() + substring.getLength(),%0A                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
+prediction.getFullText(bold)
 );%0A 
@@ -1339,18 +1339,20 @@
  return 
-ss
+list
 ;%0A    %7D%0A

remove use of deprecated methods