﻿/*
    jsonObjectLoader 
    used to load JS objects via JSON calls
*/


this.jsonObjectLoader = function(wgid){

    var _wgid = wgid;

    /// load JSON for the specified BasketItem (BasketItemJsonData)
    /// + oid - the basket item id
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.loadBasketItem = function(oid, callback){
    
        var url = getJsonUrl("LoadBasketItem", '&oid=' + oid);
       
        $.getJSON(url, function(data){
            
            if(true == canHandleJSON(data)){

                callback(data);
            
            };
            
        });
        
    };
    
    /// load JSON for the current UserInfo
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.loadUserInfo = function(callback){
    
        var url = getJsonUrl("LoadUserInfo", "");

        $.getJSON(url, function(data){
        
            if(true == canHandleJSON(data)){
                callback(data);
            };
        });
    };
    
    // load JSON for the current Order
    // + oid (64-bit int) the order id
    // + callback (function) - a function to execute on callback -
    //     the JSON data is passed to this function as a single parameter, "data"
    this.loadOrder = function(oid, callback){
    
        var url = getJsonUrl("LoadOrder", "&oid=" + oid);

        $.getJSON(url, function(data){
        
            if(true == canHandleJSON(data)){
                callback(data);
            };
        });
    };
    
    /// load JSON for the current UI preferences
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.loadUIPreferences = function(callback){
    
        var url = getJsonUrl('LoadUIPreferences', '&cw=' + $(window).width());
        
        $.getJSON(url, function(data){
            if(true == canHandleJSON(data)){
                callback(data);
            };
        });
    
    };

    
    /// load JSON for the current Product
    /// + mmid (long) - the mmid of the product to load
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.loadProduct = function(mmid, callback){
    
        var url = getJsonUrl("LoadProduct", "&mmid=" + mmid);

        $.getJSON(url, function(data){
        
            if(true == canHandleJSON(data)){
            
                // add to the product cache
                $mb.addProductData(data.InnerData, true);
                
                // GetEffectiveBreakPoint
                data.InnerData.GetEffectiveBreakPoint = function(quantity){
                
                   
                    
                    var result = null; 
                    for(var i = 0; i < this.BreakPoints.length; i++){
                   
                        var breakPoint = this.BreakPoints[i];

                        if(quantity >= breakPoint.StartQuantity && quantity <= breakPoint.EndQuantity){
                            result = breakPoint;
                            break;
                        };
                    };
                    
                    return result;
                    
                };

                // GetPrice()
                data.InnerData.GetPrice = function(quantity){
                
                    var result = '';
                    if(quantity > 0){
                        var bp = this.GetEffectiveBreakPoint(quantity);
                        var result = (quantity / bp.BatchSize) * bp.EffectivePrice.Amount;
                        result = bp.EffectivePrice.Currency + ensure2TrailingDecimals(result);
                    };
                    return result;
                
                };
                
                // GetPreDiscountPrice()
                data.InnerData.GetPreDiscountPrice = function(quantity){
                    var result = '';
                    if(quantity > 0){
                        var bp = this.GetEffectiveBreakPoint(quantity);
                        var result = (quantity / bp.BatchSize) * bp.Price.Amount;
                        result = bp.EffectivePrice.Currency + ensure2TrailingDecimals(result);
                    };
                    return result;
                };
                
                
                callback(data);
            };
        });
    };
    
    /// load JSON for the forgotten password command
    /// + email (string) - the user email
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.forgottenPassword = function(email, callback){
         $.getJSON(getJsonUrl("ForgottenPasswordJson", "&em=" + email), function(data){ callback(data); });
    };
    
    /// load JSON for the login command
    /// + email (string)
    /// + pw (string)
    /// + ru (string) - return url - unescaped
    //  + dcm (bool) - do customer merge (default false)
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.login = function(email, pw, ru, dcm, callback){
        $.getJSON(getJsonUrl("LoginJson", "&em=" + email + "&pw=" + pw + "&ru=" + escape(ru) + "&dcm=" + dcm), function(data){ callback(data); });
    };
    
    /// load JSON for the SendInvoice command
    // + orderID (long) - the order to send
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.sendInvoice = function(orderID, callback){
        $.getJSON(getJsonUrl("SendInvoice", "&oid=" + orderID), function(data){ callback(data); });
    };
    
    /// load JSON for the BasketAddOrder command
    //  + orderID (long) - the order to send
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.addOrder = function(orderID, callback){
        $.getJSON(getJsonUrl("BasketAddOrder", "&oid=" + orderID + "&sfl=false"), function(data){ callback(data); });
    };
    
    /// load JSON (QuantityCheckJsonData) for the QuantityCheck command
    //  + mmid (long) - mmid
    //  + quantity (long) - quantity queried
    //  + override (bool) - override current quantity in basket
    //  + save (bool) - as a save for later item?
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.quantityCheck = function(mmid, quantity, override, save, callback){
        $.getJSON(getJsonUrl("QuantityCheck", "&mmid=" + mmid + "&qu=" + quantity+ "&qo=" + override + "&sfl=" + save), function(data){ callback(data); });
    };
    
    /// load JSON for the SetPanelState command
    //  + state (string) - current panel state
    /// + callback (function) - a function to execute on callback -
    ///     the JSON data is passed to this function as a single parameter, "data"
    this.setPanelState = function(state, callback){
        $.getJSON(getJsonUrl("SetPanelState", "&ps=" + state), function(data){ callback(data); });
    };
    
    /* private functions */
    
    function getJsonUrl(cmd, additionalQuerystringInfo){
        var result = cookiesToQueryString(['rid','rak']) + '&wgid=' + _wgid + '&cmd=' + cmd + additionalQuerystringInfo;
        return $mb.urlBuilder.buildJsonUrl(result);
    };
    
};