﻿var NetSuiteItem = (function() {
    function fnNetSuiteItemConstructor(iNewId, iNewApplicationId, sNewSKU, sNewDescription, sNewShortName, sNewLongName, iNewElements, sNewRequirement, sNewProduct, sNewProductCategory) {
        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*           Class Level Private Variables          */
        /*                                                  */
        /*                                                  */
        /****************************************************/
        
        var oInstance;

        var iId;
        var iApplicationId;
        var sSKU;
        var sDescription;
        var sShortName;
        var sLongName;
        var iElements;
        var sRequirement;
        var sProduct;
        var sProductCategory;
        var hPrices;

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                Accessors/Mutators                */
        /*                                                  */
        /*                                                  */
        /****************************************************/
        
        this.getId = function() {
            return iId;
        }
        
        this.setId = function(iNewId) {
            iId = iNewId;
        }
        
        this.getApplicationId = function() {
            return iApplicationId;
        }
        
        this.setApplicationId = function(iNewApplicationId) {
            iApplicationId = iNewApplicationId;
        }
        
        this.getSKU = function() {
            return sSKU;
        }
        
        this.setSKU = function(sNewSKU) {
            sSKU = sNewSKU;
        }
        
        this.getDescription = function() {
            return sDescription;
        }
        
        this.setDescription = function(sNewDescription) {
            sDescription = sNewDescription;
        }
        
        this.getShortName = function() {
            return sShortName;
        }
        
        this.setShortName = function(sNewShortName) {
            sShortName = sNewShortName;
        }
        
        this.getLongName = function() {
            return sLongName;
        }
        
        this.setLongName = function(sNewLongName) {
            sLongName = sNewLongName;
        }
        
        this.getElements = function() {
            return iElements;
        }
        
        this.setElements = function(iNewElements) {
            iElements = iNewElements;
        }
        
        this.getRequirement = function() {
            return sRequirement;
        }
        
        this.setRequirement = function(sNewRequirement) {
            sRequirement = sNewRequirement;
        }
        
        this.getProduct = function() {
            return sProduct;
        }
        
        this.setProduct = function(sNewProduct) {
            sProduct = sNewProduct;
        }
        
        this.getProductCategory = function() {
            return sProductCategory;
        }
        
        this.setProductCategory = function(sNewProductCategory) {
            sProductCategory = sNewProductCategory;
        }
        
        this.getPrices = function() {
            var hNewPrices = $H();
            
            var aPrices = hPrices.values();
            var oPrice;
            
            var aPricePromotions;
            for (var iPrice = 0; iPrice < aPrices.length; iPrice++) {
                oPrice = aPrices[iPrice].clone();
                hNewPrices.set(oPrice.getCurrency(), oPrice);
            }
            
            return hNewPrices;
        }
        
        this.getPricesArray = function() {
            var aNewPrices = new Array();

            var aPrices = hPrices.values();
            var oPrice;
            var oNewPrice;
            
            var aPricePromotions;
            for (var iPrice = 0; iPrice < aPrices.length; iPrice++) {
                oPrice = aPrices[iPrice];
                oNewPrice = new NetSuiteItemPrice(oPrice.getCurrency(), oPrice.getAmount());
                aPricePromotions = oPrice.getPromotions();
                for (var iPricePromotion = 0; iPricePromotion < aPricePromotions.length; iPricePromotion++) {
                    oNewPrice.addPromotion(new NetSuitePricePromotion(aPricePromotions[iPricePromotion].getId()));                
                }
                aNewPrices.push(oNewPrice);
            }
            
            return aNewPrices;
        }
        
        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                Cloneable Interface               */
        /*                                                  */
        /*                                                  */
        /****************************************************/
        
        this.clone = function() {
            var oNewNetSuiteItem = new NetSuiteItem(this.getId(), this.getApplicationId(), this.getSKU(), this.getDescription(), this.getShortName(), this.getLongName(), this.getElements(), this.getRequirement(), this.getProduct(), this.getProductCategory());
            
            var aPrices = hPrices.values();
            for (var iPrice = 0; iPrice < aPrices.length; iPrice++)
                oNewNetSuiteItem.addPrice(aPrices[iPrice].clone());
            
            return oNewNetSuiteItem;
        }
        
        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*          Class Level Priveleged Methods          */
        /*                                                  */
        /*                                                  */
        /****************************************************/
        
        this.addPrice = function(oPrice) {
            hPrices.set(oPrice.getCurrency(), oPrice);
        }
        
        this.removePrice = function(oPrice) {
            hPrices.each(function(oPair) {
                if (oPair.value == oPrice) {
                    hPrices.unset(oPair.key);
                    throw $break;
                }
            });
        }
        
        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                 Initialize Class                 */
        /*                                                  */
        /*                                                  */
        /****************************************************/
        
        oInstance = this;

        this.setId(iNewId);
        this.setApplicationId(iNewApplicationId);
        this.setSKU(sNewSKU);
        this.setDescription(sNewDescription);
        this.setShortName(sNewShortName);
        this.setLongName(sNewLongName);
        this.setElements(iNewElements);
        this.setRequirement(sNewRequirement);
        this.setProduct(sNewProduct);
        this.setProductCategory(sNewProductCategory);
        hPrices = $H();
    }
    
    return fnNetSuiteItemConstructor;
})();