package com.afi.estimator.model
{
    import com.afi.estimator.common.DateUtil;
    import com.afi.estimator.common.XMLConverter;
    import com.afi.estimator.views.vo.EstimatorVO;
    import com.afi.estimator.views.vo.MonthlyRates;
    import com.afi.estimator.views.vo.Premium;
    import com.afi.estimator.views.vo.ResultsVO;
    
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    
    import mx.collections.ArrayCollection;
    import mx.formatters.CurrencyFormatter;
    
    [Bindable]public class EstimatorModel
    {
        public var estimatorVO:EstimatorVO = new EstimatorVO();
        public static var instance:EstimatorModel;
        public var resultsList:ArrayCollection = new ArrayCollection();
        public var age:int;
        
        private var converter:XMLConverter = new XMLConverter();
        private var f:MonthlyRates;
        private var pre:Premium;
        
        public function EstimatorModel(estVo:EstimatorVO){
            this.estimatorVO = estVo;
            EstimatorModel.instance = this;
            Level10Model.instance = new Level10Model();
            Level20Model.instance = new Level20Model();
            Level30Model.instance = new Level30Model();
            age = DateUtil.calculateAge(estimatorVO.dateOfBirth);
        }
        
        public function calculatePremiums():void{
            if(estimatorVO){
                //true => male and false => female
                if(estimatorVO.genderValue){
                    
                    if(estimatorVO.usedNicotineValue){
                        if(estimatorVO.waiverPremiumValue){
                            getMNicWaiverVOs("waiver/mNic");
                            
                        } else{
                            getMNicWaiverVOs("withoutWaiver/mNic");
                        }
                    } else{
                        if(estimatorVO.waiverPremiumValue){
                            getMNicWaiverVOs("waiver/mNonNic");
                            
                        } else{
                            getMNicWaiverVOs("withoutWaiver/mNonNic");
                        }
                    }
                } else{
                    if(estimatorVO.usedNicotineValue){
                        if(estimatorVO.waiverPremiumValue){
                            getMNicWaiverVOs("waiver/fNic");
                            
                        } else{
                            getMNicWaiverVOs("withoutWaiver/fNic");
                        }
                    } else{
                        if(estimatorVO.waiverPremiumValue){
                            getMNicWaiverVOs("waiver/fNonNic");
                            
                        } else{
                            getMNicWaiverVOs("withoutWaiver/fNonNic");
                        }
                    }
                }
            }
        }
        
        private function getMNicWaiverVOs(fileString:String):void{
            if(!Level10Model.instance.mNicWaiver.isDataExists){
                loadXML("level10/"+fileString, function():void{
                    Level10Model.instance.mNicWaiver = pre;
                    Level10Model.instance.mNicWaiver.isDataExists = true;
                });
            }
            if(!Level20Model.instance.mNicWaiver.isDataExists){
                 loadXML("level20/"+fileString,function():void{
                     Level20Model.instance.mNicWaiver = pre;
                    Level20Model.instance.mNicWaiver.isDataExists = true;
                 });
            }
            if(!Level30Model.instance.mNicWaiver.isDataExists){
                loadXML("level30/stateNonWA/"+fileString,function():void{
                    Level30Model.instance.mNicWaiver =  pre;
                    Level30Model.instance.mNicWaiver.isDataExists = true;
                    buildMNicResults();
                });
            }
        }
        
        public function loadXML(xml_name:String, callBack:Function):void {
            // Load XML
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, function(e:Event):void{
                var dataXML:XML = XML(e.target.data);
                pre = converter.xmlToVo(dataXML) as Premium;
                callBack();
            });
            loader.load(new URLRequest("com/afi/estimator/assets/xml/"+xml_name+".xml"));
        }
        
        private function buildMNicResults():ArrayCollection{
            var currForm:CurrencyFormatter = new CurrencyFormatter();
            currForm.currencySymbol = "$";
            currForm.useThousandsSeparator = true;
            currForm.precision = 2;
            
            var resultsList:ArrayCollection = new ArrayCollection();
            var level10MNic:MonthlyRates = monthlyRatesWRTAge(Level10Model.instance.mNicWaiver);
            var level20MNic:MonthlyRates = monthlyRatesWRTAge(Level20Model.instance.mNicWaiver);
            var level30MNic:MonthlyRates = monthlyRatesWRTAge(Level30Model.instance.mNicWaiver);
            var resultVO:ResultsVO = new ResultsVO();
            resultVO.coverage = "$50,000";
            resultVO.premium1 = level10MNic? currForm.format(level10MNic.coverage_50000): "Not Available";
            resultVO.premium2 = level20MNic? currForm.format(level20MNic.coverage_50000): "Not Available";
            resultVO.premium3 = level30MNic? currForm.format(level30MNic.coverage_50000): "Not Available";
            resultsList.addItem(resultVO);
            
            resultVO = new ResultsVO();
            resultVO.coverage = "$100,000";
            resultVO.premium1 = level10MNic? currForm.format(level10MNic.coverage_100000): "Not Available";
            resultVO.premium2 = level20MNic? currForm.format(level20MNic.coverage_100000): "Not Available";
            resultVO.premium3 = level30MNic? currForm.format(level30MNic.coverage_100000): "Not Available";
            resultsList.addItem(resultVO);
            
            resultVO = new ResultsVO();
            resultVO.coverage = "$150,000";
            resultVO.premium1 = level10MNic? currForm.format(level10MNic.coverage_150000): "Not Available";
            resultVO.premium2 = level20MNic? currForm.format(level20MNic.coverage_150000): "Not Available";
            resultVO.premium3 = level30MNic? currForm.format(level30MNic.coverage_150000): "Not Available";
            resultsList.addItem(resultVO);
            
            this.resultsList = resultsList;
            return resultsList;
            
        }
        
        private function monthlyRatesWRTAge(pre:Premium):MonthlyRates{
            for each(var monthlyRates:MonthlyRates in pre.monthlyRatesList){
                if(monthlyRates.issueAge == age){
                    return monthlyRates;
                }
            }
            
            return null;
        }
        
    }
}