(function () {
    'use strict';
    angular.module('ti_common').service('ticHighlightSrv', ticHighlightSrv);
    ticHighlightSrv.$inject = ['$sce'];
    function ticHighlightSrv($sce) {
        function highlight(text, search, type, ignore) {
            if (!search || (text === ignore)) {
                return $sce.trustAsHtml(text);
            }
            if (type === 2) {
                return $sce.trustAsHtml(text.replace(new RegExp(search, 'gi'), '<span class="tic-highlight-text-dark">$&</span>'));
            }
            return $sce.trustAsHtml(text.replace(new RegExp(search, 'gi'), '<span class="tic-highlight-text">$&</span>'));
        }
        function highlightMsg(type, text) {
            type = type.toUpperCase();
            if ((type === 'SUCCESS') ||
                (text.toLowerCase().indexOf('memory exported to: ') !== -1) ||
                (text.toLowerCase().indexOf('completed successfully') !== -1) ||
                (text.toLowerCase().indexOf('program verification successful') !== -1)) {
                // successful message
                return $sce.trustAsHtml('<span class="text-success" style="font-weight:500;">[SUCCESS] ' + text + '</span>');
            }
            else if ((type === 'ERROR') ||
                (text.toLowerCase().indexOf('verification failed') !== -1) ||
                ((text.toLowerCase().indexOf('error') !== -1) && (text.toLowerCase().indexOf('gel output:') === -1))) {
                // error message
                return $sce.trustAsHtml('<span class="text-danger" style="font-weight:500;">[ERROR] ' + text + '</span>');
            }
            else if (type === 'WARNING') {
                // warning message
                return $sce.trustAsHtml('<span class="text-warning" style="font-weight:500;">[WARNING] ' + text + '</span>');
            }
            else if (type === 'DEBUG') {
                // successful message
                return $sce.trustAsHtml('<span class="text-info" style="font-weight:500;">[DEBUG] ' + text + '</span>');
            }
            return $sce.trustAsHtml('<span style="color:#222;">[INFO] ' + text + '</span>');
        }
        return {
            highlight: highlight,
            highlightMsg: highlightMsg
        };
    }
})();
