<?xml version="1.0"?>
<!--
 This file is part of FlightGear, the free flight simulator
 http://www.flightgear.org/

 Copyright (C) 2010 Torsten Dreyer, Torsten (at) t3r _dot_ de

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
 published by the Free Software Foundation; either version 2 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.
-->

<PropertyList>
  <name>scenario</name>
  <modal>false</modal>
  <layout>vbox</layout>
  <resizable>false</resizable>
  <default-padding>3</default-padding>

  <group>
    <layout>hbox</layout>
    <default-padding>1</default-padding>

    <empty><stretch>true</stretch></empty>

    <text>
      <label>AI Scenario Select</label>
    </text>

    <empty><stretch>true</stretch></empty>

    <button>
      <legend></legend>
      <key>Esc</key>
      <pref-width>16</pref-width>
      <pref-height>16</pref-height>
      <border>2</border>
      <binding>
        <command>dialog-close</command>
      </binding>
    </button>
  </group>

  <hrule/>

  <text>
    <label>Choose active scenario(s) for the next program start</label>
  </text>

  <hrule/>


  <group>
    <layout>hbox</layout>

    <group>
      <name>left-column</name>
      <layout>vbox</layout>
    </group>

    <group>
      <layout>vbox</layout>
      <name>right-column</name>
    </group>

  </group>

  <hrule/>

  <group>
    <layout>hbox</layout>
      <halign>center</halign>

    <button>
      <legend>OK</legend>
      <equal>true</equal>
      <binding>
        <command>dialog-apply</command>
      </binding>
      <binding>
        <command>nasal</command>
        <script>apply()</script>
      </binding>
     <binding>
        <command>reinit</command>
        <subsystem>ai_model</subsystem>
      </binding>
      <binding>
        <command>dialog-close</command>
      </binding>
    </button>

<!--
    <button>
      <legend>Apply</legend>
      <equal>true</equal>
      <binding>
        <command>dialog-apply</command>
      </binding>
      <binding>
        <command>nasal</command>
        <script>apply()</script>
      </binding>
     <binding>
        <command>reinit</command>
        <subsystem>ai_model</subsystem>
      </binding>
    </button>
-->

    <button>
      <legend>Cancel</legend>
      <equal>true</equal>
      <key>Esc</key>
      <binding>
        <command>dialog-close</command>
      </binding>
    </button>

    <empty><stretch>true</stretch></empty>
  </group>

  <nasal>
    <open><![CDATA[
      var dlg_root = cmdarg();

      var findElementByName = func(rootN, name) {
        foreach( var child; rootN.getChildren() ) {
          var n = child.getNode( "name" );
          if( n != nil and n.getValue() == name )
            return child;
          var f = findElementByName(child,name);
          if( f != nil ) return f;
        }
        return nil;
      };

      var isEnabledScenario = func(scenario) {
        foreach( var n; props.globals.getNode("sim/ai",1).getChildren("scenario") )
          if( n.getValue() == scenario )
            return 1;
        return 0;
      };

      var columns = [ "left-column", "right-column" ];
      var processScenario = func(nr,file, rootN) {
        var scenarioN = rootN.getNode("scenario");
        if( scenarioN == nil ) return;
        var descriptionN = rootN.getNode("description");
        if( descriptionN == nil )
          var descriptionN = scenarioN.getNode("description");
        var description = descriptionN != nil ? descriptionN.getValue() : "";
        var propertyRoot = props.globals.getNode("sim/gui/dialogs/scenario",1).getChild( "scenario", nr, 1 );
        propertyRoot.getNode("selected",1).setBoolValue(isEnabledScenario(file));
        propertyRoot.getNode("name",1).setValue(file);

        var group = findElementByName( dlg_root, columns[math.mod(nr,2)] ).getChild("group", nr, 1 );
        group.getNode("layout",1).setValue("hbox");
        var cb = group.getNode("checkbox",1);
        cb.getNode("property",1).setValue(propertyRoot.getNode("selected").getPath());
        cb.getNode("label",1).setValue(file);
        cb.getNode("name",1).setValue(file);
        group.getNode("empty",1).getNode("stretch",1).setValue("true");
      }

      var apply = func {
        var targetRoot = props.globals.getNode("sim/ai",1);
        targetRoot.removeChildren("scenario");
        var i = 0;
        foreach( var src; props.globals.getNode("sim/gui/dialogs/scenario",1).getChildren( "scenario" ) ) {
          if( src.getNode("selected",1).getValue() ) {
            var scnNode = targetRoot.getChild("scenario", i, 1 );
            scnNode.setAttribute("userarchive",1);
            scnNode.setValue( src.getNode("name",1).getValue() );
            i+=1;
          }
        }
      }

      var path = getprop("/sim/fg-root") ~ "/AI";
      var i = -1;
      foreach(var file; sort(directory(path), cmp))
        if(size(file) > 4 and substr(file, -4) == ".xml")
          processScenario( i+=1, substr(file,0,size(file)-4), io.read_properties(path ~ "/" ~ file) );

    ]]></open>

    <close><![CDATA[
    ]]></close>

  </nasal>
</PropertyList>