package com.namelessmc.java_api;

import java.util.Objects;
import java.util.stream.StreamSupport;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class Website {

	private final String version;
	private final Update update;
	private final String[] modules;
	private final String language;

	Website(final JsonObject json) {
		Objects.requireNonNull(json, "Provided json object is null");

		this.version = json.get("nameless_version").getAsString();

		this.modules = StreamSupport.stream(json.get("modules").getAsJsonArray().spliterator(), false)
				.map(JsonElement::getAsString)
				.toArray(String[]::new);

		final JsonObject updateJson = json.get("version_update").getAsJsonObject();
		final boolean updateAvailable = updateJson.get("update").getAsBoolean();
		if (updateAvailable) {
			final String updateVersion = updateJson.get("version").getAsString();
			final boolean isUrgent = updateJson.get("urgent").getAsBoolean();
			this.update = new Update(isUrgent, updateVersion);
		} else {
			this.update = null;
		}

		this.language = json.get("language").getAsString();
	}

	public String getVersion() {
		return this.version;
	}

	public NamelessVersion getParsedVersion() {
		return NamelessVersion.parse(this.version);
	}

	/**
	 * @return Information about an update, or null if no update is available.
	 */
	public Update getUpdate() {
		return this.update;
	}

	public String[] getModules() {
		return this.modules;
	}

	public String getLanguage() {
		return this.language;
	}

	public static class Update {

		private final boolean isUrgent;
		private final String version;

		Update(final boolean isUrgent, final String version) {
			this.isUrgent = isUrgent;
			this.version = version;
		}

		public boolean isUrgent() {
			return this.isUrgent;
		}

		public String getVersion() {
			return this.version;
		}

		public NamelessVersion getParsedVersion() {
			return NamelessVersion.parse(this.version);
		}

	}

}

@@ -53,16 +53,43 @@
 bjects;%0A
+import java.util.Optional;%0A
 import j
@@ -262,22 +262,32 @@
 e final 
+Optional%3C
 Update
+%3E
  update;
@@ -1013,16 +1013,28 @@
 pdate = 
+Optional.of(
 new Upda
@@ -1056,24 +1056,25 @@
 dateVersion)
+)
 ;%0A%09%09%7D else %7B
@@ -1091,20 +1091,32 @@
 pdate = 
-null
+Optional.empty()
 ;%0A%09%09%7D%0A%0A%09
@@ -1418,22 +1418,32 @@
 %09public 
+Optional%3C
 Update
+%3E
  getUpda

use optional for update