/*
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package chrislo27.oldbot.bots.baristabot2.commands.normal;


import chrislo27.oldbot.bots.Bot;
import chrislo27.oldbot.bots.baristabot2.Baristron;
import chrislo27.oldbot.bots.baristabot2.Permission;
import chrislo27.oldbot.command.Command;
import chrislo27.oldbot.command.CommandData;
import chrislo27.oldbot.command.CommandResponse;
import chrislo27.oldbot.util.Utils;
import sx.blah.discord.util.MessageBuilder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class TimeTravelCommand extends Command<Baristron> {
	public TimeTravelCommand(Permission perm, String... aliases) {
		super(perm, aliases);
	}

	@Override
	public MessageBuilder execute(CommandData<Baristron> commandData) {
		long randTime = (long) (System.currentTimeMillis() +
				(Utils.lerp(-1, 1, Utils.random.nextDouble()) * 1000 * 60 * 60 * 24 * 365.25 * 100));

		if (commandData.getArgs().size() >= 1) {
			try {
				long l = Long.parseLong(commandData.getArgs().get(0));

				randTime = l * 1000;
			} catch (NumberFormatException e) {
				return CommandResponse.withAutoDeleteMessage(Bot.getNewBuilder(commandData.getChannel())
						.withContent("Not a number! Are you sure you used seconds?"), Bot.AUTO_DELETE_TIME);
			}
		}

		Bot.sendMessage(Bot.getNewBuilder(commandData.getChannel()).appendContent(
				commandData.getUser().mention() + " Travelling to `" +
						new SimpleDateFormat("EEEE, MMMM d, yyyy HH:mm:ss").format(new Date(randTime)) + "`"));
		return null;
	}

	@Override
	public void getCommandHelp(Map<String, String> map, CommandData commandData) {
		map.put("", "Time travel to a random date and time ±100 years");
		map.put("[seconds]",
				"Time travel to the time provided, which is seconds after epoch (January 1, 1970, midnight)");
	}
}
