/*
 * 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.
 */

import java.awt.Color;

import ilog.views.IlvGraphic;
import ilog.views.IlvPoint;
import ilog.views.graphic.IlvGraphicSet;
import ilog.views.maps.IlvCoordinate;
import ilog.views.maps.IlvMapFeature;
import ilog.views.maps.IlvMapGeometry;
import ilog.views.maps.IlvMapRenderException;
import ilog.views.maps.geometry.IlvMapCurve;
import ilog.views.maps.geometry.IlvMapLineString;
import ilog.views.maps.geometry.IlvMapMultiCurve;
import ilog.views.maps.projection.IlvProjectionException;
import ilog.views.maps.projection.IlvProjectionUtil;
import ilog.views.maps.rendering.IlvDefaultCurveRenderer;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;

/**
 * A renderer that takes curve geometries and transforms them into road or
 * railroad graphical objects.
 */
public class IlvRailroadRenderer extends IlvDefaultCurveRenderer {
  IlvRailroadAttributes _attributes;
  // Dummy colors just to show how to customize indexed properties.
  Color[] _dummy = new Color[3]; // here we define an array of 3 colors, but you
                                 // can create
  // and unlimited array and handle the size and the allocation of the array in
  // the get/set
  // methods.

  public IlvRailroadRenderer() {
    super();
  }

  Override
  public IlvGraphic makeGraphic(IlvMapFeature feature, IlvCoordinateTransformation tr)
      throws IlvMapRenderException, IlvCoordinateTransformationException {
    IlvMapGeometry geometry = feature.getGeometry();
    if (geometry == null)
      throw new IllegalArgumentException("No geometry");
    if (geometry instanceof IlvMapMultiCurve)
      return createGraphicFromMultiCurve(feature, tr);
    if (geometry instanceof IlvMapCurve)
      return createGraphicFromCurve(feature, tr);
    throw new IlvMapRenderException("Not an IlvMapCurve geometry");
  }

  IlvGraphic createGraphicFromMultiCurve(IlvMapFeature feature, IlvCoordinateTransformation tr)
      throws IlvMapRenderException, IlvCoordinateTransformationException {
    IlvMapMultiCurve multiCurve = (IlvMapMultiCurve) feature.getGeometry();
    int count = multiCurve.getCardinal();
    if (count == 0)
      throw new IllegalArgumentException("MultiCurve with no curve");

    if (count == 1) {
      feature.setGeometry(multiCurve.getCurve(0));
      IlvGraphic g = makeGraphic(feature, tr);
      feature.setGeometry(multiCurve);
      return g;
    }

    IlvGraphicSet result = new IlvGraphicSet();
    for (int i = 0; i < count; i++) {
      IlvMapCurve curve = multiCurve.getCurve(i);
      feature.setGeometry(curve);
      IlvGraphic g = makeGraphic(feature, tr);
      if (g != null)
        result.addObject(g, false);
    }
    feature.setGeometry(multiCurve);
    return result;
  }

  IlvGraphic createGraphicFromCurve(IlvMapFeature feature, IlvCoordinateTransformation tr)
      throws IlvMapRenderException, IlvProjectionException, IlvCoordinateTransformationException {
    IlvMapGeometry geometry = feature.getGeometry();
    IlvMapLineString ls = (IlvMapLineString) geometry;
    int count = ls.getPointCount();
    if (count == 0)
      return null;
    IlvPoint[] p = makePoints(tr, ls, count);
    IlvRailroadAttributes ra = getAttributes();
    IlvRailroad g = new IlvRailroad(p, false, ra);
    return g;
  }

  IlvPoint[] makePoints(IlvCoordinateTransformation tr, IlvMapLineString ls, int count)
      throws IlvCoordinateTransformationException {
    IlvPoint p[] = new IlvPoint[count];
    for (int i = 0; i < count; i++) {
      p[i] = new IlvPoint();
      IlvCoordinate c = ls.getPoint(i);
      IlvProjectionUtil.ToViews(tr, c, p[i]);
    }
    return p;
  }

  public IlvRailroadAttributes getAttributes() {
    if (_attributes == null)
      _attributes = new IlvRailroadAttributes();
    return _attributes;
  }

  public void setAttributes(IlvRailroadAttributes a) {
    _attributes = a;
  }

  // The "dummy" methods below are here just to show how to use
  // indexed properties with CSS.
  public Color getDummy(int i) {
    return _dummy[i];
  }

  public Color[] getDummy() {
    return _dummy;
  }

  public void setDummy(Color[] val) {
    _dummy = val;
  }

  public void setDummy(int i, Color val) {
    _dummy[i] = val;
  }

}