var jQuery = jQuery || {};
var bm = bm || {};

bm.couponsSelection = (function ($) {

    var CouponsSelection = function (element, options) {
        this.$element = $(element);
        this.maxCoupons = {
            default: 0,
            special: 0
        };
        this.coupons = [];
        this.options = $.extend({}, CouponsSelection.DEFAULTS, options);
        this.init();
    };

    CouponsSelection.VERSION = '0.0.1';

    CouponsSelection.DEFAULTS = {
        maxDefault: 20,
        minSpecial: 2,
        maxSpecial: 2,
        defaultClass: 'cDefault',
        specialClass: 'cSpecial',
        submitForm: '.js-print-coupons'
    };

    CouponsSelection.prototype.init = function () {
    };

    CouponsSelection.prototype.updateOptions = function (options) {
        if (options) {
            this.options = $.extend({}, this.options, options);
        }
    };

    CouponsSelection.prototype.addCoupon = function (coupon) {
        if (this.maxCoupons.default < this.options.maxDefault) {
            addCoupons.call(this,coupon);
            this.maxCoupons.default += 1;
        } else {
            this.$element.trigger('bm.coupons.max.default');
        }
        checkSubmitButton.call(this);
    };

    CouponsSelection.prototype.addSpecialCoupon = function (coupon) {
        if (this.maxCoupons.special < this.options.maxSpecial) {
            addCoupons.call(this,coupon);
            this.maxCoupons.special += 1;
        } else {
            this.$element.trigger('bm.coupons.max.special');
        }
        checkSubmitButton.call(this);
    };

    CouponsSelection.prototype.removeCoupon = function (id) {
        var _this = this;
        var cDefault = this.options.defaultClass;
        var cSpecial = this.options.specialClass;
        var cArray = this.coupons;
        this.coupons = $.grep(cArray, function (e) {
            var _$this = e.this;
            if (e.id !== id) {
                return true;
            } else {
                if (_$this.hasClass(cDefault)) {
                    _this.maxCoupons.default -= 1;
                }
                if (_$this.hasClass(cSpecial)) {
                    _this.maxCoupons.special -= 1;
                }
                return false;
            }
        });

        checkSubmitButton.call(this);
        this.$element.trigger('bm.coupons.remove.updated',this);
    };

    CouponsSelection.prototype.printCoupons = function (form) {
        var couponList = getCouponList.call(this);
        if(couponList) {
            this.$element.trigger('bm.coupons.print',couponList);
        }
    };

    CouponsSelection.prototype.clear = function () {
        this.coupons = [];
        this.maxCoupons = {
            default: 0,
            special: 0
        };
        checkSubmitButton.call(this);
        this.$element.trigger('bm.coupons.remove.updated',this);
    };

    var checkSubmitButton = function () {
        var _$button =  $(this.options.submitForm);
        if (this.coupons.length > 0 && _$button.attr('disabled')) {
           _$button.attr('disabled',false);
        }
        if (this.maxCoupons.special !== 0 && this.options.minSpecial > this.maxCoupons.special) {
            _$button.attr('disabled',true);
        }
        if (this.coupons.length === 0) {
            _$button.attr('disabled',true);
        }
    };

    var addCoupons = function (coupon) {
        var obj = {
            id: createRandomID(),
            coupon: coupon.data('coupon'),
            this: coupon
        };
        this.coupons.push(obj);
        obj.count = this.coupons.length;
        this.$element.trigger('bm.coupons.add.updated', obj);
    };

    var getCouponList = function () {
        if (this.coupons.length > 0) {
            var idList = [];

            $.grep(this.coupons, function (e) {
                idList.push(e.coupon);
            });

            return idList.join();
        } else {
            return false;
        }
    };

    var createRandomID = function () {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        for (var i = 0; i < 10; i++)
            text += possible.charAt(Math.floor(Math.random() * possible.length));

        return text;
    };

    function Plugin(option, additionalArguments) {
        return this.each(function () {
            var $this = $(this);
            var pluginData = $this.data('bm.couponsSelection');

            if (!pluginData) {
                var options = $.extend({}, CouponsSelection.DEFAULTS, $this.data(), typeof option === 'object' && option);
                pluginData = new CouponsSelection(this, options);
                $this.data('bm.couponsSelection', pluginData);
            } else {
                if (typeof option === 'string') {
                    pluginData[option](additionalArguments);
                } else {
                    pluginData.updateOptions(option);
                }
            }
        });

    }

    var old = $.fn.couponsSelection;

    $.fn.couponsSelection = Plugin;
    $.fn.couponsSelection.Constructor = CouponsSelection;


    // Contentfilter NO CONFLICT
    // ====================

    $.fn.couponsSelection.noConflict = function () {
        $.fn.couponsSelection = old;
        return this;
    };


    // Contentfilter DATA-API
    // =================
}(jQuery));