// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.actions;

import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
import static org.openstreetmap.josm.tools.I18n.tr;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JOptionPane;
import org.openstreetmap.josm.actions.mapmode.DrawAction;
import org.openstreetmap.josm.data.preferences.BooleanProperty;
import org.openstreetmap.josm.gui.Notification;
import org.openstreetmap.josm.gui.util.GuiHelper;
import org.openstreetmap.josm.tools.Shortcut;

/**
 * This action toggles automatic moving of the map view to last placed node
 * @since 3837
 */
public class ViewportFollowToggleAction extends ToggleAction {

    /**
     * Defines if a notification should be displayed after enabling and disabling
     */
    public static final BooleanProperty PROP_NOTIFICATION = new BooleanProperty("viewportfollow.notification", true);

    /**
     * Constructs a new {@code ViewportFollowToggleAction}.
     */
    public ViewportFollowToggleAction() {
        super(tr("Viewport Following"),
              "viewport-follow",
              tr("Enable/disable automatic moving of the map view to last placed node"),
              Shortcut.registerShortcut("menu:view:viewportfollow", tr("View: {0}", tr("Viewport Following")),
              KeyEvent.VK_F, Shortcut.CTRL_SHIFT),
              true /* register shortcut */
        );
        setHelpId(ht("/Action/ViewportFollowing"));
        setSelected(DrawAction.VIEWPORT_FOLLOWING.get());
        notifySelectedState();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!ExpertToggleAction.isExpert()) {
            // #16848 (Possible to activate "Viewport following" mode when not in "Expert mode" through keyboard shortcut)
            return;
        }
        toggleSelectedState(e);
        DrawAction.VIEWPORT_FOLLOWING.put(isSelected());
        if (!getShortcut().getKeyText().isEmpty() && PROP_NOTIFICATION.get()) {
            String msg = isSelected()
                    ? tr("Viewport following is enabled, press {0} to disable it", getShortcut().getKeyText())
                    : tr("Viewport following is disabled");
            GuiHelper.runInEDT(() -> new Notification(msg).setIcon(JOptionPane.INFORMATION_MESSAGE).show());
        }
        notifySelectedState();
    }

    @Override
    protected boolean listenToSelectionChange() {
        return false;
    }

    @Override
    protected void updateEnabledState() {
        setEnabled(getLayerManager().getEditDataSet() != null);
    }
}
