/*
 * 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.BorderLayout;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import ilog.views.IlvManagerView;
import ilog.views.maps.IlvMapUtil;
import ilog.views.maps.beans.IlvFeatureSelectorPanel;
import ilog.views.maps.datasource.IlvMapDataSource;
import ilog.views.maps.datasource.IlvTigerDataSource;

/**
 * Class that manages TIGER/Line import action.
 */
public class TigerLoadAction extends ImportAction {

  IlvFeatureSelectorPanel panel;
  TilingParameterPanel tilingPanel;
  JPanel compositePanel;

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

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

  /**
   * @see plugins.ImportAction#getDataSources(java.lang.String[])
   */
  Override
  public Enumeration<IlvMapDataSource> getDataSources(String fileNames[]) throws IOException {
    return getDS(fileNames);
  }

  /**
   * @see plugins.ImportAction#getDataSources(URL[])
   */
  Override
  public Enumeration<IlvMapDataSource> getDataSources(URL urls[]) throws IOException {
    return getDS(urls);
  }

  /**
   * @see plugins.ImportAction#getDataSources(java.lang.String[])
   */
  private Enumeration<IlvMapDataSource> getDS(Object fileNames[]) throws IOException {
    Vector<IlvMapDataSource> src = new Vector<IlvMapDataSource>();
    IlvFeatureSelectorPanel.Feature[] features = panel.getSelectedFeatures();
    // String[] faccCodes = new String[features.length];
    List<String> cfccCodes = new ArrayList<String>();
    if (features != null) {
      for (int j = 0; j < features.length; j++) {
        IlvFeatureSelectorPanel.Feature currentFeature = features[j];
        IlvFeatureSelectorPanel.Feature currentChild;
        int nChildren = currentFeature.getChildCount();
        if (nChildren != 0) {// major
          for (int i = 0; i < currentFeature.getChildCount(); i++) {
            currentChild = (IlvFeatureSelectorPanel.Feature) currentFeature.getChildAt(i);
            cfccCodes.add(currentChild.getMajorCode() + currentChild.getMinorCode());
          }
        } else {
          cfccCodes.add(currentFeature.getMajorCode() + currentFeature.getMinorCode());
        }
      }
    }
    for (int i = 0; i < fileNames.length; i++) {
      if (fileNames[i].toString().toLowerCase().endsWith(".zip")) { //$NON-NLS-1$
        String base = fileNames[i].toString();
        int lastSlash = fileNames[i].toString().lastIndexOf("\\");//$NON-NLS-1$
        if (lastSlash != -1) {
          base = base.substring(lastSlash + 1, base.length() - 4);
        }
        fileNames[i] = "jar:" + new File(fileNames[i].toString()).toURI().toURL() + "/!/" + base.toUpperCase() + ".RT1";//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
      }
      IlvTigerDataSource source;
      if (fileNames[i] instanceof URL) {
        source = new IlvTigerDataSource((URL) fileNames[i]);
      } else {
        source = new IlvTigerDataSource(fileNames[i].toString());
      }
      source.setCFCCCodeList((String[]) cfccCodes.toArray(new String[0]));
      source.setTilingParameters(tilingPanel.isTiling(), tilingPanel.getRows(), tilingPanel.getColumns());
      source.setAreaOfinterest(tilingPanel.getLonMin(), tilingPanel.getLatMin(), tilingPanel.getLonMax(),
          tilingPanel.getLatMax());
      source.setManager(getView().getManager());
      source.setName(getFormatName() + " (" + new File(fileNames[i].toString()).getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
      src.add(source);
    }
    return src.elements();
  }

  /**
   * @see plugins.ImportAction#approveSelection(File [])
   */
  Override
  public boolean approveSelection(File selection[]) {
    IlvFeatureSelectorPanel.Feature[] features = panel.getSelectedFeatures();
    if (features == null || features.length == 0) {
      JOptionPane.showMessageDialog(view,
          IlvMapUtil.getString(TigerLoadAction.class, "TigerLoadAction.NoFeatureMessage"), //$NON-NLS-1$
          IlvMapUtil.getString(TigerLoadAction.class, "TigerLoadAction.NoFeatureTitle"), //$NON-NLS-1$
          JOptionPane.ERROR_MESSAGE);
      return false;
    }
    return super.approveSelection(selection);
  }

  /**
   * @see plugins.ImportAction#getExtensions()
   */
  Override
  public String[] getExtensions() {
    return new String[] { ".rt1", ".zip" }; //$NON-NLS-1$ //$NON-NLS-2$
  }

  /**
   * Returns an IlvFeatureSelectorPanel
   * 
   * @see plugins.ImportAction#getAccessory()
   */
  Override
  public JComponent getAccessory() {
    String filename = "TigerFeaturesEN.xml"; //$NON-NLS-1$
    if (compositePanel == null) {
      compositePanel = new JPanel(new BorderLayout());
      panel = new IlvFeatureSelectorPanel(null, filename, "Tiger");//$NON-NLS-1$
      JScrollPane sp = new JScrollPane(panel);
      compositePanel.add(sp, BorderLayout.CENTER);
      tilingPanel = new TilingParameterPanel(getView(), false);
      compositePanel.add(tilingPanel, BorderLayout.SOUTH);
    }
    return compositePanel;
  }

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