package net.cooldude.homeworkmanager.api;

import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;

import net.cooldude.homeworkmanager.HomeworkManager;
import net.cooldude.homeworkmanager.api.util.TimeUtil;

import org.joda.time.DateTime;

public class HomeworkTask {

	public static final String TASK_EXTENSION = ".hwtask";
	
	private final String filename;
	private String displayname;
	private DateTime dueDate;
	private HomeworkSubject subject;
	private String description;
	private boolean hidden;
	private boolean complete;
	
	public HomeworkTask() {
		this.filename = HomeworkManager.RANDOM.nextInt(Integer.MAX_VALUE) + "_untitled";
	}
	
	public HomeworkTask(String filename) {
		this.filename = filename;
	}
	
	public String getDisplayname() {
		return displayname;
	}

	public void setDisplayname(String displayname) {
		this.displayname = displayname;
	}

	public DateTime getDueDate() {
		return dueDate;
	}

	public void setDueDate(DateTime dueDate) {
		this.dueDate = dueDate;
	}

	public HomeworkSubject getSubject() {
		return subject;
	}

	public void setSubject(HomeworkSubject subject) {
		this.subject = subject;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isHidden() {
		return hidden;
	}

	public void setHidden(boolean hidden) {
		this.hidden = hidden;
	}

	public boolean isComplete() {
		return complete;
	}

	public void setComplete(boolean complete) {
		this.complete = complete;
	}
	
	public String getFilename() {
		return filename;
	}
	
	// DON'T DO THIS! ITS TERRIBLE PRACTICE!!!!
	public HomeworkTask recreate(String filename) {
		HomeworkTask task = new HomeworkTask(filename);
		task.setComplete(isComplete());
		task.setDescription(getDescription());
		task.setDisplayname(getDisplayname());
		task.setDueDate(getDueDate());
		task.setHidden(isHidden());
		task.setSubject(getSubject());
		return task;
	}
	
	public void saveToFile() {
		DataFileManager dfm = DataFileManager.instance;
		
		dfm.openFile(DataFileManager.FILES_DIR + "tasks/" + getFilename() + TASK_EXTENSION);
		dfm.addProperty("displayname", displayname);
		dfm.addProperty("duedate", TimeUtil.encodeToString(dueDate));
		dfm.addProperty("subject", subject.getUUID());
		dfm.addProperty("description", description.replace("\n", "\\n").replace("=", "%%equal"));
		dfm.addProperty("hidden", hidden);
		dfm.addProperty("complete", complete);
		try {
			dfm.writeFile();
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Unable to save file properly. You may have lost data about the most recently changed / created homework task!");
		}
		dfm.closeFile();
	}

	public static HomeworkTask loadTaskFromFile(File f) {
		DataFileManager dfm = DataFileManager.instance;
		
		HomeworkTask task = new HomeworkTask(f.getName().replace(TASK_EXTENSION, ""));
		
		dfm.loadFile(DataFileManager.FILES_DIR + "tasks/" + f.getName());
		task.complete = Boolean.parseBoolean(dfm.getProperty("complete").getValue());
		task.hidden = Boolean.parseBoolean(dfm.getProperty("hidden").getValue());
		task.description = dfm.getProperty("description").getValue().replace("\\n", "\n").replace("%%equal", "=");
		task.displayname = dfm.getProperty("displayname").getValue();
		task.dueDate = TimeUtil.decodeFromString(dfm.getProperty("duedate").getValue());
		task.subject = HomeworkSubject.getRegisteredSubjects().get(Integer.parseInt(dfm.getProperty("subject").getValue()));
		dfm.closeFile();
		
		return task;
	}
	
	@Override
	public String toString() {
		return displayname;
	}
	
}
