package com.github.niwaniwa.we.core.command.twitter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.scheduler.BukkitRunnable;

import com.github.niwaniwa.we.core.WhiteEggCore;
import com.github.niwaniwa.we.core.command.abstracts.AbstractWhiteEggCoreCommand;
import com.github.niwaniwa.we.core.command.abstracts.ConsoleCancellable;
import com.github.niwaniwa.we.core.player.WhiteCommandSender;
import com.github.niwaniwa.we.core.player.WhitePlayer;
import com.github.niwaniwa.we.core.twitter.TwitterManager;
import com.github.niwaniwa.we.core.util.clickable.ChatExtra;
import com.github.niwaniwa.we.core.util.clickable.ChatFormat;
import com.github.niwaniwa.we.core.util.clickable.ClickEventType;
import com.github.niwaniwa.we.core.util.clickable.Clickable;
import com.github.niwaniwa.we.core.util.clickable.HoverEventType;
import com.github.niwaniwa.we.core.util.message.LanguageType;

public class WhiteEggTwitterRegisterCommand extends AbstractWhiteEggCoreCommand implements ConsoleCancellable {

	private final String key = commandMessageKey + ".twitter.register";
	private final String permission = commandPermission + ".twitter.register";
	private WeakReference<Boolean> isSuccess = null;

	@Override
	public boolean onCommand(final WhiteCommandSender sender, final Command cmd, final String label,
			final String[] args) {
		if(!sender.hasPermission(permission)){
			sender.sendMessage(msg.getMessage(sender, error_Permission, "", true));
			return true;
		}
		final WhitePlayer player = (WhitePlayer) sender;
		final TwitterManager tw = player.getTwitterManager();
		if(tw.getAccessToken() != null){
			//
			return true;
		}
		if(args.length == 0){
			// message
			this.sendURL(player);
		} else if(args.length == 1){
			if(args[0].equalsIgnoreCase("reset")){
				tw.reset();
				sender.sendMessage("りせっとしました");
				return true;
			}
			new BukkitRunnable() {
				@Override
				public void run() {
					isSuccess = new WeakReference<Boolean>(tw.OAuthAccess(args[0]));
				}
			}.runTaskAsynchronously(WhiteEggCore.getInstance());
			new BukkitRunnable() {
				@Override
				public void run() {
					if(isSuccess  == null
							|| isSuccess.get() == false){
						sender.sendMessage(msg.getMessage(player, key + ".failure", msgPrefix, true)); // success
					} else {
						sender.sendMessage(msg.getMessage(player, key + ".success", msgPrefix, true)); // failure
					}
				}
			}.runTaskLater(WhiteEggCore.getInstance(), 2 * 20);
			return true;
		}
		return true;
	}

	private void sendURL(WhitePlayer p){
		// 要修正
		List<ChatFormat> f = new ArrayList<>();
		f.add(ChatFormat.BOLD);
		Clickable click = new Clickable("Click -->", ChatColor.GOLD, f);
		ChatExtra extra = new ChatExtra("Open URL", ChatColor.GRAY, f);
		extra.setClickEvent(ClickEventType.OPEN_URL, p.getTwitterManager().getOAuthRequestURL());
		extra.setHoverEvent(HoverEventType.SHOW_TEXT, "§bTwitter OAuth Request URL");
		click.addExtra(extra);
		click.send(p.getPlayer());
	}

	@Override
	public void sendUsing(WhitePlayer sender) {
		sender.sendMessage("&7----- &6/tweet &7-----");
		sender.sendMessage("&6/register &f: &7"
				+ msg.getMessage(sender, key + ".using.line_1", "", true));
		sender.sendMessage("&6/register <pin> &f: &7"
				+ msg.getMessage(sender, key + ".using.line_2", "", true));
	}

	@Override
	public String getPermission() {
		return permission;
	}

	@Override
	public String description() {
		return msg.getMessage(LanguageType.en_US, key + ".description", "", true);
	}

	@Override
	public String description(WhiteCommandSender sender) {
		return msg.getMessage(sender, key + ".description", "", true);
	}

}

@@ -51,44 +51,8 @@
 r;%0A%0A
-import java.lang.ref.WeakReference;%0A
 impo
@@ -166,52 +166,8 @@
 and;
-%0Aimport org.bukkit.scheduler.BukkitRunnable;
 %0A%0Aim
@@ -199,28 +199,28 @@
 we.core.
-WhiteEggCore
+api.Callback
 ;%0Aimport
@@ -1184,58 +1184,8 @@
 er%22;
-%0A%09private WeakReference%3CBoolean%3E isSuccess = null;
 %0A%0A%09@
@@ -1722,341 +1722,46 @@
 %0A%09%09%09
-if(args%5B0%5D.equalsIgnoreCase(%22reset%22))%7B%0A%09%09%09%09tw.reset();%0A%09%09%09%09sender.sendMessage(%22%E3%82%8A%E3%81%9B%E3%81%A3%E3%81%A8%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F%22);%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%09new BukkitRunnable() %7B%0A%09%09%09%09@Override%0A%09%09%09%09public void run() %7B%0A%09%09%09%09%09isSuccess = new WeakReference%3CBoolean%3E(tw.OAuthAccess(args%5B0%5D));%0A%09%09%09%09%7D%0A%09%09%09%7D.runTaskAsynchronously(WhiteEggCore.getInstance());%0A%09%09%09new BukkitRunnable
+tw.OAuthAccess(args%5B0%5D, new Callback
 ()
- 
 %7B%0A%09%09
@@ -1792,53 +1792,47 @@
 oid 
-run() %7B%0A%09%09%09%09%09if(isSuccess  == null%0A%09
+onTwitter(Boolean isSuccess) %7B%0A
 %09%09%09%09%09
-%09%7C%7C 
+if(
 isSu
@@ -1840,23 +1840,8 @@
 cess
-.get() == false
 )%7B%0A%09
@@ -1944,18 +1944,24 @@
 %09%09%09%09
-%7D else %7B%0A%09
+%09return;%0A%09%09%09%09%09%7D%0A
 %09%09%09%09
@@ -2059,69 +2059,14 @@
 %09%09%09%09
-%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D.runTaskLater(WhiteEggCore.getInstance(), 2 * 20
+%7D%0A%09%09%09%7D
 );%0A%09

Remove BukkitRunnable code