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

import static org.openstreetmap.josm.tools.I18n.marktr;
import static org.openstreetmap.josm.tools.I18n.tr;

import java.util.Collection;
import java.util.Objects;

import javax.swing.Icon;

import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.tools.CheckParameterUtil;
import org.openstreetmap.josm.tools.ImageProvider;

/**
 * Command that basically replaces one OSM primitive by another of the same type.
 *
 * @since 93
 */
public class ChangeCommand extends Command {

    private final OsmPrimitive osm;
    private final OsmPrimitive newOsm;

    /**
     * Constructs a new {@code ChangeCommand} in the context of {@code osm} data set.
     * @param osm The existing primitive to modify. It must belong to a data set
     * @param newOsm The new primitive
     * @throws IllegalArgumentException if sanity checks fail
     */
    public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
        this(osm.getDataSet(), osm, newOsm);
    }

    /**
     * Constructs a new {@code ChangeCommand} in the context of a given data set.
     * @param data The data set
     * @param osm The existing primitive to modify
     * @param newOsm The new primitive
     * @throws IllegalArgumentException if sanity checks fail
     * @since 11240
     */
    public ChangeCommand(DataSet data, OsmPrimitive osm, OsmPrimitive newOsm) {
        super(data);
        this.osm = osm;
        this.newOsm = newOsm;
        sanityChecks();
    }

    private void sanityChecks() {
        CheckParameterUtil.ensureParameterNotNull(osm, "osm");
        CheckParameterUtil.ensureParameterNotNull(newOsm, "newOsm");
        if (newOsm instanceof Way && ((Way) newOsm).isEmpty()) {
            // Do not allow to create empty ways (see #7465)
            throw new IllegalArgumentException(tr("New way {0} has 0 nodes", newOsm));
        }
    }

    @Override
    public boolean executeCommand() {
        super.executeCommand();
        osm.cloneFrom(newOsm);
        osm.setModified(true);
        return true;
    }

    @Override
    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
        modified.add(osm);
    }

    @Override
    public String getDescriptionText() {
        String msg;
        switch (OsmPrimitiveType.from(osm)) {
        case NODE: msg = marktr("Change node {0}"); break;
        case WAY: msg = marktr("Change way {0}"); break;
        case RELATION: msg = marktr("Change relation {0}"); break;
        default: throw new AssertionError();
        }
        return tr(msg, osm.getDisplayName(DefaultNameFormatter.getInstance()));
    }

    @Override
    public Icon getDescriptionIcon() {
        return ImageProvider.get(osm.getDisplayType());
    }

    /**
     * Returns the original OSM primitive to modify. It belongs to a dataset.
     * @return the original OSM primitive to modify
     * @since 14283
     */
    public final OsmPrimitive getOsmPrimitive() {
        return osm;
    }

    /**
     * Returns the new OSM primitive.
     * @return the new OSM primitive
     * @since 14283
     */
    public final OsmPrimitive getNewOsmPrimitive() {
        return newOsm;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), osm, newOsm);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        if (!super.equals(obj)) return false;
        ChangeCommand that = (ChangeCommand) obj;
        return Objects.equals(osm, that.osm) &&
                Objects.equals(newOsm, that.newOsm);
    }
}
