<?xml version="1.0" encoding="utf-8"?>
<components:MessagePanel 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.*"
                         width="400"
                         height="0"
                         visible="false"
                         messageType="{Message.QUESTION}"
                         title="Identifikation auf {this.source} erforderlich"
                         creationComplete="onCreationComplete(event)"
                         showEffect="{showEffect}"
                         hideEffect="{hideEffect}">
  
  <fx:Metadata>
    /**
     * A dialog prompting the user for authentification data
     * 
     * @author Mathias Brodala
     */
  </fx:Metadata>
  
  <fx:Declarations>
    <s:Resize
      id="showEffect"
      target="{this}"
      heightTo="150"
      duration="250"/>
    <s:Resize
      id="hideEffect"
      target="{this}"
      heightTo="0"
      duration="250"/>
  </fx:Declarations>
  
  <fx:Script>
    <![CDATA[
      import com.adobe.crypto.SHA1;
      
      import de.dtele.control.MediaManager;
      import de.dtele.control.events.CredentialsEvent;
      import de.dtele.data.Assets;
      import de.dtele.data.Credentials;
      import de.dtele.data.ISource;
      import de.dtele.messages.Message;
      import de.dtele.net.MediaRequest;
      
      import mx.events.FlexEvent;
      
      [Bindable]protected var source:ISource;
      
      [Bindable]protected var request:MediaRequest;
      
      [Bindable]protected var callback:Function;
      
      protected function infoTextDataProvider(request:MediaRequest):String {
        
        switch (request.type) {
          
          case MediaRequest.LIST:
            return "Zum Auflisten der Ressourcen dieser Quelle " +
                   "ist eine Identifikation erforderlich.";
            
          case MediaRequest.ADD:
            return "Zum Hinzufügen von Ressourcen zu dieser Quelle " +
                   "ist eine Identifikation erforderlich.";
            
          case MediaRequest.REMOVE:
            return "Zum Entfernen von Ressourcen von dieser Quelle " +
                   "ist eine Identifikation erforderlich.";
            
          default:
            return "Für den Zugriff auf diese Quelle " +
                   "ist eine Identifikation erforderlich.";
        }
      }
      
      protected function onCreationComplete(event:FlexEvent):void {
       
        MediaManager.instance.addEventListener(CredentialsEvent.REQUEST, this.onCredentialsRequest);
      }
      
      /**
       * Invokes the credentials prompt upon request
       */
      protected function onCredentialsRequest(e:CredentialsEvent):void {
        
        this.source = e.source;
        this.request = e.request;
        this.callback = e.callback;
        
        this.enabled = true;
        this.visible = true;
        
        e.preventDefault();
      }

      protected function onApplyButtonClick(event:MouseEvent):void {
        
        this.enabled = false;
        this.visible = false;
        
        if (this.usernameInput.text && this.passwordInput.text) {
        
          this.callback(new Credentials(
            this.usernameInput.text,
            SHA1.hash(this.passwordInput.text)
          ));
          
          this.usernameInput.text = null;
          this.passwordInput.text = null;
        }
        
        this.source = null;
        this.request = null;
        this.callback = null;
      }
      
      protected function onCancelButtonClick(event:MouseEvent):void {
        
        this.enabled = false;
        this.visible = false;
        
        this.source = null;
        this.request = null;
        this.callback = null;
      }

    ]]>
  </fx:Script>
  
  <s:VGroup
    right="10"
    left="10"
    top="10"
    bottom="10"
    gap="5">
    <s:Label
      id="infoText"
      width="100%"
      fontSize="11"
      text="{this.infoTextDataProvider(this.request)}"/>
    <s:HGroup
      width="100%"
      gap="5">
      <s:TextInput
        id="usernameInput"
        prompt="Nutzername"
        width="50%"
        paddingTop="7"
        paddingBottom="7"/>
      <s:TextInput
        id="passwordInput"
        prompt="Passwort"
        displayAsPassword="true"
        width="50%"
        paddingTop="7"
        paddingBottom="7"/>
    </s:HGroup>
    <mx:Spacer
      height="5"/>
    <s:HGroup
      horizontalAlign="right"
      width="100%">
      <s:Button
        id="applyButton"
        label="Übernehmen"
        icon="{Assets.AcceptImage}"
        height="25"
        click="onApplyButtonClick(event)"/>
      <s:Button
        id="cancelButton"
        label="Abbrechen"
        icon="{Assets.CancelImage}"
        height="25"
        click="onCancelButtonClick(event)"/>
    </s:HGroup>
  </s:VGroup>
  
</components:MessagePanel>