package team.unstudio.udpl.util;

import java.lang.reflect.InvocationTargetException;

import org.bukkit.entity.Player;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers.TitleAction;

import com.comphenix.protocol.wrappers.WrappedChatComponent;

public final class Title {
	
	private Title(){}
	
	private static final ProtocolManager PROTOCOL_MANAGER = ProtocolLibrary.getProtocolManager();
	
	public static Result title(Player player, String title, String subTitle, int fadeIn, int stay, int fadeOut){
		Result result = setTimeAndDisplay(player, fadeIn, stay, fadeOut);
		if(result.isFailure())
			return result;
		result = title(player, title);
		if(result.isFailure())
			return result;
		result = subTitle(player, subTitle);
		if(result.isFailure())
			return result;
		return Result.success();
	}
	
	public static Result title(Player player, String title){
		PacketContainer container = PROTOCOL_MANAGER.createPacket(PacketType.Play.Server.TITLE);
		container.getTitleActions().write(0, TitleAction.TITLE);
		container.getChatComponents().write(0, WrappedChatComponent.fromText(title));
		try {
			PROTOCOL_MANAGER.sendServerPacket(player, container);
			return Result.success();
		} catch (InvocationTargetException e) {
			return Result.failure(e);
		}
	}
	
	public static Result subTitle(Player player, String subTitle){
		PacketContainer container = PROTOCOL_MANAGER.createPacket(PacketType.Play.Server.TITLE);
		container.getTitleActions().write(0, TitleAction.SUBTITLE);
		container.getChatComponents().write(0, WrappedChatComponent.fromText(subTitle));
		try {
			PROTOCOL_MANAGER.sendServerPacket(player, container);
			return Result.success();
		} catch (InvocationTargetException e) {
			return Result.failure(e);
		}
	}
	
	public static Result setTimeAndDisplay(Player player, int fadeIn, int stay, int fadeOut){
		PacketContainer container = PROTOCOL_MANAGER.createPacket(PacketType.Play.Server.TITLE);
		container.getTitleActions().write(0, TitleAction.TIMES);
		container.getIntegers().write(0, fadeIn).write(1, stay).write(2, fadeOut);
		try {
			PROTOCOL_MANAGER.sendServerPacket(player, container);
			return Result.success();
		} catch (InvocationTargetException e) {
			return Result.failure(e);
		}
	}
	
	public static Result hide(Player player){
		PacketContainer container = PROTOCOL_MANAGER.createPacket(PacketType.Play.Server.TITLE);
		container.getTitleActions().write(0, TitleAction.CLEAR);
		try {
			PROTOCOL_MANAGER.sendServerPacket(player, container);
			return Result.success();
		} catch (InvocationTargetException e) {
			return Result.failure(e);
		}
	}
	
	public static Result reset(Player player){
		PacketContainer container = PROTOCOL_MANAGER.createPacket(PacketType.Play.Server.TITLE);
		container.getTitleActions().write(0, TitleAction.RESET);
		try {
			PROTOCOL_MANAGER.sendServerPacket(player, container);
			return Result.success();
		} catch (InvocationTargetException e) {
			return Result.failure(e);
		}
	}
}

@@ -80,24 +80,61 @@
 ception;%0D%0A%0D%0A
+import javax.annotation.Nullable;%0D%0A%0D%0A
 import org.b
@@ -413,24 +413,62 @@
 tleAction;%0D%0A
+import com.google.common.base.Strings;
 %0D%0Aimport com
@@ -708,32 +708,42 @@
 e(Player player,
+ @Nullable
  String title, S
@@ -740,16 +740,26 @@
 g title,
+ @Nullable
  String 
@@ -913,32 +913,75 @@
 turn result;%0D%0A%09%09
+%0D%0A%09%09if(!Strings.isNullOrEmpty(title))%7B%0D%0A%09%09%09
 result = title(p
@@ -988,32 +988,33 @@
 layer, title);%0D%0A
+%09
 %09%09if(result.isFa
@@ -1018,32 +1018,33 @@
 sFailure())%0D%0A%09%09%09
+%09
 return result;%0D%0A
@@ -1039,24 +1039,75 @@
 rn result;%0D%0A
+%09%09%7D%0D%0A%09%09%0D%0A%09%09if(!Strings.isNullOrEmpty(subTitle))%7B%0D%0A%09
 %09%09result = s
@@ -1132,24 +1132,25 @@
 bTitle);%0D%0A%09%09
+%09
 if(result.is
@@ -1156,32 +1156,33 @@
 sFailure())%0D%0A%09%09%09
+%09
 return result;%0D%0A
@@ -1181,16 +1181,25 @@
 esult;%0D%0A
+%09%09%7D%0D%0A%09%09%0D%0A
 %09%09return

Update Title