/*
 * Copyright (c) 2006-2015 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.osd;

import com.dmdirc.FrameContainer;
import com.dmdirc.commandparser.BaseCommandInfo;
import com.dmdirc.commandparser.CommandArguments;
import com.dmdirc.commandparser.CommandType;
import com.dmdirc.commandparser.commands.Command;
import com.dmdirc.commandparser.commands.IntelligentCommand;
import com.dmdirc.commandparser.commands.context.CommandContext;
import com.dmdirc.interfaces.CommandController;
import com.dmdirc.ui.input.AdditionalTabTargets;
import com.dmdirc.ui.messages.Styliser;

import javax.annotation.Nonnull;

/**
 * The osd command shows an on screen message.
 */
public class OsdCommand extends Command implements IntelligentCommand {

    /** A command info object for this command. */
    public static final BaseCommandInfo INFO = new BaseCommandInfo("osd",
            "osd --close - closes all OSD windows\n"
            + "osd [--timeout <delay in seconds>] <message> - show the "
            + "specified message in an OSD window",
            CommandType.TYPE_GLOBAL);
    /** The OSDManager that this command should use. */
    private final OsdManager osdManager;

    /**
     * Creates a new instance of OsdCommand.
     *
     * @param controller The controller to use for command information.
     * @param osdManager OSD Manager used to control OSD windows
     */
    public OsdCommand(final CommandController controller, final OsdManager osdManager) {
        super(controller);

        this.osdManager = osdManager;
    }

    /**
     * Used to show a notification using this plugin.
     *
     * @param timeout Timeout for the OSD window. If negative then the value from the config will be
     *                used
     * @param title   Title of dialog if applicable
     * @param message Message to show
     *
     * @return True if the notification was shown.
     */
    public boolean showOSD(final int timeout, final String title,
            final String message) {
        osdManager.showWindow(timeout, Styliser.stipControlCodes(message));
        return true;
    }

    @Override
    public void execute(@Nonnull final FrameContainer origin,
            final CommandArguments args, final CommandContext context) {
        if (args.getArguments().length > 0
                && "--close".equalsIgnoreCase(args.getArguments()[0])) {
            osdManager.closeAll();
        } else if (args.getArguments().length > 0
                && "--timeout".equalsIgnoreCase(args.getArguments()[0])) {
            if (args.getArguments().length < 2) {
                sendLine(origin, args.isSilent(), FORMAT_ERROR, "You "
                        + "must specify a valid number for the OSD timeout.");
                return;
            }

            try {
                showOSD(Integer.parseInt(args.getArguments()[1]), null,
                        args.getArgumentsAsString(2));
            } catch (NumberFormatException ex) {
                sendLine(origin, args.isSilent(), FORMAT_ERROR, "You "
                        + "must specify a valid number for the OSD timeout.");
            }
        } else {
            showOSD(-1, null, args.getArgumentsAsString());
        }
    }

    @Override
    public AdditionalTabTargets getSuggestions(final int arg,
            final IntelligentCommandContext context) {
        final AdditionalTabTargets res = new AdditionalTabTargets();

        if (arg == 0) {
            res.add("--close");
            res.add("--timeout");
        } else if (arg > 0 && context.getPreviousArgs().get(0)
                .equals("--close")) {
            res.excludeAll();
        }

        return res;
    }

}
