import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Helper {

	public static synchronized void scriviFile(JSONObject json, String path) throws IOException {
		BufferedWriter b = new BufferedWriter(new FileWriter(path));
		b.write(json.toString());
		b.close();
	}
	
	public static synchronized String leggiFile(String path) throws IOException {
		String line;
		String answer = "";
		FileReader fileReader = new FileReader(path);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		while((line = bufferedReader.readLine()) != null) {
			answer += line;
		}
		bufferedReader.close();
		return answer;
	}
	
	// se add = true, aggiungo il valore n alla codaVal; 
	// se add = false, rimuovo il valore n dalla codaVal
	public static synchronized void modificaFileStato(MyQueue<Integer> coda, boolean add, int n, int valore, String path) {
		try {
			JSONObject statoJson = new JSONObject(Helper.leggiFile(path));
			statoJson.put("valore",valore);
			JSONArray arrayCodaVal = statoJson.getJSONArray("codaVal");
			if(add)
				arrayCodaVal.put(n);
			else {
				int index = -1;
				for(int i=0; i<arrayCodaVal.length(); i++) {
					if(arrayCodaVal.getInt(i)==n)
						index = i;
				}
				arrayCodaVal.remove(index);
			}
			Helper.scriviFile(statoJson.put("codaVal",arrayCodaVal), path);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static synchronized void modificaFileStato(boolean add, int n, String path) {
		try {
			JSONObject statoJson = new JSONObject(Helper.leggiFile(path));
			JSONArray arrayCodaVal = statoJson.getJSONArray("codaVal");
			if(add)
				arrayCodaVal.put(n);
			else {
				int index = -1;
				for(int i=0; i<arrayCodaVal.length(); i++) {
					if(arrayCodaVal.getInt(i)==n)
						index = i;
				}
				arrayCodaVal.remove(index);
			}
			Helper.scriviFile(statoJson.put("codaVal",arrayCodaVal), path);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static synchronized void modificaFileStato(boolean add, Pair p, String path) {
		try {
			JSONObject statoJson = new JSONObject(Helper.leggiFile(path)); 
			JSONArray arrayCodaMsg = statoJson.getJSONArray("codaMsg"); 
			if(add) 
				arrayCodaMsg.put(p.toJsonObject());
			else {
				int index = -1;
				for(int i=0; i<arrayCodaMsg.length(); i++) {
					JSONObject msgJson = arrayCodaMsg.getJSONObject(i); 
					if(msgJson.getString("topic").equals(p.getTopic()) && msgJson.getJSONObject("msg").toString().equals(p.getMsg()) )
						index = i;
				}
				arrayCodaMsg.remove(index);
			}
			Helper.scriviFile(statoJson.put("codaMsg",arrayCodaMsg), path);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static synchronized <T> void getFromFileStato(MyQueue<T> coda, String key, String path) throws JSONException, IOException {
		JSONObject statoJson = new JSONObject(Helper.leggiFile(path));
		JSONArray arrayCoda = statoJson.getJSONArray(key);
		if(coda.getClass().equals(Pair.class)) {
			MyQueue<Pair> codaMsg = (MyQueue<Pair>) coda;
			for(int i = 0; i<arrayCoda.length(); i++) {
				JSONObject pairJson = arrayCoda.getJSONObject(i);
				Pair p = new Pair(pairJson.getString("topic"), pairJson.getString("msg"));
				codaMsg.send(p);
				
			}
		}
		if(coda.getClass().equals(Integer.class)) {
			MyQueue<Integer> codaVal = (MyQueue<Integer>) coda;
			for(int i = 0; i<arrayCoda.length(); i++) {
				codaVal.send(arrayCoda.getInt(i));
			}
		}
			
	}
	
}