package de.geeksfactory.opacclient.utils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ISBNTools {
    public static String isbn13to10(String isbn13) {
        isbn13 = cleanupISBN(isbn13);

        if (isbn13.length() != 13) return isbn13;

        String isbn10 = isbn13.substring(3, 12);
        int checksum = 0;
        int weight = 10;

        for (int i = 0; i < isbn10.length(); i++) {
            char c = isbn10.charAt(i);
            checksum += Character.getNumericValue(c) * weight;
            weight--;
        }

        checksum = 11 - (checksum % 11);
        if (checksum == 10) {
            isbn10 += "X";
        } else if (checksum == 11) {
            isbn10 += "0";
        } else {
            isbn10 += checksum;
        }

        return isbn10;
    }

    public static boolean is_valid_isbn10(char[] digits) {
        digits = cleanupISBN(new String(digits)).toCharArray();
        int a = 0;
        for (int i = 0; i < 10; i++) {
            a += i * Integer.parseInt(String.valueOf(digits[i]));
        }
        return a % 11 == Integer.parseInt(String.valueOf(digits[9]));
    }

    private static String cleanupISBN(String isbn) {
        return isbn.replaceAll("[^\\dX]", ""); //Remove all characters that aren't digits or X
    }

    public static String getAmazonCoverURL(String isbn, boolean large) {
        if (large) {
            return "http://images.amazon.com/images/P/" + isbn13to10(isbn) + ".01.L";
        } else {
            return "http://images.amazon.com/images/P/" + isbn13to10(isbn) + ".01.THUMBZZZ";
        }
    }

    /**
     * If possible, changes a cover URL to represent the best sized image for the given display
     * size. If the URL is not supported, the original URL will be returned. This function may
     * execute HTTP reqests to check if the requested size is available, so use it asynchronously!
     *
     * @param url    Original cover URL
     * @param width  Desired width
     * @param height Desired height
     * @return Improved URL
     */
    public static String getBestSizeCoverUrl(String url, int width, int height) {
        Pattern vlbRegex = Pattern.compile("^https?://(?:www.)?vlb\\.de/GetBlob\\.aspx\\?" +
                ".*strIsbn=([0-9X]*)");
        Pattern amazonRegex = Pattern.compile(
                "^(https?://(:?images(?:-[^\\.]*)?\\.|[^\\.]*\\.images-)amazon\\" +
                        ".com/images/[PI]/[^\\.]*\\.(?:\\d\\d\\.)?)[^.]*\\.jpg");

        Matcher vlbMatcher = vlbRegex.matcher(url);
        Matcher amazonMatcher = amazonRegex.matcher(url);

        int max = Math.max(width, height);

        if (vlbMatcher.find()) {
            // VLB Covers, sizes accoding to documentation at page 42 of
            // http://info.vlb.de/files/vlb_rest_api_anwenderspezifikation.pdf
            if (height <= 90) {
                return "https://vlb.de/GetBlob.aspx?strIsbn=" + vlbMatcher.group(1) + "&size=S";
            } else if (height <= 200) {
                return "https://vlb.de/GetBlob.aspx?strIsbn=" + vlbMatcher.group(1) + "&size=M";
            } else if (width <= 599) {
                return "https://vlb.de/GetBlob.aspx?strIsbn=" + vlbMatcher.group(1) + "&size=L";
            } else { // Original size
                return "https://vlb.de/GetBlob.aspx?strIsbn=" + vlbMatcher.group(1);
            }
        } else if (amazonMatcher.find()) {
            // Amazon Covers, according to http://aaugh.com/imageabuse.html
            if (max <= 75) {
                return amazonMatcher.group(1) + "THUMB.jpg";
            } else if (max <= 110) {
                return amazonMatcher.group(1) + "T.jpg";
            } else if (max <= 160) {
                return amazonMatcher.group(1) + "jpg";
            } else if (max <= 500) {
                return amazonMatcher.group(1) + "L.jpg";
            } else {
                // Huge URL may not be available, so check first
                String largeUrl = amazonMatcher.group(1) + "L.jpg";
                String hugeUrl = amazonMatcher.group(1) + "_SCRM_.jpg";
                try {
                    HttpURLConnection conn = (HttpURLConnection) new URL(hugeUrl).openConnection();
                    conn.setRequestMethod("HEAD");
                    int code = conn.getResponseCode();
                    String contentType = conn.getHeaderField("Content-Type");
                    conn.disconnect();
                    if (code != 200 || !contentType.equals("image/jpeg")) {
                        return largeUrl;
                    } else {
                        return hugeUrl;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return largeUrl;
                }
            }
        } else {
            return url;
        }
    }
}

@@ -40,92 +40,8 @@
 s;%0A%0A
-import java.io.IOException;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URL;%0A
 impo
@@ -68,16 +68,16 @@
 atcher;%0A
+
 import j
@@ -1789,125 +1789,8 @@
 ned.
- This function may%0A     * execute HTTP reqests to check if the requested size is available, so use it asynchronously!
 %0A   
@@ -3383,239 +3383,21 @@
     
-if (max %3C= 75) %7B%0A                return amazonMatcher.group(1) + %22THUMB.jpg%22;%0A            %7D else if (max %3C= 110) %7B%0A                return amazonMatcher.group(1) + %22T.jpg%22;%0A            %7D else if (max %3C= 160) %7B%0A                return
+String base =
  ama
@@ -3419,16 +3419,8 @@
 p(1)
- + %22jpg%22
 ;%0A  
@@ -3433,1020 +3433,42 @@
     
-%7D else if (max %3C= 500) %7B%0A                return amazonMatcher.group(1) + %22L.jpg%22;%0A            %7D else %7B%0A                // Huge URL may not be available, so check first%0A                String largeUrl = amazonMatcher.group(1) + %22L.jpg%22;%0A                String hugeUrl = amazonMatcher.group(1) + %22_SCRM_.jpg%22;%0A                try %7B%0A                    HttpURLConnection conn = (HttpURLConnection) new URL(hugeUrl).openConnection();%0A                    conn.setRequestMethod(%22HEAD%22);%0A                    int code = conn.getResponseCode();%0A                    String contentType = conn.getHeaderField(%22Content-Type%22);%0A                    conn.disconnect();%0A                    if (code != 200 %7C%7C !contentType.equals(%22image/jpeg%22)) %7B%0A                        return largeUrl;%0A                    %7D else %7B%0A                        return hugeUrl;%0A                    %7D%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                    return largeUrl;%0A                %7D%0A            %7D
+return base + %22SL%22 + max + %22.jpg%22;
 %0A   

Update Amazon URL parser