<?xml version="1.0"?>

<PropertyList>
  <name>terrasync</name>
    
  <nasal>
      <open>
          <![CDATA[

var computeStatusText = func {
# first, check if the system is actually enabled
    if (!getprop('/sim/terrasync/enabled'))
        return "Downloading disabled";

# only happens if terrasync failed to activate, so
# indicates a problem
    if (!getprop('/sim/terrasync/active'))
        return "Automatic download inactive";

    var errCount = getprop('/sim/terrasync/error-count');
    if (errCount > 0)
        return "Errors occurred during download";

# we need to suggest some remedial action here!
    if (getprop('/sim/terrasync/stalled'))
        return "Downloading has stalled. Check your network connection and settings";

    if (getprop('/sim/terrasync/busy')) {
        var kbytesSec = getprop('/sim/terrasync/transfer-rate-bytes-sec') / 1024;
        return sprintf('Downloading: %dKB/sec', int(kbytesSec));
    }

# we are enabled but not busy, so idle
    return "Ready";
}

# timer function, update summary status string
var updateStatusText = func {
    var s = computeStatusText();
    setprop("/sim/gui/dialogs/terrasync/status", s);

    var msg = "";
    if (getprop("/sim/terrasync/available")) {
        if (getprop("/sim/terrasync/enabled"))
            msg = "Automatic download active; monitor your bandwidth on metered connection.";
    } else {
        if (getprop("/sim/terrasync/intialized"))
            msg = "Automatic download not active.";
        else
            msg = "Automatic download not supported.";
    }
    setprop("/sim/terrasync/ui-message-node", msg);
}

# the TerraSync properties of interest are tied, so we can't use a
# listener. Let's poll while the dialog is open; not ideal but not
# a major problem.
var statusTimer = maketimer(0.4, updateStatusText);     
statusTimer.start();     
updateStatusText();

setprop("/sim/gui/dialogs/terrasync/display-mode", "0");

    ]]>
      </open>
      
      <close>
          statusTimer.stop();
          statusTimer = nil;
      </close>
  
  </nasal>
    
  <layout>vbox</layout>

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

    <empty><stretch>1</stretch></empty>

    <text>
      <label>Automatic Download of Scenery and Data (TerraSync)</label>
    </text>

    <empty><stretch>1</stretch></empty>

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

  <hrule/>

  <text>
    <name>message</name>
    <color>
      <red>1.0</red>
      <green>0.4</green>
      <blue>0.4</blue>
    </color>
    <halign>left</halign>
            <property>/sim/terrasync/ui-message-node</property>
            <live>true</live>
  </text>

  <group>
    <layout>vbox</layout>

    <group>
      <layout>table</layout>
      <halign>left</halign>
      <equal>false</equal>

      <!-- only for a gap -->
      <text>
        <row>0</row>
        <col>0</col>
        <halign>center</halign>
        <label> </label>
      </text>

      <checkbox>
        <name>terrasync-enabled</name>
        <halign>left</halign>
        <row>0</row>
        <col>1</col>
        <colspan>3</colspan>
        <property>/sim/terrasync/enabled</property>
        <label>Enable automatic scenery download</label>
        <live>true</live>
        <binding>
          <command>dialog-apply</command>
          <object-name>terrasync-enabled</object-name>
        </binding>
        <enable>
          <not-equals>
            <property>/sim/terrasync/scenery-dir</property>
            <value></value>
          </not-equals>
        </enable>
      </checkbox>

      <!-- only for a gap -->
      <text>
        <row>1</row>
        <col>1</col>
        <halign>center</halign>
        <label>    </label>
      </text>

      <checkbox>
        <name>ai-data-enabled</name>
        <halign>left</halign>
        <row>1</row>
        <col>2</col>
<!--
        <colspan>2</colspan>
-->
        <property>/sim/terrasync/ai-data-enabled</property>
        <label>Update AI Traffic during FG start</label>
        <live>true</live>
        <binding>
          <command>dialog-apply</command>
          <object-name>ai-data-enabled</object-name>
        </binding>
        <enable>
            <property>/sim/terrasync/enabled</property>
            <property>/sim/traffic-manager/enabled</property>
        </enable>
      </checkbox>

      <button>
        <row>1</row>
        <col>3</col>
        <legend>Update Now</legend>
        <border>2</border>
        <equal>true</equal>
        <enable>
            <property>/sim/terrasync/enabled</property>
            <property>/sim/traffic-manager/enabled</property>
            <property>/sim/terrasync/ai-data-enabled</property>
        </enable>
        <binding>
          <command>property-assign</command>
          <property>/sim/terrasync/ai-data-enabled</property>
          <value>1</value>
        </binding>
        <binding>
          <command>property-assign</command>
          <property>/sim/terrasync/ai-data-update-now</property>
          <value>1</value>
        </binding>
      </button>

<!--      <checkbox>
        <name>other-data-enabled</name>
        <halign>left</halign>
        <row>2</row>
        <col>2</col>
        <colspan>2</colspan>
        <property>/sim/terrasync/other-data-enabled</property>
        <label>Download other data files</label>
        <live>true</live>
        <binding>
          <command>dialog-apply</command>
          <object-name>other-data-enabled</object-name>
        </binding>
        <enable>
          <property>/sim/terrasync/enabled</property>
        </enable>
      </checkbox>
