<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer 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:components="de.dtele.ui.components.*"
                autoDrawBackground="false"
                width="400"
                height.inactive="1"
                enabled.inactive="false"
                currentState="inactive"
                creationComplete="onCreationComplete(event)">
  
  <fx:Metadata>
    /**
     * An item renderer for messages
     * 
     * @author Mathias Brodala
     */
  </fx:Metadata>
  
  <s:transitions>
    <s:Transition fromState="inactive" toState="normal">
      <s:Resize target="{this}" duration="250" effectEnd="onShowEffectEnd(event)"/>
    </s:Transition>
    <s:Transition fromState="normal" toState="inactive">
      <s:Resize target="{this}" duration="250" effectEnd="onHideEffectEnd(event)"/>
    </s:Transition>
  </s:transitions>
  
  <fx:Script>
    <![CDATA[
      
      import de.dtele.data.Assets;
      import de.dtele.messages.Message;
      import de.dtele.messages.MessageAction;
      import de.dtele.messages.MessageManager;
      
      import mx.events.EffectEvent;
      import mx.events.FlexEvent;

      
      protected var removeTimer:Timer = new Timer(5000, 1);
      
      /**
       * Data provider for icons of message actions
       * 
       * @param action The action to retrieve the icon for
       * @return The icon class or null if the action type is unknown
       */
      protected function actionIconDataProvider(action:MessageAction):Object {
        
        switch (action.type) {
          
          case MessageAction.APPLY:
          case MessageAction.OK:
            
            return Assets.AcceptImage;
          
          case MessageAction.CANCEL:
            
            return Assets.CancelImage;
          
          case MessageAction.CLOSE:
            
            return Assets.CloseImage;
          
          case MessageAction.RETRY:
            
            return Assets.RetryImage;
        }
        
        return null;
      }
      
      /**
       * Data provider for human readable labels of message actions
       * 
       * @param action The action to build the label for
       * @return The label or the message action itself if unknown
       */
      protected function actionLabelDataProvider(action:MessageAction):String {
        
        switch (action.type) {
          
          case MessageAction.APPLY:
            
            return "Anwenden";
          
          case MessageAction.CANCEL:
            
            return "Abbrechen";
          
          case MessageAction.CLOSE:
            
            return "Schließen";
          
          case MessageAction.OK:
            
            return "OK";
          
          case MessageAction.RETRY:
            
            return "Wiederholen";
        }
        
        return action.type;
      }
      
      /**
       * Limits the returned states to the required ones
       */
      protected override function getCurrentRendererState():String {
        
        if (this.down) {
          
          return "down";
        }
        
        if (this.hovered) {
          
          return "hovered";
        }
        
        return this.currentState;
      }
      
      /**
       * Prepares the remove timer and switches the state to normal
       */
      protected function onCreationComplete(event:FlexEvent):void {
        
        this.removeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onTimerComplete);
        
        this.currentState = "normal";
      }

      /**
       * Disables the close button and switches the state to inactive
       */
      protected function onCloseButtonClick(e:MouseEvent):void {
        
        this.removeTimer.stop();
        this.currentState = "inactive";
      }
      
      /**
       * Sets the message response based on the user selection
       */
      protected function onActionButtonClick(e:MouseEvent):void {
        
        var button:MessageActionButton = e.target as MessageActionButton;
        var message:Message = this.data as Message;
        
        message.response = button.messageAction.response;
        
        // Prevent other actions
        this.enabled = false;
      }
      
      /**
       * Disables the close button and starts the remove effect
       */
      protected function onTimerComplete(e:TimerEvent):void {
        
        this.currentState = "inactive";
      }
      
      /**
       * Starts the remove timer if appropriate.
       * 
       * Only uncritical messages will be removed automatically.
       * The same goes for progress messages which have been completed
       */
      protected function onShowEffectEnd(e:EffectEvent):void {
        
        var message:Message = this.data as Message;
        
        switch (message.type) {
          
          case Message.INFO:
            
            this.removeTimer.start();
            break;
          
          case Message.PROGRESS:
            
            message.addEventListener(Event.COMPLETE, this.onProgressMessageComplete);
            message.addEventListener(Event.CANCEL, this.onProgressMessageComplete);
            break;
        }
      }

      /**
       * Removes the message
       */
      protected function onHideEffectEnd(e:EffectEvent):void {
        
        this.enabled = false;
        
        MessageManager.instance.removeMessage(this.data as Message);
      }
      
      /**
       * Prepares removing the message
       */
      protected function onProgressMessageComplete(e:Event):void {
        
        this.removeTimer.start();
      }
      
    ]]>
  </fx:Script>
  
  <s:states>
    <s:State name="inactive"/>
    <s:State name="normal"/>
    <s:State name="hovered"/>
    <s:State name="down"/>
  </s:states>
  
  <components:MessagePanel
    id="message"
    width="100%"
    height="100%"
    minHeight="0"
    title="{data.title}"
    messageType="{data.type}">
    <components:CloseButton
      right="10"
      top="-23"
      includeIn="hovered,down"
      enabled="{data.type != Message.PROGRESS}"
      click="onCloseButtonClick(event)"/>
    <s:VGroup
      left="10"
      right="10"
      top="10"
      bottom="10">
      <s:Label
        fontSize="11"
        text="{data.text}"/>
      <mx:ProgressBar
        id="progressBar"
        visible="{data.type == Message.PROGRESS}"
        label="%3%%"
        labelPlacement="right"
        source="{data}"/>
      <mx:HBox>
        <mx:Repeater
          id="actions"
          dataProvider="{data.actions}">
          <components:MessageActionButton
            messageAction="{actions.currentItem}"
            icon="{actionIconDataProvider(actions.currentItem)}"
            label="{actionLabelDataProvider(actions.currentItem)}"
            height="25"
            click="onActionButtonClick(event)"/>
        </mx:Repeater>
      </mx:HBox>
    </s:VGroup>
  </components:MessagePanel>
  
</s:ItemRenderer>