﻿
Type.registerNamespace("AtlasArena.ClientFacade");


///////////////////////////// SubProduct /////////////////////////////////////

AtlasArena.ClientFacade.SubProduct = function() {
    AtlasArena.ClientFacade.SubProduct.initializeBase(this);
    this.onSelectedEventHandlers = new Array();
}

AtlasArena.ClientFacade.SubProduct.prototype = {
    initialize: function() {
        AtlasArena.ClientFacade.SubProduct.callBaseMethod(this, 'initialize');


    },

    dispose: function() {
        //Add custom dispose actions here
        AtlasArena.ClientFacade.SubProduct.callBaseMethod(this, 'dispose');
    },

    OnSelectedEventHandler: function(eventHandler) {
        this.onSelectedEventHandlers.push(eventHandler);
    },

    Select: function(productID) {
       
        OnSelectedDelegate = Function.createDelegate(this, this.OnSelected);
        AtlasArena.Web.Services.Product.GetSubProducts(productID, OnSelectedDelegate);
       
    },



    OnSelected: function(args) {
    
        for (var i = 0; i < this.onSelectedEventHandlers.length; i++) {
            this.onSelectedEventHandlers[i](args);
        }

    }
}

///////////////////////////// ShoppingCart /////////////////////////////////////

AtlasArena.ClientFacade.ShoppingCart = function() {
    AtlasArena.ClientFacade.ShoppingCart.initializeBase(this);

    this.onSelectedEventHandlers = new Array();
    this.onRemovedEventHandlers = new Array();
    this.onUpdatedEventHandlers = new Array();

    this.clientCollection;
}

AtlasArena.ClientFacade.ShoppingCart.prototype = {
    initialize: function() {
        AtlasArena.ClientFacade.ShoppingCart.callBaseMethod(this, 'initialize');
    },

    dispose: function() {
        //Add custom dispose actions here

        AtlasArena.ClientFacade.ShoppingCart.callBaseMethod(this, 'dispose');
    },

    /*Select region*/
    Select: function(collection) {
        //debugger;
        if (collection != null)
            this.clientCollection = collection;
        else
            this.clientCollection = null;
        OnSelectedDelegate = Function.createDelegate(this, this.OnSelected);
        AtlasArena.Web.Services.ShoppingCart.GetOrderedItems(OnSelectedDelegate);
    },

    OnSelected: function(args) {
        //debugger;
        if (this.clientCollection != null)
            this.clientCollection.collection = args;
        for (var i = 0; i < this.onSelectedEventHandlers.length; i++) {
            this.onSelectedEventHandlers[i]();
        }

    },

    OnSelectedEventHandler: function(eventHandler) {
        this.onSelectedEventHandlers.push(eventHandler);
    },

    /*Remove region*/
    Remove: function(productID, collection) {
        if (collection != null)
            this.clientCollection = collection;
        else
            this.clientCollection = null;
        var OnRemovedDelegate = Function.createDelegate(this, this.OnRemoved);
        AtlasArena.Web.Services.ShoppingCart.RemoveProduct(productID, OnRemovedDelegate);
    },

    RemoveAll: function(collection) {
        if (collection != null)
            this.clientCollection = collection;
        else
            this.clientCollection = null;
        var OnRemovedDelegate = Function.createDelegate(this, this.OnRemoved);
        AtlasArena.Web.Services.ShoppingCart.RemoveAllProducts(OnRemovedDelegate);
    },

    OnRemoved: function(args) {
        if (this.clientCollection != null) {
            if (args > 0)
                this.clientCollection.Remove(args);
            else
                this.clientCollection.RemoveAll();
        }

        for (var i = 0; i < this.onRemovedEventHandlers.length; i++) {
            this.onRemovedEventHandlers[i](args);
        }

    },

    OnRemovedEventHandler: function(eventHandler) {
        this.onRemovedEventHandlers.push(eventHandler);
    },

    /*Update region*/
    Update: function(productID, quantityUnit, quantity, collection) {
        //debugger;
        if (collection != null)
            this.clientCollection = collection;
        else
            this.clientCollection = null;
        OnUpdatedDelegate = Function.createDelegate(this, this.OnUpdated);
        AtlasArena.Web.Services.ShoppingCart.AddProduct(productID, quantityUnit, quantity, OnUpdatedDelegate);
    },

    OnUpdated: function(args) {
       // debugger;
        if (this.clientCollection != null)
            this.clientCollection.Update(args);
        for (var i = 0; i < this.onUpdatedEventHandlers.length; i++) {
            this.onUpdatedEventHandlers[i](args);
        }

    },

    OnUpdatedEventHandler: function(eventHandler) {
        this.onUpdatedEventHandlers.push(eventHandler);
    }
}

