/*
 * 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 main;

import java.util.Random;

import ilog.views.maps.defense.symbology.app6a.IlvApp6aSymbol;
import ilog.views.sdm.model.IlvDefaultSDMModel;
import ilog.views.sdm.model.IlvDefaultSDMNode;
import ilog.views.sdm.model.IlvSDMNode;

/**
 * Specific SDM Model that simulates a battle field composed of hiearchical
 * armies.
 */
public class BattleField extends IlvDefaultSDMModel {
  Random r = new Random(123);
  final static String affiliations[] = { IlvApp6aSymbol.AFFILIATION_FRIEND, IlvApp6aSymbol.AFFILIATION_HOSTILE,
      IlvApp6aSymbol.AFFILIATION_NEUTRAL, IlvApp6aSymbol.AFFILIATION_UNKNOWN };
  final static String mobilities[] = { "MO", "MQ", "MS", "MY", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
  };
  final static String functions[] = { "UCFHS-", "UCFHL-", "UCFHM-", "UCFHH-" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
  };
  final static String units[] = { "EWAL-", "EWDLS", "EWDMS", "EWDHS" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
  };

  /**
   * Creates a new <code>BattleField</code> structure .
   */
  public BattleField() {
    buildModel();
  }

  /**
   * 
   */
  private void buildModel() {
    setAdjusting(true);
    for (int aff = 0; aff < affiliations.length; aff++) {
      double dxa = r.nextDouble() * 10;
      double dya = r.nextDouble() * 10;
      IlvDefaultSDMNode army = makeGroup("Army " + affiliations[aff], affiliations[aff], "UCFH--", "-K", null); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
      for (int func = 0; func < functions.length; func++) {
        double dxf = r.nextDouble() * 4;
        double dyf = r.nextDouble() * 4;
        IlvDefaultSDMNode division = makeGroup("Division " + functions[func], affiliations[aff], functions[func], "-I", //$NON-NLS-1$ //$NON-NLS-2$
            army);
        for (int mob = 0; mob < mobilities.length; mob++) {
          double dxm = r.nextDouble() * 1;
          double dym = r.nextDouble() * 1;
          IlvDefaultSDMNode regiment = makeGroup("Regiment " + mobilities[mob], affiliations[aff], functions[func], //$NON-NLS-1$
              "-G", division); //$NON-NLS-1$
          for (int j = 1; j < 9; j++) {
            double dx = r.nextDouble() * 0.25;
            double dy = r.nextDouble() * 0.25;
            // JV-3867
            IlvApp6aSymbol s = new IlvApp6aSymbol("SFGPUCFHS---CUS-", Math.toRadians(-7 + dx + dxm + dxf + dxa), //$NON-NLS-1$
                Math.toRadians(45 - 7 + dy + dym + dyf + dya));
            s.setIDAffiliation(affiliations[aff]);
            s.setIDFunction(units[func]);
            s.setIDSizeMobility(mobilities[mob]);
            // use IlvApp6aSymbol.MODIFIER_UNIQUE_DESIGNATION rather than name
            // to have it show on the symbol
            s.setProperty("name", units[func] + mobilities[mob] + "(" + j + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            addObject(s, regiment, null);
          }
        }
      }
    }
    // we indicate the map builder that it is not necessary to ask for saving
    // this data.
    // make sure group is at the right place.
    setAdjusting(false);
  }

  private IlvDefaultSDMNode makeGroup(String name, String aff, String func, String mobility, IlvSDMNode parent) {
    // IlvDefaultSDMNode grp = (IlvDefaultSDMNode)
    // engine.getModel().createNode("group");
    IlvApp6aSymbol grp = new IlvApp6aSymbol("group", "SFGPUCFHS---CUS-", 0, 0); //$NON-NLS-1$//$NON-NLS-2$
    grp.setIDAffiliation(aff);
    grp.setIDFunction(func);
    grp.setIDSizeMobility(mobility);
    // uncomment to show name on the symbol:
    // engine.getModel().setObjectProperty(grp,
    // IlvApp6aSymbol.MODIFIER_UNIQUE_DESIGNATION, name);
    setObjectProperty(grp, "name", name); //$NON-NLS-1$
    addObject(grp, parent, null);
    return grp;
  }

}