package org.wikipedia.gallery;

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

import com.google.gson.annotations.SerializedName;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("unused")
public class MediaListItem implements Serializable {
    @Nullable private String title;
    @SerializedName("section_id") private int sectionId;
    @Nullable private String type;
    @Nullable private TextInfo caption;
    private boolean showInGallery;
    @Nullable @SerializedName("srcset") private List<ImageSrcSet> srcSets;

    public MediaListItem() {
    }

    public MediaListItem(@NonNull String title) {
        this.title = title;
    }

    @NonNull
    public String getType() {
        return StringUtils.defaultString(type);
    }

    @Nullable
    public TextInfo getCaption() {
        return caption;
    }

    public boolean showInGallery() {
        return showInGallery;
    }

    @NonNull
    public String getTitle() {
        return StringUtils.defaultString(title);
    }

    @NonNull
    public List<ImageSrcSet> getSrcSets() {
        return srcSets != null ? srcSets : Collections.emptyList();
    }

    @NonNull
    public String getImageUrl(int preferredSize) {
        Pattern pattern = Pattern.compile("/(\\d+)px-");

        String imageUrl = getSrcSets().get(getSrcSets().size() - 1).getSrc();
        int previousSize = 0;
        for (ImageSrcSet srcSet : getSrcSets()) {
            Matcher matcher = pattern.matcher(srcSet.getSrc());
            if (matcher.find() && matcher.group(1) != null) {
                int currentSize = Integer.parseInt(matcher.group(1));

                if (currentSize == preferredSize) {
                    imageUrl = srcSet.getSrc();
                    break;
                } else if (preferredSize < currentSize && preferredSize > previousSize) {
                    imageUrl = srcSet.getSrc();
                    break;
                }
                previousSize = currentSize;
            }
        }

        return StringUtils.defaultString(imageUrl);
    }

    public class ImageSrcSet implements Serializable {
        @Nullable private String src;
        @Nullable private String scale;

        @NonNull
        public String getSrc() {
            return StringUtils.defaultString(src);
        }
    }
}

@@ -1430,17 +1430,16 @@
 )px-%22);%0A
-%0A
         
@@ -1477,31 +1477,9 @@
 get(
-getSrcSets().size() - 1
+0
 ).ge
@@ -1502,24 +1502,44 @@
 int 
-previousSize = 0
+lastSizeDistance = Integer.MAX_VALUE
 ;%0A  
@@ -1743,19 +1743,36 @@
 rentSize
- = 
+Distance = Math.abs(
 Integer.
@@ -1797,18 +1797,34 @@
 roup(1))
+ - preferredSize)
 ;
-%0A
 %0A       
@@ -1851,24 +1851,34 @@
 Size
- == preferredSiz
+Distance %3C lastSizeDistanc
 e) %7B
@@ -1950,57 +1950,26 @@
     
-break;%0A                %7D else if (preferredSize %3C
+lastSizeDistance =
  cur
@@ -1980,180 +1980,35 @@
 Size
- && preferredSize %3E previousSize) %7B%0A                    imageUrl = srcSet.getSrc();%0A                    break;%0A                %7D%0A                previousSize = currentSize;
+Distance;%0A                %7D
 %0A   
@@ -2024,25 +2024,24 @@
 %7D%0A        %7D%0A
-%0A
         retu
@@ -2047,42 +2047,16 @@
 urn 
-StringUtils.defaultString(
 imageUrl
 );%0A 
@@ -2051,17 +2051,16 @@
 imageUrl
-)
 ;%0A    %7D%0A

Use absolute value to get the closest scale of the image url