(function() {
    
    function getWordAt(str, pos) {
        str = String(str);
        pos = pos >>> 0;
        var left = str.slice(0, pos + 2).search(/\S+$/),
            right = str.slice(pos).search(/\s/);
        if (right < 0) 
            return str.slice(left);
        return str.slice(left, right + pos);
    }

    function getNoTwitchEmotes(event, message){
        var emotes = event.getTags().get('emotes'),
        str = message, i;

        if (emotes.length() > 0) {
            emotes = emotes.replaceAll('[0-9]+:', '').split('/');
            for (i in emotes) {
                str = str.replace(getWordAt(message, parseInt(emotes[i].split('-')[0])), '');
            }
        }
        return str;
    }


    function sendData(tpe, te, ce) {
        $.panelsocketserver.sendJSONToAll(JSON.stringify({
            'eventFamily': 'emote',
            'eventType': tpe,
            'data': {
                twitch: te,
                custom: ce,
            }
        }));
    }

    $.bind('ircChannelMessage', function(event){
        var sender = event.getSender(),
        message = event.getMessage();
        tags = event.getTags();
        if(message.indexOf('!')!=0)
            sendData('bounce',event.getTags().get('emotes').split('/'), getNoTwitchEmotes(event,message).split(' '))
    })

    $.bind('command', function(event) {
        const sender = "" + event.getSender().toLowerCase(),
            command = event.getCommand(),
            args = event.getArgs(),
            action = args[0];

        if (command.equalsIgnoreCase('ew')) {
            if (!action) {
                $.say($.whisperPrefix(sender) + $.lang.get('ew.help', ' Use "!ew [rain | firework | volcano | bomb | explosion] emote" to render emotes.'));
            } else if (action.equalsIgnoreCase('rain')) {
                sendData('rain',event.getTags().get('emotes').split('/'), getNoTwitchEmotes(event,event.args.join(' ')).split(' '))
            } else if (action.equalsIgnoreCase('firework')) {
                sendData('firework',event.getTags().get('emotes').split('/'), getNoTwitchEmotes(event,event.args.join(' ')).split(' '))
            } else if (action.equalsIgnoreCase('volcano')) {
                sendData('volcano',event.getTags().get('emotes').split('/'), getNoTwitchEmotes(event,event.args.join(' ')).split(' '))
            } else if (action.equalsIgnoreCase('bomb')) {
                sendData('bomb',event.getTags().get('emotes').split('/'), getNoTwitchEmotes(event,event.args.join(' ')).split(' '))
            } else if (action.equalsIgnoreCase('explosion')) {
                sendData('explosion',event.getTags().get('emotes').split('/'), getNoTwitchEmotes(event,event.args.join(' ')).split(' '))
            } else {
                $.say($.whisperPrefix(sender) + $.lang.get('ew.help', ' Use "!ew [rain | firework | volcano | bomb | explosion] emote" to render emotes.'));
            }
        }
    });

    $.bind('twitchFollow', function(event) {
        sendData('firework', [],['AYAYA'])
            
    });
    $.bind('twitchSubscriber', function(event) {
        sendData('volcano', [], ['AYAYA']);
    });

    $.bind('initReady', function() {
        $.registerChatCommand('./custom/custom/customEmoteWall.js', 'ew');
        $.registerChatSubcommand('ew', 'rain', 2);
        $.registerChatSubcommand('ew', 'firework', 2);
        $.registerChatSubcommand('ew', 'volcano', 2);
        $.registerChatSubcommand('ew', 'bomb', 2);
        $.registerChatSubcommand('ew', 'explosion', 2);
    });

})();