/**
 * Plumrocket Inc.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the End-user License Agreement
 * that is available through the world-wide-web at this URL:
 * http://wiki.plumrocket.net/wiki/EULA
 * If you are unable to obtain it through the world-wide-web, please
 * send an email to support@plumrocket.com so we can send you a copy immediately.
 *
 * @package     Plumrocket Product Filter v3.x.x
 * @copyright   Copyright (c) 2016 Plumrocket Inc. (http://www.plumrocket.com)
 * @license     http://wiki.plumrocket.net/wiki/EULA  End-user License Agreement
 */

define([
    'jquery',
    'Plumrocket_ProductFilter/js/model/price',
    'jquery/ui',
    'jquery/ui-touch-punch',
    'domReady!'
], function ($, price) {
    "use strict";

    $.widget('plumrocket.priceslider', {

        initedElement: $('#product-filter-init'),

        currentRanges : [],

        _create: function () {
            var self = this,
                emptyPriceRequest = Object.keys(self.options.request).length === 0;

            if (!this.initedElement.data('max-price') || emptyPriceRequest) {
                this.initedElement.data('max-price', self.options.max);
                this.initedElement.data('min-price', self.options.min);
            }

            var values = [];
            if (this.options.request.min) {
                self.currentRanges[0] = Math.floor(this.options.request.min);
                values.push(this.options.request.min);
            } else {
                self.currentRanges[0] = self.options.min;
                values.push(self.options.min);
            }

            if (this.options.request.max) {
                self.currentRanges[1] = Math.ceil(this.options.request.max);
                values.push(this.options.request.max);
            } else {
                self.currentRanges[1] = self.options.max;
                values.push(self.options.max);
            }

            try {
                $(this.element).slider({
                    range: true,
                    values: values,
                    min: this.initedElement.data('min-price'),
                    max: this.initedElement.data('max-price'),
                    slide: function (event, ui) {
                        self.currentRanges = ui.values;
                        self.changeInputValue(self.currentRanges[ 0 ], self.currentRanges[ 1 ]);
                    },
                    stop: function (event, ui) {
                        price.change(self.currentRanges[ 0 ]/self.options.rate, self.currentRanges[ 1 ]/self.options.rate, true);
                    }
                });
            } catch (err) {}
        },

        changeRangeHtml: function (from, to) {
            $(this.options.amountFrom).html(from);
            $(this.options.amountTo).html(to);
        },

        changeInputValue: function (from, to) {
            // price.change(from,to);
            this.changeRangeHtml(from, to);
            if ($('div[data-filter-type="input"]').length) {
                $(this.options.inputFrom).val(from);
                $(this.options.inputTo).val(to);
            }
        }
    });

    return $.plumrocket.priceslider;
});