/**
 * Copyright © 2016 Wyomind. All rights reserved.
 * See LICENSE.txt for license details.
 */

msiStockStatus = {
    attributes: [],
    updateStocks: function (classname) {
        msiStockStatus.attributes = [];

        msiStockStatus.length = jQuery(classname).length;
        jQuery(classname).each(function () {
            if (classname == ".super-attribute-select") {
                var attr_id = jQuery(this).attr("attribute-id");
                if (attr_id == undefined && jQuery(this).attr("id")) {
                    attr_id = jQuery(this).attr("id").replace("attribute", "");
                }
                if (attr_id == undefined) {
                    attr_id = jQuery(this).attr("name").replace("super_attribute[", "").replace("]", "");
                }
                option_id = jQuery(this).attr("option-selected");
                if (option_id == undefined && jQuery(this).val() != "") {
                    option_id = jQuery(this).val()
                }
                if (option_id != null && jQuery(this).val() != "")
                    msiStockStatus.attributes.push({'id': attr_id, 'value': option_id});
            } else {

                attr_id = jQuery(this).attr("attribute-id");

                if (typeof attr_id === "undefined") {
                    attr_id = jQuery(this).attr("data-attribute-id");
                    if (typeof attr_id === "undefined") {
                        attr_id = jQuery(this).data("attribute-id");
                    }
                }


                option_id = jQuery(this).find(".selected").attr("option-id");
                if (typeof option_id === "undefined") {
                    option_id = jQuery(this).find(".selected").attr("data-option-id");
                    if (typeof option_id === "undefined") {
                        val = jQuery(this).find("option:selected").val();
                        if (val > 0) {
                            option_id = val;
                        }

                    }
                }

                if (typeof option_id != "undefined") {
                    msiStockStatus.attributes.push({'id': attr_id, 'value': option_id});
                }

            }
        });


        jQuery(".msiss-grid SPAN.status").attr("class", "status ").html("-")
        if (msiStockStatus.length == msiStockStatus.attributes.length) {

            jQuery(".msiss-grid .notice").hide();
            if (typeof msiStockStatusData !== 'undefined' && typeof msiStockStatusData.stocks !== 'undefined') {

                jQuery.each(msiStockStatusData.stocks, function (i, s) {

                    if (msiStockStatus.attributes.length > 0) {
                        found = true;
                        jQuery.each(msiStockStatus.attributes, function (x, attr) {
                            eval("attribute = s.attribute" + attr.id);
                            if (attribute != attr.value) {
                                found = false;
                            }
                        });
                    } else {
                        found = false;
                    }

                    if (found) {
                        jQuery(".msiss-message-container").removeClass("notice").addClass("message").addClass(msiStockStatusData.stocks[i].class);
                        jQuery(".msiss-message").html(msiStockStatusData.stocks[i].message);

                        jQuery.each(s.stock, function (i, pos) {

                            jQuery("#store_" + pos.store).show();
                            if (pos.message) {
                                jQuery("#store_" + pos.store + " SPAN.status").attr("class", "status " + pos.status).html(pos.message)
                            } else {
                                jQuery("#store_" + pos.store + " SPAN.status").attr("class", "status " + pos.status).html(eval("msiStockStatusData." + pos.status));
                            }
                            if (pos.status == "hidden") {
                                jQuery("#store_" + pos.store).hide();
                            }
                        });
                    }
                });
            }
        } else {


            jQuery(".msiss-message-container").removeClass("loader").removeClass("message").removeClass("error").removeClass("success").addClass("notice");
            jQuery(".msiss-message").html(msiStockStatusData.default);

            jQuery(".msiss-grid .notice").show();


        }
    }
};

require(["jquery", "mage/mage"], function ($) {
    if (msiStockStatus != undefined) {
        $(function () {
            $(document).on("change", ".super-attribute-select", function () {

                msiStockStatus.updateStocks(".super-attribute-select");
            });
            $(document).on("click", ".swatch-attribute", function () {


                msiStockStatus.updateStocks(".swatch-attribute");


            });
        });
    }
});
