/*
 * MIT License
 *
 * Copyright (c) 2016 Santiago Hollmann
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.shollmann.android.igcparser.util;

import android.text.TextUtils;

import com.google.android.gms.maps.model.LatLng;
import com.shollmann.android.igcparser.model.ILatLonRecord;
import com.shollmann.android.igcparser.model.LatLon;

import java.io.File;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class Utilities {
    private static DecimalFormat decimalFormatter = null;
    private static DecimalFormatSymbols symbols = null;

    public static LatLon generateCoordinates(String lat, String lon) {
        double latDegrees = Double.valueOf(lat.substring(0, 2));
        String latMinutes = lat.substring(2, 4);
        String latSeconds = lat.substring(4, lat.length() - 1);
        double latRealMinutes = Double.valueOf(latMinutes + "." + latSeconds) / 60;
        double latDecimalFormat = latDegrees + latRealMinutes;
        if (hasSuffix(lat, 'S')) {
            latDecimalFormat = -1 * latDecimalFormat;
        }

        double lonDegrees = Double.valueOf(lon.substring(0, 3));
        String lonMinutes = lon.substring(3, 5);
        String lonSeconds = lon.substring(5, lon.length() - 1);
        double lonRealMinutes = Double.valueOf(lonMinutes + "." + lonSeconds) / 60;
        double lonDecimalFormat = lonDegrees + lonRealMinutes;
        if (hasSuffix(lon, 'W')) {
            lonDecimalFormat = -1 * lonDecimalFormat;
        }
        return new LatLon(latDecimalFormat, lonDecimalFormat);
    }

    private static boolean hasSuffix(String lat, char c) {
        return lat.charAt(lat.length() - 1) == c;
    }

    public static List<LatLng> getLatLngPoints(List<ILatLonRecord> listRecord) {
        ArrayList<LatLng> listLatLng = new ArrayList<>();
        for (ILatLonRecord record : listRecord) {
            final double lat = record.getLatLon().getLat();
            final double lon = record.getLatLon().getLon();
            if (lat != 0 && lon != 0) {
                listLatLng.add(new LatLng(lat, lon));
            }
        }
        return listLatLng;
    }

    public static String generateTime(String igcTime) {
        try {
            final String hoursString = igcTime.substring(0, 2);
            final String minutesString = igcTime.substring(2, 4);
            final String secondsString = igcTime.substring(4, 6);
            return hoursString + ":" + minutesString + ":" + secondsString;
        } catch (IndexOutOfBoundsException e) {
            Logger.logError(e.getMessage());
        }
        return Constants.EMPTY_STRING;
    }

    public static String getFlightTime(String departureTime, String landingTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        try {
            Date departureDate = sdf.parse(departureTime);
            Date landingDate = sdf.parse(landingTime);

            long diff = landingDate.getTime() - departureDate.getTime();
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000);

            return String.format(Constants.FLIGHT_DURATION_FORMAT, diffHours, diffMinutes);
        } catch (ParseException e) {
            Logger.logError(e.getMessage());
        }
        return Constants.FLIGHT_DURATION_ERROR;
    }

    public static String getTimeHHMM(String timeHHMMSS) {
        try {
            return timeHHMMSS.substring(0, timeHHMMSS.length() - 3);
        } catch (IndexOutOfBoundsException e) {
            Logger.logError(e.getMessage());
        }
        return Constants.EMPTY_STRING;
    }

    public static String getFormattedNumber(int number, Locale locale) {
        if (decimalFormatter == null) {
            symbols = DecimalFormatSymbols.getInstance(locale);
            decimalFormatter = new DecimalFormat();
            decimalFormatter.setDecimalFormatSymbols(symbols);
        }
        return decimalFormatter.format(number);
    }

    public static File getXCSoarDataFolder() {
        File xcsoarDataFile = new File(Constants.Path.XCSOARDATA);

        if (xcsoarDataFile.listFiles() != null) {
            return xcsoarDataFile;
        }

        xcsoarDataFile = new File(Constants.Path.SAMSUNG_XCSOARDATA);
        if (xcsoarDataFile.listFiles() != null) {
            return xcsoarDataFile;
        }

        xcsoarDataFile = new File(Constants.Path.STORAGE_XCSOARDATA);
        if (xcsoarDataFile.listFiles() != null) {
            return xcsoarDataFile;
        }

        xcsoarDataFile = new File(Constants.Path.STORAGE_LEGACY_XCSOARDATA);
        if (xcsoarDataFile.listFiles() != null) {
            return xcsoarDataFile;
        }

        return xcsoarDataFile;
    }

    public static File getSdCardFolder() {
        File xcsoarDataFile = new File(Constants.Path.SDCARD);
        if (xcsoarDataFile.listFiles() != null) {
            return xcsoarDataFile;
        }

        xcsoarDataFile = new File(Constants.Path.STORAGE_SDCARD);
        if (xcsoarDataFile.listFiles() != null) {
            return xcsoarDataFile;
        }

        xcsoarDataFile = new File(Constants.Path.STORAGE_LEGACY_SDCARD);
        if (xcsoarDataFile.listFiles() != null) {
            return xcsoarDataFile;
        }

        xcsoarDataFile = new File(Constants.Path.SAMSUNG_SDCARD);
        if (xcsoarDataFile.listFiles() != null) {
            return xcsoarDataFile;
        }

        return xcsoarDataFile;
    }

    public static String capitalizeText(String title) {
        if (title == null) {
            return null;
        }
        StringBuilder standardizeTitle = new StringBuilder();
        for (String word : title.split(Constants.SPACE_STRING)) {
            word = word.toLowerCase();
            word = capitalize(word);
            standardizeTitle.append(word).append(Constants.SPACE_STRING);
        }

        return standardizeTitle.toString().trim();
    }

    private static String capitalize(String word) {
        if (TextUtils.isEmpty(word)) {
            return Constants.EMPTY_STRING;
        }
        char first = word.charAt(0);
        if (Character.isUpperCase(first)) {
            return word;
        } else {
            return Character.toUpperCase(first) + word.substring(1);
        }
    }
}

@@ -3706,33 +3706,17 @@
 ch (
-IndexOutOfBoundsException
+Throwable
  e) 
@@ -4388,30 +4388,25 @@
  catch (
-ParseException
+Throwable
  e) %7B%0A  
@@ -4675,33 +4675,17 @@
 ch (
-IndexOutOfBoundsException
+Throwable
  e) 

Fix crash in utilities because of unhandled exception