define([
    '../../core/utils/data',
    '../../core/utils/helper',
    '../../core/utils/method',
    '../../core/models/index'
], (data, helper, method, model) => {
    (function($) {
        new method('toggle', {
            init: function (element) {
                const context = this;

                const options = context.local._options;
                const name = element.attr('name') || '';
                const type = element.attr('type') || 'chekcbox';

                const $toggle = $(context.$helper.parseTemplate('toggle'));
                if (element.closest('label').length < 1) {
                    element.wrap('<label></label>');
                }

                let regex = /input-(\w+)/gi;
                if (typeof element.attr('class') !== 'undefined' && element.attr('class').match(regex)) {
                    const matches = [];
                    const className = element.attr('class');
                    let match = null;

                    /* jshint ignore:start */
                    /* @DATE 2016. 12. 16 */
                    /* @USER Kenneth */
                    /* @NOTE Condition expression escaping. */
                    while (match = regex.exec(className)) {
                        matches.push(match[1]);
                    }
                    /* jshint ignore:end */

                    for (let idx in matches) {
                        let word = matches[idx];
                        $toggle.addClass('works-' + word);
                    }
                }

                $toggle.find('.switch .switch-ball').ripple({
                    theme: 'dark',
                    over: true,
                    passive: true
                });
                $toggle.insertAfter(element);

                element.prependTo($toggle.find('.toggle'));

                context.$helper.bind($toggle.find('.toggle'), 'click', function (event) {
                    if (typeof event.namespace === 'undefined' && element.prop('checked') === true && element.attr('type') === 'radio') {
                        event.preventDefault();
                        element.prop('checked', false).triggerHandler('change');
                    }
                });

                context.$helper.bind(element, 'change', function (event, extra) {
                    context.$helper.triggerHandler(element, 'update');

                    if(typeof extra !== 'undefined' && typeof extra.init !== 'undefined' && extra.init === true) {
                        $toggle.find('.switch .switch-ball').ripple('start');
                    }
                }, true);

                context.$helper.bind(element, 'update', function (event, extra) {
                    if (element.prop('checked') === false) {
                        $toggle.find('.toggle-label').removeClass('toggle-label-active').filter('.toggle-label-left').addClass('toggle-label-active');
                    }
                    else {
                        $toggle.find('.toggle-label').removeClass('toggle-label-active').filter('.toggle-label-right').addClass('toggle-label-active');
                    }

                    if (typeof extra !== 'undefined') {
                        $.extend(options, extra);
                    }

                    const placeholderClass = ['toggle-label-left', 'toggle-label-right'];
                    const placeholderDefault = ['Off', 'On'];
                    for (let idx in placeholderClass) {
                        if (
                            typeof options.placeholder !== 'undefined' &&
                            options.placeholder !== null &&
                            typeof options.placeholder[idx] !== 'undefined' &&
                            options.placeholder[idx] !== ''
                        ) {
                            $toggle.find('.' + placeholderClass[idx]).text(options.placeholder[idx]);
                        } else {
                            $toggle.find('.' + placeholderClass[idx]).text(placeholderDefault[idx]);
                        }
                    }
                }, true);

                if (element.prop('checked') === false) {
                    $toggle.find('.toggle-label').removeClass('toggle-label-active').filter('.toggle-label-left').addClass('toggle-label-active');
                }
                else {
                    $toggle.find('.toggle-label').removeClass('toggle-label-active').filter('.toggle-label-right').addClass('toggle-label-active');
                }

                context.$helper.bind($toggle.find('input'), 'click', event => {
                    event.stopPropagation();
                });

                element.hide();
            },
            template: {
                toggle: '<span class="toggle-wrapper"><span class="toggle-label toggle-label-left">Off</span><label class="toggle"><span class="switch"><span class="switch-ball"></span><span class="switch-bg"></span></span></label><span class="toggle-label toggle-label-right">On</span></span>'
            },
            options: {
                once: true,
                placeholder: null
            }
        });
    }(jQuery));
});