﻿///<reference path="../../TESCO.js" />
///<reference path="../entities.js" />

TESCO.$("sites.UI.entities").Product = (function() {

    //  private static
    var _products = {};
    var _base = TESCO.UI.entities.Product;
		_base.steps.excludedAdd = "x";
		
    //  constructor
    function _constructor(obj) {
        var _fields = {
            "tpnb": obj.tpnb
        }
        _constructor.base.constructor.call(this, obj);
        _products[this.id()] = _fields;
        return this;
    }
    _constructor.extend(_base);

    _constructor.prototype.NAME = "TESCO.sites.UI.entities.Product";

    _constructor.prototype.createRequest = function(id, newQuantity, action) {
        return new TESCO.sites.UI.entities.Product.Quantity(id, newQuantity, action);
    }

    _constructor.prototype.getTPNB = function() {
        return _products[this.id()].tpnb;
    }
    
    _constructor.prototype.getBasketLineInc = function(f, step, basketId) {
		if (step === _base.steps.excludedAdd) { 
			f["basket-" + basketId].quantity = f.quantity;
			return f.quantity;
		} else {
			return _constructor.base.getBasketLineInc.apply(this, arguments);
		}
    }
    
    _constructor.prototype.updateBasketQuantity = function(step, basket, trace, sourceBasket) {
		//	is product in excluded shelf?
		var _tr = basket.getTrByProduct(this);
		if (_tr) {
			var _type = TESCO.UI.Basket.Shelf.getTypeFromTr(basket, _tr);
			if (!sourceBasket && step === "i" && _type.indexOf("Excluded") != -1) {	//	quantity updated, but it's in the excluded shelf
				step = _base.steps.excludedAdd;
				sourceBasket = basket;
			}
		}
		//	can product be added?
		_constructor.base.updateBasketQuantity.call(this, step, basket, trace, sourceBasket);
	}
    return _constructor;
})();

TESCO.sites.UI.entities.Baskets = function(baskets) {
    var _baskets = [];
    for (var i = 0, L = baskets.length; i < L; i++) {
        _baskets.push(TESCO.sites.UI.entities.Basket(baskets[i].id, baskets[i].view, baskets[i].products));
    }
    return _baskets;
}

TESCO.sites.UI.entities.Baskets.Request = function(baskets, useFirst) {
	if (useFirst) { 
		this.request = TESCO.sites.UI.entities.Basket(baskets[0].id, baskets[0].view, baskets[0].products);
	} else {
		this.request = {
			"baskets" : TESCO.sites.UI.entities.Baskets(baskets)
		}
	}
}

TESCO.sites.UI.entities.Basket = function(id, view, products) {
    /// <summary>creates a basket entity for serialisation</summary>
    /// <param name="id">string basket id</param>
    /// <param name="products">array of product entities</param>
    /// <param name="view">basket view</param>
    /// <param name="action">string action</param>
    /// <returns>new Object<returns>

    var _products = [];
    if (products) {
        for (var i = 0, L = products.length; i < L; i++) {
            var _action;
            switch (products[i].action) {
                case TESCO.UI.entities.Product.actions.move:
                    _action = products[i].action;
                    break;
                case TESCO.UI.entities.Product.actions.remove:
                    _action = (id === TESCO.sites.Configuration.activeBasket.id) ? "Remove" : "RemoveFromPreviousBasket";
                    break;
                default:
                    _action = "None";
                    break;
            }
            _products[i] = products[i].createRequest(products[i].getId(), products[i].newQuantity, _action);
        }
        _products = { "product": _products }
    }

    return {
        "basket": {
            "type": id,
            "view": TESCO.UI.Basket.viewString(view),
            "products": _products
        }
    }
}

TESCO.sites.UI.entities.Basket.Empty = function(basket) {
    return new TESCO.sites.retail.UI.Dialogue.Request("EmptyBasket",
        { "name": "emptyBasket" }, 
        { "param": [
                {
                    "name": "type",
                    "value": basket.id
                },
                {
                    "name": "view",
                    "value": TESCO.UI.Basket.viewString(basket.view)
                }
            ]
        }
    );
}

TESCO.sites.UI.entities.Basket.Request = function(basket) {
    this.request = TESCO.sites.UI.entities.Basket(basket.id, basket.view);
}

TESCO.sites.UI.entities.Product.Quantity = function(id, newQuantity, action) {
	//  todo: reconcile actions with MT
    return {
        "id": id,
        "basketLineQuantity": newQuantity,
        "action": action
    }
}
