package com.alexrnl.subtitlecorrector.io.subrip;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.alexrnl.commons.error.ExceptionUtils;
import com.alexrnl.commons.io.IOUtils;
import com.alexrnl.subtitlecorrector.common.Subtitle;
import com.alexrnl.subtitlecorrector.common.SubtitleFile;
import com.alexrnl.subtitlecorrector.io.SubtitleReader;

/**
 * Reader for the SubRip format.<br />
 * @author Alex
 */
public class SubRipReader extends SubtitleReader {
	/** Logger */
	private static Logger			lg	= Logger.getLogger(SubRipReader.class.getName());
	
	/** The date formatter */
	private final SimpleDateFormat	dateFormatter;
	
	/**
	 * Constructor #1.<br />
	 * Default constructor, uses UTF-8 for reading the file.
	 */
	public SubRipReader () {
		this(StandardCharsets.UTF_8);
	}

	/**
	 * Constructor #2.<br />
	 * @param charSet
	 *        the character set to use for reading the file.
	 */
	public SubRipReader (final Charset charSet) {
		super(charSet);
		dateFormatter = new SimpleDateFormat(SubRip.SUBRIP_DATE_FORMAT);
	}

	@Override
	protected Subtitle readSubtitle (final SubtitleFile subtitleFile, final BufferedReader reader)
			throws IOException {
		final long begin;
		final long end;
		final StringBuilder content = new StringBuilder();
		String currentLine;
		
		// Removing empty lines
		do {
			currentLine = IOUtils.readLine(reader).trim();
		} while (currentLine.isEmpty());
		
		// Get the subtitle number
		int subtitleIndex;
		try {
			subtitleIndex = Integer.parseInt(currentLine);
		} catch (final NumberFormatException e) {
			lg.warning("Could not parse '" + currentLine + "' as a number: " + ExceptionUtils.display(e));
			throw new IOException("Could not parse a subtitle number properly", e);
		}
		if (lg.isLoggable(Level.INFO)) {
			lg.info("Reading subtitle#" + subtitleIndex);
		}
		
		// Get the begin and end dates of the subtitle
		currentLine = IOUtils.readLine(reader).trim();
		final String[] dates = currentLine.split(SubRip.SUBRIP_DATE_SEPARATOR);
		if (dates.length < 2) {
			throw new IOException("Could not parse " + currentLine + " as a valid line in the subrip format");
		}
		try {
			begin = dateFormatter.parse(dates[0]).getTime();
			end = dateFormatter.parse(dates[1]).getTime();
			if (lg.isLoggable(Level.FINE)) {
				lg.fine("Begin date=" + dateFormatter.format(new Date(begin)));
				lg.fine("End date=" + dateFormatter.format(new Date(end)));
			}
		} catch (final ParseException e) {
			lg.warning("Could not parse either " + dates[0] + " or " + dates[1] + " as a date");
			throw new IOException("Problem while parsing a date", e);
		}
		
		// Get the content of the subtitle
		do {
			currentLine = IOUtils.readLine(reader).trim();
			if (!currentLine.isEmpty()) {
				content.append(currentLine).append(System.lineSeparator());
			}
		} while (!currentLine.isEmpty());
		
		return new Subtitle(begin, end, content.toString());
	}
}

@@ -2008,12 +2008,12 @@
 vel.
+F
 IN
-FO
+E
 )) %7B
@@ -2023,12 +2023,12 @@
 %09lg.
+f
 in
-fo
+e
 (%22Re
@@ -2506,32 +2506,33 @@
 gable(Level.FINE
+R
 )) %7B%0A%09%09%09%09lg.fine
@@ -2531,16 +2531,17 @@
 %09lg.fine
+r
 (%22Begin 
@@ -2600,16 +2600,17 @@
 %09lg.fine
+r
 (%22End da

Update log level of SubRipReader class