package project_2;

import java.io.File;
import java.io.PrintWriter;

public class PrintOnFile {
	
	/*
	 * This Class Print equtions in a file
	 * */
	
	private LinkedList<Equation> allEquatios;
	private LinkedList<Equation> validEquations;
	private LinkedList<Equation> invalidEquations;
	private PrintWriter w;


	public PrintOnFile(LinkedList<Equation> allEquatios, LinkedList<Equation> validEquations,
			LinkedList<Equation> invalidEquations) {

		this.allEquatios = allEquatios;
		this.validEquations = validEquations;
		this.invalidEquations = invalidEquations;
	}

	public void print(){
		printValid();
		printInvalid();
		printAll();

	}

	private void printValid(){
		String s="";
		try {
			w = new PrintWriter("Valid_Equation.txt");
			for (int i = 0; i < validEquations.getSize(); i++) {
				s=s+"["+validEquations.getIndexData(i).getPostfixLeft()+" = "+validEquations.getIndexData(i).getResultLeft()+" ] "+
						" =? "+" [ "+validEquations.getIndexData(i).getPostfixRight()+" = "+validEquations.getIndexData(i).getResultRight()+" ] "+" -> "
						+validEquations.getIndexData(i).getIsEquals()+"\r\n"+"-----------\r\n";
			}
			w.println(s);
			s="";
			
			w.close();
		} 
		catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	public void printInvalid(){
		String s="";
		try {
			w = new PrintWriter("In_Valid_Equation.txt");
			for (int i = 0; i < invalidEquations.getSize(); i++) {
				if(invalidEquations.getIndexData(i).isValidLeft()){
					s=s+invalidEquations.getIndexData(i).getLeftEquation()+"-> Valid -> "+invalidEquations.getIndexData(i).getResultLeft()+"\r\n"+"-----------\r\n";
				}
				else{
					s=s+invalidEquations.getIndexData(i).getLeftEquation()+"-> In Valid"+"\r\n"+"-------\r\n";
				}
				if(invalidEquations.getIndexData(i).isValidRight()){
					s=s+invalidEquations.getIndexData(i).getRightEquation()+"-> Valid -> "+invalidEquations.getIndexData(i).getResultRight()+"\r\n"+"---------\r\n";
				}
				else{
					s=s+invalidEquations.getIndexData(i).getRightEquation()+"-> In Valid"+"\r\n"+"-------\r\n";
				}
			}
			w.println(s);
			s="";
	
			w.close();
		} 
		catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void printAll(){
		String s="";
		try {
			w = new PrintWriter("All_Equations.txt");
			
			for (int i = 0; i < allEquatios.getSize(); i++) {
				
				if(allEquatios.getIndexData(i).isValidLeft()){
					s=s+allEquatios.getIndexData(i).getLeftEquation()+"-> Valid -> "+allEquatios.getIndexData(i).getPostfixLeft()+"\r\n"+"-----------\r\n";
				}
				else{
					s=s+allEquatios.getIndexData(i).getLeftEquation()+"-> In Valid"+"\r\n"+"-------\r\n";
				}
				if(allEquatios.getIndexData(i).isValidRight()){
					s=s+allEquatios.getIndexData(i).getRightEquation()+"-> Valid -> "+allEquatios.getIndexData(i).getPostfixRight()+"\r\n"+"---------\r\n";
				}
				else{
					s=s+allEquatios.getIndexData(i).getRightEquation()+"-> In Valid"+"\r\n"+"-------\r\n";
				}
				
			}
			w.println(s);
			s="";
	
			w.close();
		} 
		catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	
	
	
	
	
	

}
