package eng;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Data {

	private ArrayList<String> data;
	private ArrayList<ObjectHolder> object;

	public Data() {
		data = new ArrayList<String>();
		object = new ArrayList<ObjectHolder>();
	}

	public void addData(String dataLine) {
		data.add(dataLine);
	}

	public void addDataSet(ArrayList<String> dataSet) {
		for(int i = 0; i < dataSet.size(); i++)
			data.add(dataSet.get(i));
	}

	public String getData(String dataLine) {

		for(int i = 0; i < data.size(); i++)
			if(data.get(i).indexOf(dataLine) != -1)
				return data.get(i);

		return null;
	}

	public void removeData(String dataLine) {

		for(int i = 0; i < data.size(); i++)
			if(data.get(i).indexOf(dataLine) != -1) {
				data.remove(i);
				i--;
			}
	}

	public ArrayList<String> getAllData() {
		return data;
	}

	public ArrayList<String> getDataSet(String dataLine) {

		ArrayList<String> dataSet = new ArrayList<String>();

		for(int i = 0; i < data.size(); i++)
			if(data.get(i).indexOf(dataLine) != -1)
				dataSet.add(data.get(i));

		return dataSet;
	}
	
	public boolean hasData(String dataLine) {
		
		for(int i = 0; i < data.size(); i++)
			if(data.get(i).indexOf(dataLine) != -1)
				return true;
		
		return false;
	}
	
	public void sort() {
		Collections.sort(data);
	}

	public void readFile(File file) {

		data = new ArrayList<String>();
		Scanner sc = null;

		try {
			sc = new Scanner(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		while(sc.hasNextLine())
			data.add(sc.nextLine());
	}

	public void saveFile() {

		JFileChooser fc = new JFileChooser();
		int rval = fc.showSaveDialog(fc);

		PrintWriter outputStream = null;

		if(rval == JFileChooser.APPROVE_OPTION) {

			File file = fc.getSelectedFile();

			try {

				file = new File(file + ".txt");
				outputStream = new PrintWriter(new FileOutputStream(file));
				
				sort();
				
				for(int i = 0; i < data.size(); i++)
					outputStream.println(data.get(i));
				
				outputStream.close();
			}
			
			catch(IOException a) {
				JOptionPane.showMessageDialog(null, "File already exists.");
			}
		}
	}
	
	public void addObject(String name, Object obj) {
		object.add(new ObjectHolder(name, obj));
	}
	
	public Object getObject(String name) {
		
		for(int i = 0; i < object.size(); i++)
			if(object.get(i).getName().equals(name))
				return object.get(i).getObject();
		
		return null;
	}
	
	public void removeObject(String name) {
		
		for(int i = 0; i < object.size(); i++)
			if(object.get(i).getName().equals(name))
				object.remove(i);
	}
	
	public boolean hasObject(String name) {
		
		for(int i = 0; i < object.size(); i++)
			if(object.get(i).getName().equals(name))
				return true;
		
		return false;
	}
}

class ObjectHolder {
	
	private String name;
	private Object object;
	
	public ObjectHolder(String name, Object object) {
		this.name = name;
		this.object = object;
	}
	
	public String getName() {
		return name;
	}
	
	public Object getObject() {
		return object;
	}
}