package org.wikipedia.page.linkpreview;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.wikipedia.R;
import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.dataclient.page.PageSummary;
import org.wikipedia.page.PageTitle;
import org.wikipedia.util.L10nUtil;
import org.wikipedia.util.StringUtil;

import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class LinkPreviewContents {
    private static final int EXTRACT_MAX_SENTENCES = 2;

    private final PageTitle title;
    private final CharSequence extract;
    private final boolean disambiguation;

    public PageTitle getTitle() {
        return title;
    }

    public CharSequence getExtract() {
        return extract;
    }

    public boolean isDisambiguation() {
        return disambiguation;
    }

    LinkPreviewContents(@NonNull PageSummary pageSummary, @NonNull WikiSite wiki) {
        title = new PageTitle(pageSummary.getApiTitle(), wiki);
        disambiguation = pageSummary.getType().equals(PageSummary.TYPE_DISAMBIGUATION);
        String extractStr;
        extractStr = pageSummary.getExtractHtml();
        if (disambiguation) {
            extractStr = "<p>" + L10nUtil.getStringForArticleLanguage(title, R.string.link_preview_disambiguation_description) + "</p>" + extractStr;
        }
        extract = StringUtil.fromHtml(extractStr);
        title.setThumbUrl(pageSummary.getThumbnailUrl());
    }

    private static String createLegacyExtractText(@NonNull PageSummary pageSummary,
                                                  @NonNull WikiSite wikiSite) {
        String noParens = removeParens(pageSummary.getExtract());
        List<String> sentences = getSentences(noParens, wikiSite);
        return makeStringFromSentences(sentences, EXTRACT_MAX_SENTENCES);
    }

    /**
     * Remove text contained in parentheses from a string.
     * @param text String to be processed.
     * @return New string that is the same as the original string, but without any
     * content in parentheses.
     */
    private static String removeParens(@Nullable String text) {
        if (text == null) {
            return "";
        }

        StringBuilder outStr = new StringBuilder(text.length());
        char c;
        int level = 0;
        int i = 0;
        for (; i < text.length(); i++) {
            c = text.charAt(i);
            if (c == ')' && level == 0) {
                // abort if we have an imbalance of parentheses
                return text;
            }
            if (c == '(') {
                level++;
                continue;
            } else if (c == ')') {
                level--;
                continue;
            }
            if (level == 0) {
                // Remove leading spaces before parentheses
                if (c == ' ' && (i < text.length() - 1) && text.charAt(i + 1) == '(') {
                    continue;
                }
                outStr.append(c);
            }
        }
        // fill in the rest of the string
        if (i + 1 < text.length()) {
            outStr.append(text.substring(i + 1));
        }
        // if we had an imbalance of parentheses, then return the original string,
        // instead of the transformed one.
        return (level == 0) ? outStr.toString() : text;
    }

    /**
     * Split a block of text into sentences, taking into account the language in which
     * the text is assumed to be.
     * @param text Text to be transformed into sentences.
     * @param wiki WikiSite that will provide the language of the given text.
     * @return List of sentences.
     */
    private static List<String> getSentences(String text, WikiSite wiki) {
        List<String> sentenceList = new ArrayList<>();
        BreakIterator iterator = BreakIterator.getSentenceInstance(new Locale(wiki.languageCode()));
        // feed the text into the iterator, with line breaks removed:
        text = text.replaceAll("(\r|\n)", " ");
        iterator.setText(text);
        for (int start = iterator.first(), end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) {
            String sentence = text.substring(start, end).trim();
            if (TextUtils.isGraphic(sentence)) {
                // if it's the first sentence, then remove parentheses from it.
                String formattedSentence = sentenceList.isEmpty() ? removeParens(sentence) : sentence;
                sentenceList.add(formattedSentence);
            }
        }
        // if we couldn't detect any sentences using the BreakIterator, then just return the
        // original text as a single sentence.
        if (sentenceList.isEmpty()) {
            sentenceList.add(text);
        }
        return sentenceList;
    }

    private static String makeStringFromSentences(List<String> sentences, int maxSentences) {
        return TextUtils.join(" ", sentences.subList(0, Math.min(maxSentences, sentences.size())));
    }
}

