package com.afi.estimator.model {
    import flash.events.*;
    import flash.net.*;

    [Bindable]public class TextModel {
        // Singleton
        
        [Bindable]public static var instance:TextModel;
        
        public static function init(callback:Function):void {
            TextModel.instance = new TextModel(callback);
        }
        
        // Make sure that the page exists to minimize runtime errors
        [Bindable]public var result:Object;
        
        public static function get(pageName:String):Object {
            instance.result = TextModel.instance.pages[pageName] || {};
            return instance.result;
        }
        
        public static function getFaq(callback:Function):void {
            if (instance.glossary != {}) callback(instance.faq);
            else instance.gloss_callbacks.push(callback);
        }

        public static function getGlossary(callback:Function):void {
            if (instance.glossary != {}) callback(instance.glossary);
            else instance.gloss_callbacks.push(callback);
        }
        // Class Def.
        
        private var pages:Object = {};
        private var faq:Object = {};
        private var glossary:Object = {};

        private var callback:Function;
        public var faq_callbacks:Array = [];
        public var gloss_callbacks:Array = [];
        
        public function TextModel(callback:Function):void {
            this.callback = callback;
            XML.ignoreWhitespace = true;
            
            loadCopy("copydeck-en", parseCopydeck);
            loadCopy("faq-en", parseFaq);
            loadCopy("glossary-en", parseGlossary);
        }

        public function loadCopy(xml_name:String, prase_method:Function):void {
            // Load XML
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, prase_method);
            loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:Event):void {
                trace("Error", e);
            });
            
            loader.load(new URLRequest("com/afi/estimator/assets/xml/"+xml_name+".xml"));
        }

        // Parsers
        private function parseGlossary(e:Event):void {    
            var data:XML = new XML(e.target.data);

            for (var i:* in data.term) {
                var letter:String = data.term[i].name.charAt(0);
                if (glossary[letter] == null) {
                    glossary[letter] = new Array();
                }

                var entry:Object = {
                    name: data.term[i].name,
                    description: data.term[i].description
                };
                glossary[letter].push(entry);
            }

            for each (var c:Function in faq_callbacks) c(faq);
        }
    
        private function parseFaq(e:Event):void {
            var data:XML = new XML(e.target.data);

            for (var i:* in data.section) {
                faq[data.section[i].@name] = data.section[i];
            }

            for each (var c:Function in faq_callbacks) c(faq);
        }

        private function parseCopydeck(e:Event):void {
            var data:XML = new XML(e.target.data);
            
            for (var i:* in data.section) {
                var pageName:String = data.section[i].@name;
                pages[pageName] = {};
                for (var ii:* in data.section[i].children()) {
                    pages[pageName][data.section[i].child(ii).name()] = XMLList(data.section[i].child(ii)).text();
                }
            }
            
            callback();
        }
        

    }
}