package org.keyscience.adapters;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

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

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

public class MapItemAdapter implements GoogleMap.InfoWindowAdapter {
    private View popup = null;
    private LayoutInflater inflater = null;
    private Context context;

    private Marker lastMarker = null;

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

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

    @Override
    public View getInfoContents(Marker marker) {
        if (inflater == null)
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (popup == null) {
            popup = inflater.inflate(R.layout.marker_frame, null);
        }

        if (lastMarker == null
                || !lastMarker.getId().equals(marker.getId())) {
            lastMarker = marker;

            ImageView closeButton = popup.findViewById(R.id.closeImage);

            TextView txtTitle = popup.findViewById(R.id.postTitle);
            TextView txtDesc = popup.findViewById(R.id.snippet);
            TextView txtSubmittedBy = popup.findViewById(R.id.submitted_by);

            String formattedText = marker.getSnippet();
            formattedText = formattedText.replaceAll("Uploaded on:", "");
            formattedText = formattedText.replaceAll("\\\\", "");

            formattedText = formattedText.replaceAll("\"\"", "\"");

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

            // Get <p> text
            List<String> pStrings = new ArrayList<>();
            for (Element x : p) {
                pStrings.add(x.text());
            }

            // Get <img> src
            Elements img = doc.getElementsByTag("img");
            String imageUrl = "";
            for (Element el : img) {
                imageUrl = el.absUrl("src");
            }

            // Get bold <strong> for title
            List<String> strongStrings = new ArrayList<>();
            Elements strong = doc.getElementsByTag("strong");
            for (Element el : strong) {
                strongStrings.add(el.text());
            }

            txtTitle.setText(strongStrings.get(0).toUpperCase());
            txtDesc.setText(pStrings.get(0));

            String submitterAndDate = pStrings.get(2) + " on " + pStrings.get(1);
            txtSubmittedBy.setText(submitterAndDate);

            Uri image = Uri.parse(imageUrl);

            ImageView userImage = popup.findViewById(R.id.userImage);

            if (image == null) {
                closeButton.setVisibility(View.GONE);
                userImage.setVisibility(View.GONE);
            } else {
                closeButton.setVisibility(View.VISIBLE);
                userImage.setVisibility(View.VISIBLE);
                Picasso.get().load(image)
                        .resize(1000, 1000)
                        .centerInside()
                        .noFade()
                        .into(userImage, new MarkerCallback(marker));
            }
        }

        return (popup);
    }

    class MarkerCallback implements Callback {
        Marker marker = null;

        MarkerCallback(Marker marker) {
            this.marker = marker;
        }

        @Override
        public void onSuccess() {
            if (marker != null && marker.isInfoWindowShown()) {
                marker.showInfoWindow();
            }
        }

        @Override
        public void onError(Exception e) {
            Log.e(getClass().getSimpleName(), "Error loading image!");
            Toast.makeText(context, "Error Loading: Image not found!", Toast.LENGTH_SHORT).show();
        }
    }
}