@@ -52,97 +52,28 @@
 roid
-.text.TextUtils;%0A%0Aimport androidx.annotation.NonNull;%0Aimport androidx.annotation.Nullable
+x.annotation.NonNull
 ;%0A%0Ai
@@ -303,208 +303,42 @@
 l;%0A%0A
-import java.text.BreakIterator;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Locale;%0A%0Apublic class LinkPreviewContents %7B%0A    private static final int EXTRACT_MAX_SENTENCES = 2;%0A
+public class LinkPreviewContents %7B
 %0A   
@@ -1285,3533 +1285,7 @@
    %7D
-%0A%0A    private static String createLegacyExtractText(@NonNull PageSummary pageSummary,%0A                                                  @NonNull WikiSite wikiSite) %7B%0A        String noParens = removeParens(pageSummary.getExtract());%0A        List%3CString%3E sentences = getSentences(noParens, wikiSite);%0A        return makeStringFromSentences(sentences, EXTRACT_MAX_SENTENCES);%0A    %7D%0A%0A    /**%0A     * Remove text contained in parentheses from a string.%0A     * @param text String to be processed.%0A     * @return New string that is the same as the original string, but without any%0A     * content in parentheses.%0A     */%0A    private static String removeParens(@Nullable String text) %7B%0A        if (text == null) %7B%0A            return %22%22;%0A        %7D%0A%0A        StringBuilder outStr = new StringBuilder(text.length());%0A        char c;%0A        int level = 0;%0A        int i = 0;%0A        for (; i %3C text.length(); i++) %7B%0A            c = text.charAt(i);%0A            if (c == ')' && level == 0) %7B%0A                // abort if we have an imbalance of parentheses%0A                return text;%0A            %7D%0A            if (c == '(') %7B%0A                level++;%0A                continue;%0A            %7D else if (c == ')') %7B%0A                level--;%0A                continue;%0A            %7D%0A            if (level == 0) %7B%0A                // Remove leading spaces before parentheses%0A                if (c == ' ' && (i %3C text.length() - 1) && text.charAt(i + 1) == '(') %7B%0A                    continue;%0A                %7D%0A                outStr.append(c);%0A            %7D%0A        %7D%0A        // fill in the rest of the string%0A        if (i + 1 %3C text.length()) %7B%0A            outStr.append(text.substring(i + 1));%0A        %7D%0A        // if we had an imbalance of parentheses, then return the original string,%0A        // instead of the transformed one.%0A        return (level == 0) ? outStr.toString() : text;%0A    %7D%0A%0A    /**%0A     * Split a block of text into sentences, taking into account the language in which%0A     * the text is assumed to be.%0A     * @param text Text to be transformed into sentences.%0A     * @param wiki WikiSite that will provide the language of the given text.%0A     * @return List of sentences.%0A     */%0A    private static List%3CString%3E getSentences(String text, WikiSite wiki) %7B%0A        List%3CString%3E sentenceList = new ArrayList%3C%3E();%0A        BreakIterator iterator = BreakIterator.getSentenceInstance(new Locale(wiki.languageCode()));%0A        // feed the text into the iterator, with line breaks removed:%0A        text = text.replaceAll(%22(%5Cr%7C%5Cn)%22, %22 %22);%0A        iterator.setText(text);%0A        for (int start = iterator.first(), end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) %7B%0A            String sentence = text.substring(start, end).trim();%0A            if (TextUtils.isGraphic(sentence)) %7B%0A                // if it's the first sentence, then remove parentheses from it.%0A                String formattedSentence = sentenceList.isEmpty() ? removeParens(sentence) : sentence;%0A                sentenceList.add(formattedSentence);%0A            %7D%0A        %7D%0A        // if we couldn't detect any sentences using the BreakIterator, then just return the%0A        // original text as a single sentence.%0A        if (sentenceList.isEmpty()) %7B%0A            sentenceList.add(text);%0A        %7D%0A        return sentenceList;%0A    %7D%0A%0A    private static String makeStringFromSentences(List%3CString%3E sentences, int maxSentences) %7B%0A        return TextUtils.join(%22 %22, sentences.subList(0, Math.min(maxSentences, sentences.size())));%0A    %7D
 %0A%7D%0A

Remove unused methods