-->
      <text>
        <name>warning_text</name>
        <row>3</row>
        <col>0</col><colspan>4</colspan>
        <color>
          <red>1.0</red>
          <green>0.4</green>
          <blue>0.4</blue>
          <alpha>1</alpha>
        </color>
        <halign>center</halign>
        <label/>
      </text>

    </group>

    <hrule/>

    <group>
      <layout>hbox</layout>
      <halign>left</halign>
      <default-padding>4</default-padding>

      <!-- only for a gap -->
      <text>
        <halign>center</halign>
        <label> </label>
      </text>

      <button>
        <legend>Information</legend>
        <binding>
          <command>property-assign</command>
          <property>/sim/gui/dialogs/terrasync/display-mode</property>
          <value>0</value>
        </binding>
      </button>

      <!-- only for a gap -->
      <text>
        <halign>center</halign>
        <label></label>
      </text>

      <button>
        <legend>  Log  </legend>
        <binding>
          <command>property-assign</command>
          <property>/sim/gui/dialogs/terrasync/display-mode</property>
          <value>1</value>
        </binding>
      </button>
    </group>

    <!-- 'tabbed' group controlled by display-mode property -->
    <group>
      <layout>hbox</layout>
      <halign>left</halign>

      <!-- gap -->
      <text>
        <label></label>
      </text>

      <group>
        <layout>table</layout>
        <halign>fill</halign>
        <valign>fill</valign>
        <default-padding>0</default-padding>

        <group>
          <layout>table</layout>
          <halign>left</halign>
          <valign>top</valign>
          <equal>false</equal>

          <row>0</row>
          <col>0</col>
          <visible>
            <equals>
              <property>/sim/gui/dialogs/terrasync/display-mode</property>
              <value>0</value>
            </equals>
          </visible>

          <!-- status text is generated by Nasal dynamically -->
          <text>
            <row>0</row>
            <col>0</col>
            <colspan>2</colspan>

            <halign>left</halign>
            <pref-width>200</pref-width>
            <format>Status: %s</format>
            <property>/sim/gui/dialogs/terrasync/status</property>
            <live>true</live>
            <color>
              <red>0.7</red>
              <green>0.7</green>
              <blue>0.7</blue>
              <alpha>1</alpha>
            </color>
          </text>

          <text>
            <halign>left</halign>
            <row>1</row>
            <col>0</col>
            <label>KBytes downloaded:</label>
            <color>
              <red>0.7</red>
              <green>0.7</green>
              <blue>0.7</blue>
              <alpha>1</alpha>
            </color>
          </text>
          <text>
            <row>1</row>
            <col>1</col>
            <format>%d</format>
            <property>/sim/terrasync/downloaded-kbytes</property>
            <live>true</live>
            <halign>right</halign>
            <stretch>true</stretch>
            <color>
              <red>0.7</red>
              <green>0.7</green>
              <blue>0.7</blue>
              <alpha>1</alpha>
            </color>
          </text>

          <text>
            <halign>left</halign>
            <row>2</row>
            <col>0</col>
            <label>Processed elements:</label>
            <color>
              <red>0.7</red>
              <green>0.7</green>
              <blue>0.7</blue>
              <alpha>1</alpha>
            </color>
          </text>
          <text>
            <row>2</row>
            <col>1</col>
            <format>%s</format>
            <property>/sim/terrasync/update-count</property>
            <live>true</live>
            <stretch>true</stretch>
            <halign>right</halign>
            <color>
              <red>0.7</red>
              <green>0.7</green>
              <blue>0.7</blue>
              <alpha>1</alpha>
            </color>
          </text>

          <text>
            <halign>left</halign>
            <row>3</row>
            <col>0</col>
            <label>Processed scenery tiles:</label>
            <color>
              <red>0.7</red>
              <green>0.7</green>
              <blue>0.7</blue>
              <alpha>1</alpha>
            </color>
          </text>
          <text>
            <row>3</row>
            <col>1</col>
            <format>%s</format>
            <property>/sim/terrasync/tile-count</property>
            <live>true</live>
            <stretch>true</stretch>
            <halign>right</halign>
            <color>
              <red>0.7</red>
              <green>0.7</green>
              <blue>0.7</blue>
              <alpha>1</alpha>
            </color>
          </text>
        </group>

        <loglist>
          <row>0</row>
          <col>0</col>

          <visible>
            <equals>
              <property>/sim/gui/dialogs/terrasync/display-mode</property>
              <value>1</value>
            </equals>
          </visible>

        	<halign>fill</halign>
        	<valign>fill</valign>
        	<stretch>true</stretch>
        	<pref-width>600</pref-width>
          <pref-height>300</pref-height>
          <logclass>terrasync</logclass>
        	<padding>6</padding>
        </loglist>

      </group>

      <!-- gap -->
      <text>
        <label></label>
      </text>

    </group> <!-- end of tabbed group -->

    <hrule/>

    <group>
      <layout>hbox</layout>
      <default-padding>3</default-padding>
      <empty><stretch>true</stretch></empty>

      <button>
        <legend>Close</legend>
        <border>2</border>
        <equal>true</equal>
        <key>Esc</key>
        <binding>
          <command>dialog-close</command>
        </binding>
      </button>
      <empty><stretch>true</stretch></empty>
    </group>

  </group>

</PropertyList>