package io.github.likcoras.ssbot.ignore;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class IgnoreHandler {
	
	private final Set<String> ignored;
	private final File file;
	
	public IgnoreHandler() {
		
		ignored = new HashSet<String>();
		file = new File("ignore.txt");
		
	}
	
	public void loadIgnores() throws IOException {
		
		file.createNewFile();
		
		BufferedReader in = new BufferedReader(new FileReader(file));
		
		synchronized (ignored) {
			
			ignored.clear();
			
			String line;
			while ((line = in.readLine()) != null)
				ignored.add(line.toLowerCase());
			
		}
		
		in.close();
		
	}
	
	public Set<String> listIgnores() {
		
		return Collections.unmodifiableSet(ignored);
		
	}
	
	public boolean isIgnored(String user) {
		
		return ignored.contains(user.toLowerCase());
		
	}
	
	public void addIgnore(String user) throws IOException {
		
		BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
		out.write(user + "\n");
		out.flush();
		out.close();
		
		loadIgnores();
		
	}
	
	public void delIgnore(String user) throws IOException {
		
		BufferedWriter out = new BufferedWriter(new FileWriter(file));
		
		for (String ignore : ignored)
			if (!ignore.equalsIgnoreCase(user))
				out.write(ignore + "\n");
		
		out.flush();
		out.close();
		
		loadIgnores();
		
	}
	
}

@@ -483,24 +483,37 @@
 %7D%0A%09%0A%09public 
+synchronized 
 void loadIgn
@@ -641,40 +641,8 @@
 %0A%09%09%0A
-%09%09synchronized (ignored) %7B%0A%09%09%09%0A%09
 %09%09ig
@@ -662,13 +662,11 @@
 ;%0A%09%09
-%09
 %0A%09%09
-%09
 Stri
@@ -674,17 +674,16 @@
 g line;%0A
-%09
 %09%09while 
@@ -715,17 +715,16 @@
 = null)%0A
-%09
 %09%09%09ignor
@@ -755,16 +755,8 @@
 ));%0A
-%09%09%09%0A%09%09%7D%0A
 %09%09%0A%09
@@ -973,24 +973,37 @@
 %7D%0A%09%0A%09public 
+synchronized 
 void addIgno
@@ -1048,195 +1048,205 @@
 %09%0A%09%09
-BufferedWriter out = new BufferedWriter(new FileWriter(file, true));%0A%09%09out.write(user + %22%5Cn%22);%0A%09%09out.flush();%0A%09%09out.close();%0A%09%09%0A%09%09loadIgnores();%0A%09%09%0A%09%7D%0A%09%0A%09public void delIgnore(String user
+if (ignored.add(user))%0A%09%09%09flush();%0A%09%09%0A%09%7D%0A%09%0A%09public synchronized void delIgnore(String user) throws IOException %7B%0A%09%09%0A%09%09if (ignored.remove(user))%0A%09%09%09flush();%0A%09%09%0A%09%7D%0A%09%0A%09private synchronized void flush(
 ) th
@@ -1371,48 +1371,8 @@
 ed)%0A
-%09%09%09if (!ignore.equalsIgnoreCase(user))%0A%09
 %09%09%09o
@@ -1436,28 +1436,8 @@
 %0A%09%09%0A
-%09%09loadIgnores();%0A%09%09%0A
 %09%7D%0A%09

Improve how IgnoreHandler handles operations