package org.keyscience.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.keyscience.R;

public class ExploreTheKeyAdapter implements GoogleMap.InfoWindowAdapter {

    private Context context;

    public ExploreTheKeyAdapter(Context context) {
        this.context = context;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        View infoWindow = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.explore_key_info_window, null);
        TextView textView = infoWindow.findViewById(R.id.info_window_text);
        TextView title = infoWindow.findViewById(R.id.info_window_title);

        String formattedText = marker.getSnippet();

        Document doc = Jsoup.parse(formattedText);
        Elements p = doc.getElementsByTag("p");

        // retrieve only the first paragraph
        textView.setText(p.get(0).text());
        title.setText(marker.getTitle());
        return infoWindow;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
}
