package me.pagarme.util;

import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;
import org.joda.time.DateTime;

public class PagarmeCalendar {
    private static final String HOLIDAY_CALENDAR_PATH = "https://raw.githubusercontent.com/pagarme/business-calendar/master/data/brazil/";

    private static JSONArray getPagarmeOfficialHolidayCalendar(Integer year) throws Exception {
        URL holidayCalendarURL = new URL(HOLIDAY_CALENDAR_PATH + year + ".json");
        Scanner scanner = new Scanner(holidayCalendarURL.openStream());
        String responseJSON = scanner.useDelimiter("\\Z").next();
        scanner.close();
        JSONObject calendarJSON = new JSONObject(responseJSON);
        JSONArray holidayCalendarAsJSONArray = (JSONArray) calendarJSON.getJSONArray("calendar");

        return holidayCalendarAsJSONArray;
    }

    private static String[] getHolidays(Integer year) throws Exception {
        JSONArray holidayCalendarAsJSONArray = getPagarmeOfficialHolidayCalendar(year);
        String[] holidays = new String[holidayCalendarAsJSONArray.length()];
        for (int i = 0; i < holidayCalendarAsJSONArray.length(); i++) {
            holidays[i] = holidayCalendarAsJSONArray.getJSONObject(i).get("date").toString();
        }

        return holidays;
    }

    public static DateTime getValidWeekday() throws Exception {
        DateTime weekday = new DateTime().plusDays(7);

        while ((isWeekend(weekday) == true || isHoliday(weekday) == true)){
            weekday = weekday.plusDays(2);
        }

        return weekday;
    }

    public static Boolean isWeekend(DateTime weekday) throws Exception {
        DateFormat dayName = new SimpleDateFormat("EEE", Locale.ENGLISH);
        return (dayName.format(weekday.toDate()).equals("Sun") || dayName.format(weekday.toDate()).equals("Sat"));
    }

    public static Boolean isHoliday(DateTime weekday) throws Exception {
        DateFormat yearMonthDay = new SimpleDateFormat("yyyy-MM-dd");
        for (String holiday : getHolidays(weekday.getYear())) {
            if (yearMonthDay.format(weekday.toDate()).equals(holiday)) {
                return true;
            }
        }

        return false;
    }
}

@@ -155,16 +155,59 @@
 canner;%0A
+%0Aimport javax.net.ssl.HttpsURLConnection;%0A%0A
 import o
@@ -285,16 +285,70 @@
 eTime;%0A%0A
+import me.pagar.security.TLSSocketConnectionFactory;%0A%0A
 public c
@@ -700,61 +700,464 @@
     
-Scanner scanner = new Scanner(holidayCalendarURL.open
+HttpsURLConnection connection = (HttpsURLConnection) holidayCalendarURL.openConnection();%0A        %0A        String version = System.getProperty(%22java.version%22);%0A        int sysMajorVersion = Integer.parseInt(String.valueOf(version.charAt(2)));%0A%0A        if (sysMajorVersion == 6) %7B%0A            connection.setSSLSocketFactory(new TLSSocketConnectionFactory());%0A        %7D%0A%0A        connection.connect();%0A        Scanner scanner = new Scanner(connection.getInput
 Stre

fix pagarme calendar client