<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:view="com.tonyfendall.mapping.view.*"
               
               viewSourceURL="srcview/index.html"
               
               creationComplete="reset()" >
    
    <s:layout>
        <s:BasicLayout />
    </s:layout>

    <fx:Script>
        <![CDATA[
            import com.tonyfendall.mapping.core.Map;
            import com.tonyfendall.mapping.core.MapUtil;
            
            import flash.utils.clearInterval;
            import flash.utils.setInterval;

            // Map being solved
            private var map:Map;
            
            // Track algorithm iterations
            private var iterator:uint;
            private var iterationCount:uint;
            private const MAX_ITERATIONS:uint = 200;
            
            
            /**
             * Create a new map 
             */
            private function reset():void
            {
                map = MapUtil.buildMap();
                mapView.map = map;
                
                iterationCount = 0;
                iterate(); // Run first iteration to spread points out a bit
            }
            
            
            /**
             * Begin running algorythm iterations
             */
            private function play():void
            {
                if(iterationCount > MAX_ITERATIONS) {
                    reset();
                }
                    
                b_play.enabled = false;
                b_stop.enabled = true;
                b_reset.enabled = false;
                
                iterator = setInterval(iterate, 100); // start iterating every 200ms
            }
            
            
            /**
             * Stop running algorythm iterations
             */
            private function stop():void
            {
                clearInterval(iterator);

                b_play.enabled = true;
                b_stop.enabled = false;
                b_reset.enabled = true;
            }
            

            /**
             * Iterate the algorythm and update the map view
             */
            private function iterate():void
            {
                if(iterationCount > MAX_ITERATIONS) {
                    stop();
                    return;
                }
                
                map.iterate();
                
                mapView.invalidateDisplayList(); // redraw map
    
                errorLabel.text = "Total Error: "+ int(map.getTotalError());
                
                iterationCount++;
            }
            
        ]]>
    </fx:Script>
    
    
    <!-- Buttons -->
    <s:HGroup horizontalCenter="0" >
        <s:Button id="b_play" label="PLAY" click="play()"/>
        <s:Button id="b_stop" label="STOP" click="stop()" enabled="false"/>
        <s:Button id="b_reset" label="RESET" click="reset()"/>
    </s:HGroup>
    
    
    <!-- Border -->
    <s:Rect y="25" width="100%" height="100%" radiusX="4" radiusY="4">
        <s:stroke>
            <s:SolidColorStroke color="#BBBBBB" weight="1" />
        </s:stroke>
    </s:Rect>
    
    
    <!-- Map View -->
    <view:MapView id="mapView" y="25" width="100%" height="100%" />
    
    
    <s:Label id="errorLabel" left="4" bottom="4" fontWeight="bold"/>
    
</s:Application>