<?xml version="1.0" encoding="utf-8"?>
<s:Group 
    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:screens="screens.*"
    horizontalCenter="0"
    verticalCenter="0"
    
    creationComplete="_creationCompleteHandler(event)"
    >
    
    <s:states>
        <s:State name="waitingVote" />
        <s:State name="voteInProgress" stateGroups="_voting"/>
        <s:State name="waitingVoteEnd" stateGroups="_voting"/>
        <s:State name="voteResults" />
    </s:states>
    
    <fx:Script>
        <![CDATA[
            import controllers.VoteController;
            
            import events.VoteEvent;
            
            import models.VoteDefinition;
            
            import mx.events.FlexEvent;
            
            [Bindable]protected var currentVoteDefinition:VoteDefinition;
            
           
            

            protected function _creationCompleteHandler(event:FlexEvent):void
            {
                var voteCtrl:VoteController = VoteController.instance;
                voteCtrl.addEventListener(VoteEvent.START_VOTE, onVoteStarted, false,0,true);
                voteCtrl.addEventListener(VoteEvent.STOP_VOTE, onVoteStopped, false,0,true);
                voteCtrl.addEventListener(VoteEvent.SHOW_RESULTS, onShowResults, false,0,true);
            }
            
            protected function btnSubmitVote_clickHandler(event:MouseEvent):void
            {
                // Collect our answers then send them
                var answers:Array = [];
                if (labelVote1.selected) answers.push(1);
                if (labelVote2.selected) answers.push(2);
                if (labelVote3.selected) answers.push(3);
                if (labelVote4.selected) answers.push(4);
                if (labelVote5.selected) answers.push(5);
                
                // Send our vote
                VoteController.instance.submitAnswers(answers);
                
                // We can no longer edit our vote
                onVoteStopped();
            }
            
            
            protected function onVoteStarted(event:VoteEvent):void
            {
                var voteDefinition:VoteDefinition = event.data as VoteDefinition;
                if (voteDefinition == null) return;
                currentVoteDefinition = voteDefinition;
                // By default, no answer is checked 
                if (labelVote1 != null)
                {
                    labelVote1.selected = false;
                    labelVote2.selected = false;
                    labelVote3.selected = false;
                    labelVote4.selected = false;
                    labelVote5.selected = false;
                }
                currentState = 'voteInProgress';
            }
            
            
            protected function onVoteStopped(event:VoteEvent=null):void
            {
                currentState = 'waitingVoteEnd';
            }
            
            
            protected function onShowResults(event:VoteEvent):void
            {
                currentState = 'voteResults';
                results.showResults();
            }
            
        ]]>
    </fx:Script>
    
    <fx:Declarations />
    
    <!--- Waiting vote info -->
    <s:Group 
        includeIn="waitingVote" 
        horizontalCenter="0" verticalCenter="0"
        >
        <s:Label text="waiting vote..." />
    </s:Group>
    
    <!--- Vote -->
    <s:Group 
        includeIn="_voting" 
        horizontalCenter="0" verticalCenter="0"
        >
        <s:layout>
            <s:VerticalLayout />
        </s:layout>
        
        <s:Label id="labelVote0" text="{currentVoteDefinition.question}" 
                 fontSize="20" width="100%" />
        
        <!-- In a real-world developpement, the results would be made of 
        composants and note repeated like this. I've chosen to use this very 
        basic checkbox repetition to focus on the network dialogs. -->
        
        <s:CheckBox id="labelVote1" 
                    enabled="{this.currentState == 'voteInProgress'}" 
                    label="{currentVoteDefinition.answer1}"
                    visible="{currentVoteDefinition.answer1 != null}"
                    styleName="answer" fontSize="14" width="100%"
                    />
        
        <s:CheckBox id="labelVote2" 
                    enabled="{this.currentState == 'voteInProgress'}" 
                    label="{currentVoteDefinition.answer2}"
                    visible="{currentVoteDefinition.answer2 != null}"
                    styleName="answer" fontSize="14" width="100%" 
                    />
        
        <s:CheckBox id="labelVote3" 
                    enabled="{this.currentState == 'voteInProgress'}" 
                    label="{currentVoteDefinition.answer3}"
                    visible="{currentVoteDefinition.answer3 != null}"
                    styleName="answer" fontSize="14" width="100%"
                    />
        
        <s:CheckBox id="labelVote4" 
                    enabled="{this.currentState == 'voteInProgress'}" 
                    label="{currentVoteDefinition.answer4}"
                    visible="{currentVoteDefinition.answer4 != null}"
                    styleName="answer" fontSize="14" width="100%"
                    />
        
        <s:CheckBox id="labelVote5" 
                    enabled="{this.currentState == 'voteInProgress'}" 
                    label="{currentVoteDefinition.answer5}"
                    visible="{currentVoteDefinition.answer5 != null}"
                    styleName="answer" fontSize="14" width="100%"
                    />
        
        <s:Button 
            id="btnSubmitVote"
            label="Submit"  width="250" height="40"
            label.waitingVoteEnd="No more answers allowed"
            enabled="{this.currentState == 'voteInProgress'}"
            click="btnSubmitVote_clickHandler(event)"
            />
    </s:Group>
    
    <!--- Vote results -->
    <screens:Vote_Results 
        id="results"
        includeIn="voteResults" />
    
</s:Group>