package io.github.xtman.swift.client.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class SwiftResponseHeaderUtils {

    public static final String VALUE_SEPARATOR = ",";

    // Fri, 12 Aug 2016 14:24:16 GMT
    public static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";

    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        return sdf.format(date);
    }

    public static Date parseDate(String str) throws Throwable {
        if (str != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            return sdf.parse(str);
        }
        return null;
    }

    public static Date parseDate(Map<String, List<String>> headerFields, String key) throws Throwable {
        return parseDate(getHeaderFieldValue(headerFields, key));
    }

    public static String getHeaderFieldValue(Map<String, List<String>> headerFields, String key) throws Throwable {
        if (headerFields != null && headerFields.containsKey(key)) {
            return joinHeaderFieldValues(headerFields.get(key));
        }
        return null;
    }

    public static String joinHeaderFieldValues(List<String> values) {
        if (values == null || values.isEmpty()) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < values.size(); i++) {
            if (i > 0) {
                sb.append(VALUE_SEPARATOR);
            }
            sb.append(values.get(i));
        }
        String v = sb.toString();
        if (v.isEmpty()) {
            return null;
        }
        return v;
    }

    public static Long parseLong(Map<String, List<String>> headerFields, String key) throws Throwable {
        String value = getHeaderFieldValue(headerFields, key);
        if (value != null) {
            return Long.parseLong(value);
        }
        return null;
    }

    public static long parseLong(Map<String, List<String>> headerFields, String key, long defaultValue)
            throws Throwable {
        Long v = parseLong(headerFields, key);
        if (v == null) {
            return defaultValue;
        } else {
            return v;
        }
    }

    public static Double parseDouble(Map<String, List<String>> headerFields, String key) throws Throwable {
        String value = getHeaderFieldValue(headerFields, key);
        if (value != null) {
            return Double.parseDouble(value);
        }
        return null;
    }

    public static double parseLong(Map<String, List<String>> headerFields, String key, double defaultValue)
            throws Throwable {
        Double v = parseDouble(headerFields, key);
        if (v == null) {
            return defaultValue;
        } else {
            return v;
        }
    }

    public static boolean parseBoolean(Map<String, List<String>> headerFields, String key, boolean defaultValue)
            throws Throwable {
        String v = getHeaderFieldValue(headerFields, key);
        if (v == null) {
            return defaultValue;
        } else {
            return Boolean.parseBoolean(v);
        }
    }

    public static Map<String, String> convertHeaderFields(Map<String, List<String>> headerFields,
            String valueDelimiter) {
        Map<String, String> map = null;
        if (headerFields != null && !headerFields.isEmpty()) {
            map = new LinkedHashMap<String, String>(headerFields.size());
            for (String key : headerFields.keySet()) {
                String value = null;
                List<String> values = headerFields.get(key);
                if (values != null && !values.isEmpty()) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < values.size(); i++) {
                        if (i > 0) {
                            sb.append(valueDelimiter);
                        }
                        sb.append(values.get(i));
                    }
                    value = sb.toString();
                }
                map.put(key, value);
            }
        }
        if (map != null && !map.isEmpty()) {
            return map;
        }
        return null;
    }

    public static Map<String, String> convertHeaderFields(Map<String, List<String>> headerFields) {
        return convertHeaderFields(headerFields, VALUE_SEPARATOR);
    }

}
