var Wick = Wick || {};
Wick.Clamping = (function () {
    var originalTexts = {};

    return {
        clamp: function (origin, params) {

            var text = $(origin).text().trim();
            var link = $(origin).find('.clamp_btn');
            var linkNode = null;
            if (link.length) {
                linkNode = link.detach();
            }
            if (params.reset) {
                return {
                    text: originalTexts[params.id],
                    type: 'reset',
                    link: linkNode
                }
            }

            var cssObj = {};
            cssObj.fontSize = $(origin).css('font-size');
            cssObj.lineHeight = $(origin).css('line-height');
            cssObj.width = $(origin).width();

            if (originalTexts[params.id]) {
                text = originalTexts[params.id];
            } else {
                originalTexts[params.id] = text;
            }

            $(origin).text(text);

            var clone = $('<p></p>');
            clone.css({
                display: 'inline-block',
                margin: 0,
                width: 'auto',
                maxWidth: cssObj.width,
                fontSize: cssObj.fontSize,
                lineHeight: cssObj.lineHeight
            });

            $('body').append(clone);
            var arr = text.split(" ");
            var someString = '', safeWordIndex, dangerWordIndex;

            if ($(origin).height() <= parseInt($(origin).css('line-height')) * (params.lines)) {
                clone.remove();
                return {
                    text: text,
                    type: 'full',
                    link: linkNode
                }
            }
            for (var i = 0; i < arr.length; i++) {
                someString += arr[i];
                clone.text(someString);

                if (clone.height() > parseInt(clone.css('line-height')) * (params.lines - 1)) {
                    safeWordIndex = i;
                    break
                }
                someString += ' ';
            }
            var newArr = arr.splice(safeWordIndex);
            var safePart = arr.join(' ').length;
            var workPart = newArr.join(' ');
            someString = '... Read more';
            clone.text(someString);
            var extraWidth = clone.width();
            someString = '';
            for (var j = 0; j < workPart.length; j++) {
                someString += workPart[j];
                clone.text(someString);
                if (clone.width() > cssObj.width - extraWidth - 8) {
                    dangerWordIndex = j;
                    break
                }
            }

            someString = text.slice(0, safePart + dangerWordIndex);
            someString += '...';
            clone.text(someString);
            var clampedText = clone.text();
            clone.remove();

            return {
                text: clampedText,
                type: 'usual',
                link: linkNode
            }
        },
        clampAll: function (obj, params) {
            var checkToMobile = $(window).width() < 768;

            for (var i = 0; i < obj.length; i++) {

                var unit = obj.eq(i);
                var newID = params.id + i;
                unit.addClass('clamp_container');
                var newLines;
                if (params.variableLines) {
                    newLines = params.lines - params.variableLines[i];
                } else {
                    newLines = params.lines;
                }
                var clampage = Wick.Clamping.clamp(unit, {id: newID, lines: newLines});
                unit.text(clampage.text);

                var btn = $('<a>', {
                    text: 'Read more',
                    class: 'clamp_btn',
                    href: '#',
                    'data-id': newID,
                    'data-lines': newLines,
                    on: {
                        click: function (e) {
                            e.preventDefault();
                            var element = $(this).parent();
                            element.toggleClass('clamp-full');
                            var respond = null;
                            if ($(this).text() === 'Read more') {
                                $(this).text('Read less');
                                if (params.additionalLines) {
                                    if (checkToMobile) {
                                        element.parents(".full-width-offers").find(".card__inner").addClass("full-height");
                                        respond = Wick.Clamping.clamp(element, {
                                            reset: true,
                                            id: $(this).attr('data-id')
                                        });
                                    } else {
                                        $(this).parents(".full-width-offers").find(".card__content-wrap").addClass("change-card");
                                        respond = Wick.Clamping.clamp(element, {
                                            lines: +$(this).attr('data-lines') + params.additionalLines,
                                            id: $(this).attr('data-id')
                                        });

                                    }
                                } else {
                                    respond = Wick.Clamping.clamp(element, {reset: true, id: $(this).attr('data-id')});
                                }
                            } else {
                                element.parents(".full-width-offers").find(".card__inner").removeClass("full-height");
                                element.parents(".full-width-offers").find(".card__content-wrap").removeClass("change-card");
                                $(this).text('Read more');
                                respond = Wick.Clamping.clamp(element, {
                                    lines: $(this).attr('data-lines'),
                                    id: $(this).attr('data-id')
                                });
                            }
                            element.text(respond.text).append(respond.link);

                        }
                    }
                });
                if (clampage.type === 'usual') {
                    unit.append(btn);
                }

                if ((unit.height() / parseInt(unit.css('line-height'))) < newLines && checkToMobile) {
                    unit.children('.clamp_btn').addClass('clamp_btn-block');
                }
            }
        },
        flush: function (obj) {
            for (var i = 0; i < obj.length; i++) {
                var unit = obj.eq(i);
                var id = unit.find('.clamp_btn').attr('data-id');
                if (id) {
                    unit.text(Wick.Clamping.clamp(unit, {id: id, reset: true}).text);
                }
                unit.removeClass('clamp_container clamp-full');
            }
        },
        variableArray: function (obj) {
            // this method returns the array, how many lines of text is in every element of jQuery collection passed in
            var arr = [];
            for (var i = 0; i < obj.length; i++) {
                var unit = obj.eq(i);
                var val = Math.round(unit.height() / parseInt(unit.css('lineHeight')));
                arr.push(val);
            }
            return arr;
        }
    }

    //$0.innerHTML = Wick.Clamping.clamp($0, {id:'one', lines: 3}).text
    //$($0).text(Wick.Clamping.clamp($($0), {id:'one', lines: 3}).text)

    //Wick.Clamping.clampAll($($0), {id:'one', lines: 3})

    //Wick.Clamping.flush($($0), 'one')

    /*let TESTparamsTEST = {
        id: 'x1',
        reset: false,
        lines: 3
    };*/
})();
