/*
 * This file is part of gitg
 *
 * Copyright (C) 2014 - Jesse van den Kieboom
 *
 * gitg 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 2 of the License, or
 * (at your option) any later version.
 *
 * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
 */

namespace Gitg
{

class RefActionDelete : GitgExt.UIElement, GitgExt.Action, GitgExt.RefAction, Object
{
	// Do this to pull in config.h before glib.h (for gettext...)
	private const string version = Gitg.Config.VERSION;

	public GitgExt.Application? application { owned get; construct set; }
	public GitgExt.RefActionInterface action_interface { get; construct set; }
	public Gitg.Ref reference { get; construct set; }

	public RefActionDelete(GitgExt.Application        application,
	                       GitgExt.RefActionInterface action_interface,
	                       Gitg.Ref                   reference)
	{
		Object(application:      application,
		       action_interface: action_interface,
		       reference:        reference);
	}

	public string id
	{
		owned get { return "/org/gnome/gitg/ref-actions/delete"; }
	}

	public string display_name
	{
		owned get { return _("Delete…"); }
	}

	public string description
	{
		owned get { return _("Delete the selected reference"); }
	}

	public bool enabled
	{
		get
		{
			return (reference.is_branch() && !((Ggit.Branch)reference).is_head())
			       || reference.is_tag() || reference.is_remote();
		}
	}

	public void activate()
	{
		var query = new GitgExt.UserQuery();

		var name = reference.get_shorthand();

		if (reference.is_branch())
		{
			query.title = _("Confirm branch deletion");
			query.message = (_("Are you sure that you want to permanently delete the branch <b><i>“%s”</i></b>?")).printf(name);
			query.default_is_destructive = true;
			query.message_use_markup = true;
		}
		else if (reference.is_tag())
		{
			query.title = _("Confirm tag deletion");
			query.message = (_("Are you sure that you want to permanently delete the tag <b><i>“%s”</i></b>?")).printf(name);
			query.default_is_destructive = true;
			query.message_use_markup = true;
		}
		else
		{
			query.title = _("Confirm remote branch deletion");
			query.message = (_("Are you sure that you want to permanently delete the remote branch <b><i>“%s”</i></b>?")).printf(name);
			query.default_is_destructive = true;
			query.message_use_markup = true;
		}

		query.set_responses(new GitgExt.UserQueryResponse[] {
			new GitgExt.UserQueryResponse(_("Cancel"), Gtk.ResponseType.CANCEL),
			new GitgExt.UserQueryResponse(_("Delete"), Gtk.ResponseType.OK)
		});

		query.default_response = Gtk.ResponseType.OK;
		query.response.connect(on_response);

		action_interface.application.user_query(query);
	}

	private bool on_response(Gtk.ResponseType response)
	{
		if (response != Gtk.ResponseType.OK)
		{
			return true;
		}

		try
		{
			if (reference.is_remote())
			{
				var remote_name = reference.parsed_name.remote_name;
				var remote = application.remote_lookup.lookup(remote_name);
				var remote_branch = "refs/heads/%s".printf(reference.parsed_name.remote_branch);

				delete_remote_branch.begin(remote, remote_branch, (obj, res) => {
					delete_remote_branch.end(res);
				});
			}
			else
				reference.delete();
		}
		catch (Error e)
		{
			string title;
			string message;

			var name = reference.get_shorthand();

			if (reference.is_tag())
			{
				// Translators: %s is the name of the tag
				title = _("Failed to delete tag %s").printf(name);

				// Translators: the first %s is the name of the tag, the second is an error message
				message = _("The tag %s could not be deleted: %s").printf(name, e.message);
			}
			else if (reference.is_branch())
			{
				// Translators: %s is the name of the branch
				title = _("Failed to delete branch %s").printf(name);

				// Translators: the first %s is the name of the branch, the second is an error message
				message = _("The branch %s could not be deleted: %s").printf(name, e.message);
			}
			else /*if (reference.is_remote())*/
			{
				// Translators: %s is the name of the branch
				title = _("Failed to delete remote branch %s").printf(name);

				// Translators: the first %s is the name of the branch, the second is an error message
				message = _("The remote branch %s could not be deleted: %s").printf(name, e.message);
			}

			action_interface.application.show_infobar(title,
			                                          message,
			                                          Gtk.MessageType.ERROR);

			return true;
		}

		action_interface.remove_ref(reference);
		action_interface.refresh();
		return true;
	}

	public async bool delete_remote_branch(Gitg.Remote remote, string remote_branch)
	{
		var notification = new RemoteNotification(remote);
		application.notifications.add(notification);

		notification.text = _("Deleting branch %s from %s").printf(remote_branch, remote.get_url());

		try
		{
			yield remote.push(false, "", remote_branch, null);
			((Gtk.ApplicationWindow)application).activate_action("reload", null);
		}
		catch (Error e)
		{
			notification.error(_("Failed to delete branch %s from %s: %s")
							   .printf(remote_branch, remote.get_url(), e.message));
			stderr.printf("Failed to delete: %s\n", e.message);

			return false;
		}

		/* Translators: the %s will get replaced with the remote url, */
		notification.success(_("Deleted from %s").printf(remote.get_url()));

		return true;
	}
}

}

// ex:set ts=4 noet
