package dev.peerat.backend.model;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import dev.peerat.framework.utils.json.JsonArray;
import dev.peerat.framework.utils.json.JsonMap;

public class Puzzle {

	private int id;
	private String name;
	private String content;
	private byte[] soluce;
	private String verify;
	private int scoreMax;
	private Set<String> tags;
	private int depend;
	private Timestamp startDate;

	public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags,
			int depend, Timestamp startDate) {
		this.id = id;
		this.name = name;
		this.content = content;
		this.soluce = soluce;
		this.verify = verify;
		this.scoreMax = scoreMax;
		setTags(tags);
		this.depend = depend;
		this.startDate = startDate;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getContent() {
		return content;
	}

	public byte[] getSoluce() {
		return this.soluce;
	}

	public int getScoreMax() {
		return this.scoreMax;
	}

	public Set<String> getTags() {
		return this.tags;
	}

	/**
	 * DO NOT EVER EVER SHOW TO MISTER LUDWIG XD
	 * 
	 * @return DEATH
	 */
	public JsonArray getJsonTags() {
		if (tags == null)
			return null;
		JsonArray tagsJSON = new JsonArray();
		for (String tag : tags) {
			JsonMap tagJSON = new JsonMap();
			tagJSON.set("name", tag);
			tagsJSON.add(tagJSON);
		}
		return tagsJSON;
	}

	public void setTags(String tags) {
		if (tags == null || tags.isEmpty())
			this.tags = null;
		else
			this.tags = new HashSet<String>(Arrays.asList(tags.split(",")));
	}

	public int getDepend() {
		return this.depend;
	}
	
	public boolean hasStarted(){
		LocalDateTime now = LocalDateTime.now();
		boolean show = true;
		if(startDate != null) show &= now.isAfter(startDate.toLocalDateTime());
		return show;
	}

	@Override
	public boolean equals(Object object) {
		if (this == object)
			return true;
		if (!(object instanceof Puzzle))
			return false;
		return this.id == (((Puzzle) object).id);
	}

	@Override
	public int hashCode() {
		return id;
	}
}
