package me.limeglass.skellett.elements.expressions;

import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.Timespan;
import ch.njol.util.Kleenean;

public class ExprTimeValues extends SimpleExpression<Long> {

	static {
		Skript.registerExpression(ExprTimeValues.class, Long.class, ExpressionType.COMBINED, "(0ticks|1milli[( |-)]seconds|2seconds|3minutes|4hours|5days|6weeks|7months|8years) (of|from|[with]in) %timespans%");
	}

	private Expression<Timespan> timespans;
	private int setting;

	@Override
	public Class<? extends Long> getReturnType() {
		return Long.class;
	}

	@Override
	public boolean isSingle() {
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
		timespans = (Expression<Timespan>) expressions[0];
		setting = parseResult.mark;
		return true;
	}

	@Override
	public String toString(@Nullable Event event, boolean debug) {
		if (debug || timespans == null)
			return "time values of timespan";
		switch (setting) {
			case 0:
				return "ticks of timespan(s): " + timespans.toString(event, debug);
			case 1:
				return "milliseconds of timespan(s): " + timespans.toString(event, debug);
			case 2:
				return "seconds of timespan(s): " + timespans.toString(event, debug);
			case 3:
				return "minutes of timespan(s): " + timespans.toString(event, debug);
			case 4:
				return "hours of timespan(s): " + timespans.toString(event, debug);
			case 5:
				return "days of timespan(s): " + timespans.toString(event, debug);
			case 6:
				return "weeks of timespan(s): " + timespans.toString(event, debug);
			case 7:
				return "months of timespan(s): " + timespans.toString(event, debug);
			case 8:
				return "years of timespan(s): " + timespans.toString(event, debug);
			default:
				return "time values of timespan";
		}
	}

	@Override
	@Nullable
	protected Long[] get(Event event) {
		if (timespans == null)
			return null;
		return Arrays.stream(timespans.getArray(event)).map(timespan -> {
			switch (setting) {
				case 0:
					return timespan.getTicks_i();
				case 1:
					return timespan.getMilliSeconds();
				case 2:
					return TimeUnit.MILLISECONDS.toSeconds(timespan.getMilliSeconds());
				case 3:
					return TimeUnit.MILLISECONDS.toMinutes(timespan.getMilliSeconds());
				case 4:
					return TimeUnit.MILLISECONDS.toHours(timespan.getMilliSeconds());
				case 5:
					return TimeUnit.MILLISECONDS.toDays(timespan.getMilliSeconds());
				case 6:
					return TimeUnit.MILLISECONDS.toDays(timespan.getMilliSeconds()) / 7;
				case 7:
					return (TimeUnit.MILLISECONDS.toDays(timespan.getMilliSeconds()) / 7) / 4.348214;
				case 8:
					return ((TimeUnit.MILLISECONDS.toDays(timespan.getMilliSeconds()) / 7) / 4.348214) / 12;
				default:
					return null;
			}
		}).filter(Objects::nonNull).toArray(Long[]::new);
	}

}

@@ -661,15 +661,17 @@
  %22(0
+%C2%A6
 ticks%7C1
+%C2%A6
 mill
@@ -687,16 +687,17 @@
 econds%7C2
+%C2%A6
 seconds%7C
@@ -697,16 +697,17 @@
 econds%7C3
+%C2%A6
 minutes%7C
@@ -711,36 +711,40 @@
 es%7C4
+%C2%A6
 hours%7C5
+%C2%A6
 days%7C6
+%C2%A6
 weeks%7C7
+%C2%A6
 months%7C8
 year
@@ -739,16 +739,17 @@
 months%7C8
+%C2%A6
 years) (
@@ -986,12 +986,11 @@
 urn 
-fals
+tru
 e;%0A%09

Fix time from timespan