/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

package plugins;

import java.awt.geom.Rectangle2D;
import java.io.File;
import java.net.URL;
import java.security.AccessControlException;

import javax.swing.JComponent;

import calibration.BasicImageAccessory;
import ilog.views.IlvManagerView;
import ilog.views.maps.IlvMapUtil;
import ilog.views.maps.format.image.IlvRasterBasicImageReader;
import ilog.views.maps.raster.IlvRasterAbstractReader;
import ilog.views.maps.raster.IlvRasterProperties;
import ilog.views.maps.srs.coordtrans.IlvMathTransform;

/**
 * Class that manages GIF,JPG and TIFF import action.
 */
public class BasicImageLoadAction extends DefaultLoadAction {
  boolean isTiled = true;
  BasicImageAccessory _ac = new BasicImageAccessory(false);

  private BasicImageAccessory getAc() {
    if (_ac == null) {
      _ac = new BasicImageAccessory(false);
    }
    return _ac;
  }

  /**
   * Creates an action for the specified view
   * 
   * @param view
   *          view to add map data to.
   */
  public BasicImageLoadAction(IlvManagerView view) {
    super(view);
  }

  /**
   * @see plugins.ImportAction#getSubDirectory()
   */
  Override
  protected String getSubDirectory() {
    return IlvMapUtil.getString(getClass(), "BasicImageLoadAction.CDDirectory"); //$NON-NLS-1$
  }

  private URL lastURL;
  private String lastFileName;

  /**
   * @see plugins.DefaultLoadAction#makeReader(java.lang.String)
   */
  Override
  public IlvRasterAbstractReader makeReader(String fileName) {
    // JV-5476 : make sure we do not reuse the exact same reader, or we cause
    // problem when one of them is removed from the map.
    if (fileName.equals(lastFileName)) {
      getAc().setFile(new File(fileName));
    }
    lastFileName = fileName;
    return makeReader();
  }

  /**
   * @see plugins.DefaultLoadAction#makeReader(URL)
   */
  Override
  public IlvRasterAbstractReader makeReader(URL url) {
    // JV-5476 : make sure we do not reuse the exact same reader, or we cause
    // problem when one of them is removed from the map.
    if (url.equals(lastURL)) {
      getAc().setURL(url);
    }
    lastURL = url;
    return makeReader();
  }

  private IlvRasterAbstractReader makeReader() {
    BasicImageAccessory ac = getAc();
    IlvRasterBasicImageReader r = ac.getReader();
    if (r == null) {
      r = new IlvRasterBasicImageReader();
      r.addMap(ac.getFile().toString());
      Rectangle2D.Double e = r.getImageBounds(0);
      ac.getModel().getControlPoint(0).setIconX(e.x);
      ac.getModel().getControlPoint(0).setIconY(e.y);
      ac.getModel().getControlPoint(1).setIconX(e.x + e.width);
      ac.getModel().getControlPoint(1).setIconY(e.y + e.height);
      IlvRasterProperties p = r.getRasterProperties(0);
      p.setTileWidth(p.getNumColumns());
      p.setTileHeight(p.getNumLines());
    }
    IlvMathTransform trans = ac.getModel().makeTransform();
    if (trans == null) {
      Rectangle2D bounds = ac.getModel().getLatLonBounds();
      r.setImageBounds(0, bounds.getMinX(), bounds.getMaxY(), bounds.getMaxX(), bounds.getMinY()); // in
                                                                                                   // kernel
    } else {
      r.setInternalTransformation(trans);
    }
    return r;
  }

  /**
   * @see plugins.ImportAction#selectedFileChanged(java.io.File)
   */
  Override
  public void selectedFileChanged(File file) {
    boolean dir = false;
    try {
      if (file != null)
        dir = file.isDirectory();
    } catch (AccessControlException e) {
      // applets.
    }
    if (file != null && accept(file) && !dir) {
      getAc().setFile(file);
    } else {
      getAc().setFile(null);
    }
  }

  /**
   * @see plugins.ImportAction#selectedURLChanged(URL)
   */
  Override
  public void selectedURLChanged(URL url) {
    if (url != null && accept(new File(url.getFile()))) {
      getAc().setURL(url);
    } else {
      getAc().setURL(null);
    }
  }

  /**
   * Returns a BasicImageAccessory.
   * 
   * @see plugins.ImportAction#getAccessory()
   */
  Override
  public JComponent getAccessory() {
    getAc().setView(getView());
    return getAc();
  }

  /**
   * @see plugins.DefaultLoadAction#getExtensions()
   */
  Override
  public String[] getExtensions() {
    return new String[] { ".gif", //$NON-NLS-1$
        ".tif", //$NON-NLS-1$
        ".png", //$NON-NLS-1$
        ".jpg", //$NON-NLS-1$
        ".jpeg" //$NON-NLS-1$
    };
  }

  /**
   * Returns false.
   * 
   * @see plugins.ImportAction#isMultiSelectionEnabled()
   */
  Override
  public boolean isMultiSelectionEnabled() {
    return false;
  }

  /**
   * @see plugins.ImportAction#getFormatName()
   */
  Override
  public String getFormatName() {
    return ilog.views.maps.IlvMapUtil.getString(getClass(), "BasicImageLoadAction.FormatName"); //$NON-NLS-1$
  }
}