package es.uniovi.asw.parser;

import es.uniovi.asw.model.Citizen;
import es.uniovi.asw.reportwritter.GenerateLogText;
import es.uniovi.asw.util.Comprobador;
import es.uniovi.asw.util.Console;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class RListTXT extends RList implements ReadList {

	@Override
	public List<Citizen> readFile(String path) {
		List<Citizen> ciudadanos = new ArrayList<Citizen>();

		String name;
		String surname;
		String email;
		String birth;
		String address;
		String nationality;
		String nif;
		int row = 0;
		StringBuilder logger = new StringBuilder();

		boolean nameResult, surnameResult, emailResult, birthResult, addressResult, nationalityResult,
				nifResult = true;
		String cadena;

		try {
			FileReader f = new FileReader(path);
			BufferedReader b = new BufferedReader(f);

			while ((cadena = b.readLine()) != null) {
				row++;
				String[] datos = cadena.split(",");
				if (datos.length != 7)
					logger.append("\tEn la linea " + row + " faltan datos del ciudadano");
				else {
					name = datos[0];
					nameResult = Comprobador.esTodoTexto(name);
					surname = datos[1];
					surnameResult = Comprobador.esTodoTexto(name);
					email = datos[2];
					emailResult = Comprobador.esEmailCorrecto(email);
					birth = datos[3];
					birthResult= Comprobador.esFecha(birth);
					address = datos[4];
					addressResult = Comprobador.esAddressCorrecto(address);
					nationality = datos[5];
					nationalityResult = Comprobador.esTodoTexto(nationality);
					nif = datos[6];
					nifResult = nif != null ? true : false;
					boolean todoOK = GenerateLogText.completeTextForLog(logger, nameResult, surnameResult, emailResult,
							birthResult, addressResult, nationalityResult, nifResult, row);
					if (todoOK) {
						ciudadanos.add(anadirCitizen(name, surname, email,
								new SimpleDateFormat("dd/MM/yyyy").parse(birth), address, nationality, nif));
					}
				}
			}
			b.close();
		} catch (ParseException e) {
			e.printStackTrace();

			Console.print("La fecha de nacimiento no tiene el formato correcto");
		} catch (IOException e) {
			String[] fileName = path.split("/");
			e.printStackTrace();
			Console.print("El fichero " + fileName[fileName.length - 1] + " no existe");

		} catch (Exception e) {
			e.printStackTrace();

		}

		// Crear el fichero log
		String[] cachos = path.split("/");
		String nombreFich1 = cachos[cachos.length - 1];
		/*String nombreFich = nombreFich1.replace(".xlsx", "");

		WreportP reporter = new WreportP();
		/*
		 * reporter.report(logger.); reporter.createErrorLogFile();
		 */
		//
		return ciudadanos;
	}
}

@@ -24,16 +24,56 @@
 arser;%0A%0A
+import es.uniovi.asw.dbupdate.WreportR;%0A
 import e
@@ -152,16 +152,64 @@
 ogText;%0A
+import es.uniovi.asw.reportwritter.WriteReport;%0A
 import e
@@ -540,16 +540,78 @@
 List %7B%0A%0A
+%09private static final WriteReport reporter = new WreportR();%0A%0A
 %09@Overri
@@ -992,21 +992,17 @@
 yResult,
-%0A%09%09%09%09
+ 
 nifResul
@@ -1255,21 +1255,23 @@
 %09%09%09%09
-logger.append
+reporter.report
 (%22%5Ct
@@ -1287,15 +1287,29 @@
 ea %22
- + row 
+%0A%09%09%09%09%09%09%09+ row%0A%09%09%09%09%09%09%09
 + %22 
@@ -1334,16 +1334,59 @@
 iudadano
+, no se ha podido a%C3%B1adir a la base de datos
 %22);%0A%09%09%09%09
@@ -1657,16 +1657,17 @@
 thResult
+ 
 = Compro
@@ -1996,17 +1996,24 @@
 (logger,
- 
+%0A%09%09%09%09%09%09%09
 nameResu
@@ -2089,33 +2089,40 @@
 tionalityResult,
- 
+%0A%09%09%09%09%09%09%09
 nifResult, row);
@@ -2157,16 +2157,25 @@
 udadanos
+%0A%09%09%09%09%09%09%09%09
 .add(ana
@@ -2215,16 +2215,18 @@
 %09%09%09%09%09%09%09%09
+%09%09
 new Simp
@@ -2251,16 +2251,29 @@
 M/yyyy%22)
+%0A%09%09%09%09%09%09%09%09%09%09%09%09
 .parse(b
@@ -2287,17 +2287,27 @@
 address,
- 
+%0A%09%09%09%09%09%09%09%09%09%09
 national
@@ -2376,32 +2376,34 @@
 ception e) %7B%0A%09%09%09
+//
 e.printStackTrac
@@ -2548,24 +2548,26 @@
 it(%22/%22);%0A%09%09%09
+//
 e.printStack
@@ -2637,17 +2637,22 @@
 gth - 1%5D
- 
+%0A%09%09%09%09%09
 + %22 no e
@@ -2834,182 +2834,8 @@
 1%5D;%0A
-%09%09/*String nombreFich = nombreFich1.replace(%22.xlsx%22, %22%22);%0A%0A%09%09WreportP reporter = new WreportP();%0A%09%09/*%0A%09%09 * reporter.report(logger.); reporter.createErrorLogFile();%0A%09%09 */%0A%09%09//
 %0A%09%09r

Update RListTXT.java