package rra.reader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import rra.parser.RRAParser;
public class RRAswgFileReader {
private File fi=null;
private RRAParser parser=null;
private RRAMediator visualEvents=null;
private boolean stopIt=false;
private boolean disposeSmall=false;
private boolean pruneHealing=true;
private int disposeActions=0;
public RRAswgFileReader(File file,RRAMediator visualEvents) throws Exception {
this.fi=file;
this.parser=new RRAParser();
this.visualEvents=visualEvents;
this.visualEvents.setParser(parser);
}
public void run(boolean closeItAfterReading,boolean disposeSmall,int disposeActions,boolean pruneHealing) {
this.disposeSmall=disposeSmall;
this.disposeActions=disposeActions;
this.pruneHealing=pruneHealing;
int i=0;
try {
this.visualEvents.processEvent(RRAMediator.EventNewFile, null);
FileReader fr=new FileReader(this.fi);
BufferedReader bfrdr=new BufferedReader(fr);
String line=null;
while((line=bfrdr.readLine())!=null) {
i++;
statusChangeEvent("Reading line "+i);
try {
parser.parse(line,disposeSmall,disposeActions,pruneHealing);
} catch (Exception e) {
e.printStackTrace();
}
}
this.visualEvents.processEvent(RRAMediator.EventEOF, this.parser.getCombatList());
if(closeItAfterReading) {
bfrdr.close();
fr.close();
} else {
while(!stopIt) {
while((line=bfrdr.readLine())!=null) {
i++;
statusChangeEvent("Reading line "+i);
try {
if(parser.parse(line,disposeSmall,disposeActions,pruneHealing)) {
List<Object> l=new ArrayList<Object>();
l.add(this.parser.getCombatList().get(this.parser.getCombatList().size()-1));
this.visualEvents.processEvent(RRAMediator.EventNewCombat, l);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
bfrdr.close();
fr.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
statusChangeEvent("Idle");
}
public void statusChangeEvent(String status) {
List<String> l=new ArrayList<String>();
l.add(status);
this.visualEvents.processEvent(RRAMediator.EventStatusChange, l);
}
public void tryToStopIt() {
this.stopIt=true;
}
}