<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="initApplication()" width="500" height="470" title="Tutoriel AIR SQLite developpez" viewSourceURL="srcview/index.html">
    
    <mx:Script>
        <![CDATA[
            import GUI.ContactRenderer;
            import mx.collections.ArrayCollection;
            import mx.core.IFlexDisplayObject;
            import mx.managers.PopUpManager;
            import mx.controls.Alert;
            
            import DB.Database;
            import GUI.ContactWindow;
            
            [Bindable]
            private var contactList:ArrayCollection;
            [Bindable]
            private var contactCount:uint;
            [Bindable]
            private var pageItemCount:uint;
            [Bindable]
            private var firstItemIndex:uint;
            [Bindable]
            private var lastItemIndex:uint;
            [Bindable]
            private var hasPreviousItemToDisplay:Boolean;
            [Bindable]
            private var hasNextItemToDisplay:Boolean;
        
            private function initApplication():void
            {
                pageItemCount = 2;
                
                if(!Database.Instance.createDatabase())
                {
                    Alert.show("Erreur lors de la création de la base :\n" + Database.Instance.LastErrorMessage, "Tutoriel SQLite developpez");
                }else{
                    updateList(true);
                    
                    Database.Instance.addEventListener(Database.DATABASE_CHANGE_EVENT_NAME, function(event:Event):void
                    {
                        updateList(true);
                    });
                }
            }
            
            private function addContact():void
            {
                var window:IFlexDisplayObject = PopUpManager.createPopUp(this, ContactWindow , true);
                var CreationWindow:ContactWindow = ContactWindow(window);
                PopUpManager.centerPopUp(CreationWindow);
            }
            
            private function updateList(pUpdateCount:Boolean=false):void
            {
                if(pUpdateCount)
                {
                    contactCount = Database.Instance.getContactCount();
                    firstItemIndex = 0;
                }
                
                var ReturnedList:Array = Database.Instance.getContactList(firstItemIndex, pageItemCount);
                
                if(ReturnedList == null)
                {
                    Alert.show("Erreur lors de la récupération des contacts de la base :\n" + Database.Instance.LastErrorMessage, "Tutoriel SQLite developpez");
                }else{
                    contactList = new ArrayCollection(ReturnedList);
                    
                    lastItemIndex = firstItemIndex + contactList.length;
                    hasNextItemToDisplay = lastItemIndex < contactCount;
                    hasPreviousItemToDisplay = firstItemIndex > 0;
                }
            }
        
            private function firstPage():void
            {
                firstItemIndex = 0;
                updateList();
            }
            
            private function nextPage():void
            {
                firstItemIndex += pageItemCount;
                updateList();
            }
            
            private function previousPage():void
            {
                firstItemIndex -= pageItemCount;
                updateList();
            }
            
            private function lastPage():void
            {
                firstItemIndex = contactCount - contactCount % pageItemCount;
                updateList();
            }
            
        ]]>
    </mx:Script>
    
    <mx:Style>
    .nextButton  
    {  
        up-skin: Embed("GUI/images/Right1.png");  
        down-skin: Embed("GUI/images/Right1.png");  
        over-skin: Embed("GUI/images/Right1.png");  
        disabled-skin: Embed("GUI/images/Right1disabled.png");  
    }
    
    .previousButton  
    {  
        up-skin: Embed("GUI/images/Left1.png");  
        down-skin: Embed("GUI/images/Left1.png");  
        over-skin: Embed("GUI/images/Left1.png");  
        disabled-skin: Embed("GUI/images/Left1disabled.png");  
    }  
      
    .lastButton  
    {  
        up-skin: Embed("GUI/images/Right2.png");  
        down-skin: Embed("GUI/images/Right2.png");  
        over-skin: Embed("GUI/images/Right2.png");  
        disabled-skin: Embed("GUI/images/Right2disabled.png");  
    }  
      
    .firstButton  
    {  
        up-skin: Embed("GUI/images/Left2.png");  
        down-skin: Embed("GUI/images/Left2.png");  
        over-skin: Embed("GUI/images/Left2.png");  
        disabled-skin: Embed("GUI/images/Left2disabled.png");  
    }  

    </mx:Style>
        
    <mx:HBox width="95%">
        <mx:Label text="Liste des contacts :" />
        <mx:Spacer width="100%" />
    </mx:HBox>
    
    <mx:TileList dataProvider="{contactList}" itemRenderer="GUI.ContactRenderer" width="440" height="100%" maxColumns="1" columnWidth="420" />
    
    <mx:ApplicationControlBar width="100%">
        <mx:Button label="Nouveau contact" horizontalCenter="true" click="addContact()" />
        <mx:Label text="Liste :" fontWeight="bold" color="0x000000" visible="{contactList.length > 0}" />
        <mx:Label text="{firstItemIndex+1}-{lastItemIndex} / {contactCount}" color="0x323232" visible="{contactList.length > 0}" />
        <mx:Spacer width="100%" />
        <mx:Button styleName="firstButton" color="0x000000" click="firstPage()" enabled="{hasPreviousItemToDisplay}" />
        <mx:Button styleName="previousButton" color="0x000000" click="previousPage()" enabled="{hasPreviousItemToDisplay}" />
        <mx:Button styleName="nextButton" color="0x000000" click="nextPage()" enabled="{hasNextItemToDisplay}" />
        <mx:Button styleName="lastButton" color="0x000000" click="lastPage()" enabled="{hasNextItemToDisplay}" />
    </mx:ApplicationControlBar>
        
</mx:WindowedApplication>