package brn.sim.scenario.txdor;

import java.util.List;

import click.sim.builder.ClickBuilder;

import brn.sim.AbstractParams;


public class TxDOrClickSim extends TxDOrSim {

  /*
   * (non-Javadoc)
   * @see brn.sim.AbstractDriver#installBuilders(brn.sim.AbstractParams)
   */
  protected void installBuilders(AbstractParams options) {
    super.installBuilders(options);

    builderProvider.addBuilder(new ClickBuilder.Mac());
    builderProvider.addBuilder(new ClickBuilder.Net());
  }

  /* (non-Javadoc)
   * @see brn.sim.AbstractDriver#getSimulationSuite(String version)
   */
  public List getSimulationSuite(String version) {
//    List ret = new ArrayList();
//
//    // TODO use more than one flow
//
//    for (int seed = 1; seed < 4; seed++) {
//      for (int nodes = 25; nodes <= 50; nodes += 25) {
//        for (int stdDev = 4; stdDev <= 8; stdDev += 4) {
//          for (int csSize = 1; csSize <= 4; csSize++) {
//            for (int useRts = 0; useRts <= 1; useRts++) {
//              for (int globFwdTable = 0; globFwdTable <= 1; globFwdTable++) {
//                for (int dataTxDiv = 0; dataTxDiv <= 1; dataTxDiv++) {
//                  for (int ignoreRtsCtsNav = 0; ignoreRtsCtsNav <= 1; ignoreRtsCtsNav++) {
//                    // dataTxDiv only if using rts
//                    if (useRts == 0 && (dataTxDiv == 1 || ignoreRtsCtsNav == 1))
//                      continue;
//                    // dataTxDiv only if using opp. routing
//                    if (csSize == 1
//                        && (dataTxDiv == 1 || globFwdTable == 1 || ignoreRtsCtsNav == 1))
//                      continue;
//
//                    TxDOrClickParams params = (csSize != 1 ?
//                        TxDOrClickParams.defaultParams() : TxDOrClickParams.unicastParams());
//                    params.seed = seed;
//                    params.nodes = nodes;
//
//                    FieldBuilder.FieldParams field = (FieldBuilder.FieldParams) params.field;
//                    field.fieldX = 2000;
//                    field.fieldY = 2000;
//                    field.pathloss = Constants.PATHLOSS_DIST_SHADOWING;
//                    field.exponent = 2.7;
//                    field.stdDeviation = stdDev;
//
//                    RadioBuilder.NoiseParams radio = (RadioBuilder.NoiseParams) params.node.radio;
//                    radio.fieldX = field.fieldX;
//                    radio.fieldY = field.fieldY;
//                    radio.placement = Constants.PLACEMENT_RANDOM;
//                    radio.threshold = .7;
//                    radio.min_connectivity_betweenNodes = 2;
//
//                    MacBuilder.M802_11Params mac = (MacBuilder.M802_11Params) params.node.mac;
//                    mac.thresholdRts = (useRts == 1 ? 500 : 3000);
//                    if (csSize > 1) {
//                      ((MacBuilder.M802_11TxDivParams)mac).useDataTxDiv = (dataTxDiv == 1 ? true : false);
//                      ((MacBuilder.M802_11TxDivParams)mac).ignoreRtsCtsNav = (ignoreRtsCtsNav == 1 ? true : false);
//                    }
//
//                    if (csSize != 1) {
//                      RouteBuilder.TxDORParams route = (RouteBuilder.TxDORParams) params.node.route;
//                      route.candidateSetSize = csSize;
//                      route.globalForwarderTable = (globFwdTable == 1);
//                    }
//
//                    ret.add(TxDOrClickParams.setParamsBatch(params));
//                  }
//                }
//              }
//            }
//          }
//        }
//      }
//    }
//
//    return ret;
    return null;
  }

//  public static class SimulationResult {
//    int simulationId;
//
//    int seed;
//    int nodes;
//    double stdDev;
//    int csSize;
//    boolean globFwdTable;
//    boolean useRts;
//    boolean dataTxDiv;
//    boolean ignoreRtsCtsNav;
//
//    public double avgThroughput;
//    public double avgDelay;
//    public double sendPackets;
//    public double recvPackets;
//
//    public static String getHeader() {
//      StringBuilder builder = new StringBuilder();
//      builder.append("simulationId").append("\t");
//
//      builder.append("seed").append("\t");
//      builder.append("nodes").append("\t");
//      builder.append("stdDev").append("\t");
//      builder.append("csSize").append("\t");
//      builder.append("globFwdTable").append("\t");
//      builder.append("useRts").append("\t");
//      builder.append("dataTxDiv").append("\t");
//      builder.append("ignoreRtsCtsNav").append("\t");
//
//      builder.append("avgThroughput").append("\t");
//      builder.append("avgDelay").append("\t");
//      builder.append("sendPackets").append("\t");
//      builder.append("recvPackets").append("\t");
//      return builder.toString();
//    }
//
//    public String toString() {
//      StringBuilder builder = new StringBuilder();
//      builder.append(simulationId).append("\t");
//
//      builder.append(seed).append("\t");
//      builder.append(nodes).append("\t");
//      builder.append(stdDev).append("\t");
//      builder.append(csSize).append("\t");
//      builder.append(globFwdTable).append("\t");
//      builder.append(useRts).append("\t");
//      builder.append(dataTxDiv).append("\t");
//      builder.append(ignoreRtsCtsNav).append("\t");
//
//      builder.append(avgThroughput).append("\t");
//      builder.append(avgDelay).append("\t");
//      builder.append(sendPackets).append("\t");
//      builder.append(recvPackets).append("\t");
//      return builder.toString();
//    }
//  }
//
//  public static void evalSimulationResults(String dbUrl, String dbUser, String dbPasswd) throws Exception {
//    DbBinaryLoader loader = new DbBinaryLoader(dbUrl, dbUser, dbPasswd);
//    List lstIds = loader.loadSimulationIds();
//    String configPath = "Global, Config";
//
//    List results = new LinkedList();
//
//    for(int i = 0; i < lstIds.size(); i++) {
//      SimulationResult result = new SimulationResult();
//      result.simulationId = ((Integer)lstIds.get(i)).intValue();
//
//      TxDOrClickParams params = (TxDOrClickParams) loader.load(result.simulationId, configPath);
//      if (null == params) {
//        System.err.println("Object with path " + configPath + " not found for id " + result.simulationId);
//        continue;
//      }
//      result.seed = params.seed;
//      result.nodes = params.nodes;
//      result.stdDev = ((FieldBuilder.FieldParams)params.field).stdDeviation;
//      result.csSize = 1;
//      result.globFwdTable = false;
//      if (params.node.route instanceof RouteBuilder.TxDORParams) {
//        result.csSize = ((RouteBuilder.TxDORParams)params.node.route).candidateSetSize;
//        result.globFwdTable = ((RouteBuilder.TxDORParams)params.node.route).globalForwarderTable;
//      }
//      result.useRts = ((MacBuilder.M802_11Params)params.node.mac).thresholdRts < 1000;
//      result.dataTxDiv = false;
//      result.ignoreRtsCtsNav = false;
//      if (params.node.mac instanceof MacBuilder.M802_11TxDivParams) {
//        result.dataTxDiv = ((MacBuilder.M802_11TxDivParams)params.node.mac).useDataTxDiv;
//        result.ignoreRtsCtsNav = ((MacBuilder.M802_11TxDivParams)params.node.mac).ignoreRtsCtsNav;
//      }
//
//      String path = "All flows, Flow stats";
//      PropertiesData data = (PropertiesData) loader.load(result.simulationId, path);
//      FlowStats stats = (FlowStats) data.getProperties();
//      if (null == stats) {
//        System.err.println("Object with path " + path + " not found for id " + result.simulationId);
//        continue;
//      }
//      result.avgThroughput = stats.getAvgThroughput();
//      result.avgDelay = stats.getAvgDelay();
//      result.sendPackets = stats.getSendPackets();
//      result.recvPackets = stats.getRecvPackets();
//
//      results.add(result);
//      System.out.print(".");
//    }
//
//    System.out.println(SimulationResult.getHeader());
//    for (int i = 0; i < results.size(); i++) {
//      System.out.println(((SimulationResult)results.get(i)).toString());
//    }
//  }


  protected void runLocal() {
    TxDOrClickParams params = TxDOrClickParams.defaultParams(new TxDOrClickParams());
    params.setParamsTest();

    params.seed = 2;
    params.nodes = 4;
    params.dumpMac = true;


//    params.transParams = new TransBuilder.TcpParams();
//    AppBuilder.TcpParams tcpParams = new AppBuilder.TcpParams();
//    tcpParams.tcpBytes = tcpParams.tcpMSS * 2000;
//    params.trafficParams.flowParams = tcpParams;

//    params.trafficParams.flows = 3;

    try {
      run(params.setParamsTest());
    } catch (Throwable e) {
      e.printStackTrace();
      System.exit(1);
    }
  }

  public static void main(String[] args) throws Throwable {
    TxDOrClickSim test = new TxDOrClickSim();
    test.run(args);
  }
}
