package com.application.tak.takapplication.services;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by azielinska on 29/11/2017.
 */

public class FlexibleDateParser {
    private List<ThreadLocal<SimpleDateFormat>> threadLocals = new ArrayList<ThreadLocal<SimpleDateFormat>>();

    public FlexibleDateParser( final TimeZone tz){
        threadLocals.clear();
        List<String> formats = new ArrayList<String>();
        formats.add("dd.MM.yyyy");
        formats.add("dd MMM yyyy");
        formats.add("EEEE");
        formats.add("dd/MM/yyyy");
        formats.add("dd-MM-yyyy");
        formats.add("MM.dd.yyyy");
        formats.add("MM/dd/yyyy");
        formats.add("MM-dd-yyyy");
        for (final String format : formats) {
            ThreadLocal<SimpleDateFormat> dateFormatTL = new ThreadLocal<SimpleDateFormat>() {
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    sdf.setTimeZone(tz);
                    sdf.setLenient(false);
                    return sdf;
                }
            };
            threadLocals.add(dateFormatTL);
        }
    }

    public Date parseDate(String dateStr) throws ParseException {
        for (ThreadLocal<SimpleDateFormat> tl : threadLocals) {
            SimpleDateFormat sdf = tl.get();
            try {
                return sdf.parse(dateStr);
            } catch (ParseException e) {
                // Ignore and try next date parser
            }
        }
        // All parsers failed
        return null;
    }
}