var FitnessAdmin;

FitnessAdmin = {};

FitnessAdmin.InventoryItem = {};

FitnessAdmin.InventoryItem.Update = new function(){

    var defaults = {
        'selector_product' : '#product_autocomplete',
        'url_find_inventory_item' : 'inventory-item/update-item',
        "products" : [],
        "url_save" : '',
        "id_product": null
    };

    var _SELECTORS = {
        INVENTORY_PRODUCT_NAME: '#inventory_product_name',
        INVENTORYITEM_TYPE: '#inventoryitem-type',
        INVENTORYITEM_COUNT: '#inventoryitem-count',
        INVENTORY_FORM: '#inventory-product'
    };

    var inventoryItem = null;
    
    this.init = function (options) {
        defaults = $.extend(defaults,options);
        createGUI();
        preselectProduct();
    };

    var preselectProduct = function () {
        if ( defaults.id_product ){
            for (var i = 0; i < defaults.products.length ;i++ ){
                var p = defaults.products[i];
                if ( p.id_product == defaults.id_product){
                    $(defaults.selector_product).val(p.name);
                }
            }
        }else{
            $(_SELECTORS.INVENTORY_FORM).hide();
        }
        $(defaults.selector_product).focus().select();
    };

    var createGUI = function () {
        initAutoComplete();
    };

    var initAutoComplete = function () {
            var $input = $(defaults.selector_product);
            $input.typeahead({source : defaults.products,
                autoSelect: true,
                items: 20,
                minLength: 3
            });
            $input.change(function() {
                var current = $input.typeahead("getActive");
                $("#filter_text").val('');
                if (current) {
                    // Some item from your model is active!
                    if (current.name == $input.val()) {
                        // This means the exact match is found. Use toLowerCase() if you want case insensitive match.
                        //noinspection JSUnresolvedVariable
                        console.info(current);
                        _findProduct(current.id_inventory_item);
                    } else {
                        // This means it is only a partial match, you can either add a new item
                        // or take the active if you don't want new items
                        inventoryItem = null;
                        renderForm();
                    }
                } else {
                    // Nothing is active so it is a new value (or maybe empty value)
                    inventoryItem = null;
                    renderForm();
                }
            });
    };

    var selectInventoryItem = function (data) {
        console.info('data',data);
        inventoryItem = data;
    };

    var  _findProduct = function  (id){
        var data, url;

        url = defaults.url_find_inventory_item;
        data = {
            'id' : 	id
        };

        $.ajax({
            dataType: "json",
            url: url,
            data: data
        }).then(selectInventoryItem).then(renderForm);
    };

    function renderForm( ) {
        console.info( "render",inventoryItem);
        var name = "",type = "",count = "";
        if ( inventoryItem){
            name = inventoryItem.productName;
            type = inventoryItem.type;
            if ( inventoryItem.count != null ){
                count = inventoryItem.count;
            }
        }
        $(_SELECTORS.INVENTORY_PRODUCT_NAME).html(name);
        $(_SELECTORS.INVENTORYITEM_TYPE).val(type);
        $(_SELECTORS.INVENTORYITEM_COUNT).val(count);

        var formContainer = $(_SELECTORS.INVENTORY_FORM);
        var form = $(_SELECTORS.INVENTORY_FORM).find('form');

        if ( inventoryItem == null ){
            formContainer.hide();
        }else {
            formContainer.show();
            form.attr('action', defaults.url_save +"&id="+inventoryItem.id_inventory_item);
            $(_SELECTORS.INVENTORYITEM_COUNT).focus().select();
        }
    }

};