<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:ns="AfcsNameSpace"
    layout="horizontal"
    creationComplete="connect()"
     viewSourceURL="srcview/index.html">

    <mx:Script>
        <![CDATA[
            import com.adobe.rtc.sharedManagers.StreamManager;
            import com.adobe.rtc.events.StreamEvent;
            import com.adobe.rtc.events.CollectionNodeEvent;
            import com.adobe.rtc.pods.FileShare;
            import com.adobe.rtc.pods.WebCamera;
            import com.adobe.rtc.events.SessionEvent;
            import com.adobe.rtc.sharedManagers.descriptors.StreamDescriptor;
            import com.adobe.rtc.authentication.AdobeHSAuthenticator;
            import com.adobe.rtc.session.ConnectSession;

            /*
            * informations de connexion
            */
            private var login:String = "VOTRE_LOGIN" ;
            private var password:String = "VOTRE_MOT_DE_PASSE";
            private var salonURL:String = "URL_DU_SALON";
            
            [Bindable]
            private var sessionManager:ConnectSession ; // gestionnaire de session

            private var wCam:WebCamera;
            private var fShare:FileShare ;


            /*
            * ouverture d'une session de connexion à un salon
            */
            private function connect():void
            {
                sessionManager = new ConnectSession();

                // classe d'identification
                var identificator:AdobeHSAuthenticator = new AdobeHSAuthenticator();

                identificator.userName = login;
                identificator.password = password;

                sessionManager.authenticator = identificator;
                sessionManager.roomURL = salonURL;

                sessionManager.addEventListener(SessionEvent.SYNCHRONIZATION_CHANGE , onSynchro );

                sessionManager.login();
            }


            // une fois connecté
            private function onSynchro( e:SessionEvent ):void
            {
                if ( sessionManager.isSynchronized )
                {
                    // initialisation de l'interface » récupération et afficahge des données partagées
                    initUI();
                }
            }


            // récupére les infos des différents Managers et ajoute le pod WebCamera
            private function initUI():void
            {
                updateRoomInfos();

                updateUserInfo();

                updateStreamInfo();
                sessionManager.streamManager.addEventListener( StreamEvent.STREAM_DELETE , updateStreamInfo );

                updateFileInfo();

                addWebCam();
            }


            // Etat du salon : récupération d'infos sur IConnectSession.roomManager
            private function updateRoomInfos():void
            {
                var etatSalon:String =
                    "nom : " + sessionManager.roomManager.roomName + "\n" +
                    "URL : " + sessionManager.roomManager.roomURL + "\n" +
                    "etat : " + sessionManager.roomManager.roomState + "\n" +
                    "bandwidth : " + sessionManager.roomManager.bandwidth + "\n\n" +
                    "autopromote : " + sessionManager.roomManager.autoPromote + "\n..." ;

                chpEtatSalon.text = etatSalon ;
            }


            // récupération de la liste des utilisateurs connectés » IConnectSession.userManager
            private function updateUserInfo( e : Event = null ):void
            {
                userList.dataProvider = sessionManager.userManager.userCollection ;
            }


            // récupération de la liste des flux en diffusion » IConnectSession.streamManager
            private function updateStreamInfo( e : Event = null ):void
            {
                streamList.dataProvider = sessionManager.streamManager.getStreamDescriptors();

                chpNbStream.text = "Il y a " + getNbFlux().toString() + " flux diffusé(s).";
            }


            // récupération de la liste des fichiers partagés » IConnectSession.fileManager
            private function updateFileInfo( e : Event = null ):void
            {
                fileList.dataProvider = sessionManager.fileManager.fileDescriptors;
                chpNbFile.text = "Nombre de fichiers partagés : " + sessionManager.fileManager.fileDescriptors.length ;
            }


            // ajoute un pod Webcam
            private function addWebCam():void
            {
                wCam = new WebCamera();
                wCam.connectSession = sessionManager;
                wCam.width = 220;

                wCam.addEventListener( StreamEvent.STREAM_RECEIVE , updateStreamInfo );

                streamPanel.addChildAt( wCam , 0 );
            }


            // renvoie la description d'un flux pour affichage dans la liste
            // » nom du diffuseur + type de flux
            private function getStreamDescription( o:Object ):String
            {
                var stream:StreamDescriptor = o as StreamDescriptor;

                var streamLabel:String = sessionManager.userManager.getUserDescriptor( stream.initiatorID ).displayName + ' / ' ;
                streamLabel    += stream.type == StreamManager.CAMERA_STREAM ? 'video' : 'audio' ;

                return streamLabel ;
            }

            // récupére le nombre de flux
            private function getNbFlux():uint
            {
                if ( sessionManager.isSynchronized )
                {
                    return sessionManager.streamManager.getStreamDescriptors().length;
                }
                return 0;
            }

        ]]>
    </mx:Script>

    <mx:Panel title="Salon" width="250" height="100%">
        <mx:Text id="chpEtatSalon" width="100%" minWidth="0" />
    </mx:Panel>

    <mx:Panel title="Utilisateurs" width="250" height="100%">
        <mx:List id="userList" labelField="displayName" width="100%" />

    </mx:Panel>

    <mx:Panel id="streamPanel" title="Flux" width="250" height="100%">
        <!--<ns:WebCamera id="wCam"/>-->

        <mx:Label id="chpNbStream" text="Nombre de flux diffusés : {getNbFlux()}" />
        <mx:List id="streamList" width="100%" labelFunction="{ getStreamDescription }" /> <!--"-->
    </mx:Panel>

    <mx:Panel id="filePanel" title="Fichiers" width="250" height="100%">
        <mx:Label id="chpNbFile" />

        <mx:List id="fileList" width="100%" labelField="filename" />
    </mx:Panel>

</mx:Application>