export class SovTools {

    static log(value) {
        try {
            console.log(value);
        } catch {
        }
    }

    static arrayRemove(arr, value) {
        return arr.filter(function (ele) {
            return ele != value;
        });
    }

    static toPlainText(html) {
        let scratchDiv = document.createElement('div');
        scratchDiv.innerHTML = html;
        return scratchDiv.textContent;
    }

    static getDateFormatted(date, separator) {
        if (date) {
            var date = new Date(date);
            var _d = date.getDate(),
                d = _d > 9 ? _d : '0' + _d,
                _m = date.getMonth() + 1,
                m = _m > 9 ? _m : '0' + _m,
                formatted = d + separator + m + separator + date.getFullYear();
            return formatted;
        } else {
            return '';
        }
    }

    static getUrlParameter(sParam) {
        var sPageURL = window.location.search.substring(1),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');

            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
            }
        }
    }

    static indexOfFirstDigit(input) {
        let i = 0;
        for (; input[i] < '0' || input[i] > '9'; i++) ;
        return i == input.length ? -1 : i;
    }

    static indexOfLastDigit(input) {
        let i = input.length - 1;
        for (; input[i] < '0' || input[i] > '9'; i--) ;
        return i == input.length ? -1 : i;
    }

    static formatNumber(number, toFixed) {
        if (number) return Number(number.replace(',', '.')).toFixed(toFixed);
        else return null;
    }

    static formatNumberWithoutRounding(number, toFixed) {
        if (number){
            let factor = Math.pow(10, toFixed);
            return Math.floor(number * factor) / factor
        }else{
            return null
        }
    }

    static formatNumberWithoutFixed(number) {
        if (typeof number == 'string') number = number.replace(',', '.');
        if (number) return Number(number);
        else return null;
    }

    static getUrlParameter(sParam) {
        var sPageURL = window.location.search.substring(1),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');

            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
            }
        }
    };

    static readFlashMessages(flashMessages) {
        var currentFlash = new Array();
        for (var type in flashMessages) {
            for (var key in flashMessages[type]) {
                if (!currentFlash.includes(flashMessages[type][key])) {
                    currentFlash.push(flashMessages[type][key]);
                    SovNotification.add(type, flashMessages[type][key]);
                }
            }
        }
    }

    static checkFormValidity(formId) {
        if (!document.getElementById(formId.substr(1)).checkValidity()) {
            document.getElementById(formId.substr(1)).reportValidity()
            return false;
        } else {
            return true;
        }
    }

    static initCountSmsCharacter(countSmsTextareaClass) {
        $(countSmsTextareaClass).each(function () {
            $(this).after('<div class="count-sms-character-info"></div>');
            SovTools.displaySmsTotalCharacter($(this));
            $(this).on('keyup', function () {
                SovTools.displaySmsTotalCharacter($(this));
            });
        })
    }

    static displaySmsTotalCharacter($textarea) {
        var totalCharacter = $textarea.val().length;
        var totalCharacterCurrentSms = totalCharacter % 160;
        if (totalCharacterCurrentSms == 0 && totalCharacter > 0) {
            totalCharacterCurrentSms = 160;
        }
        let totalSms = Math.ceil(totalCharacter / 160);
        $textarea.next().html(totalCharacterCurrentSms + '/160 -<strong>' + totalSms + ' sms</strong>');

    }
}
