(function() {
    

    function sendData(tpe, data) {
        $.panelsocketserver.sendJSONToAll(JSON.stringify({
            'eventFamily': 'overlay',
            'eventType': tpe,
            'data': data
        }));
    }

    $.bind('command', function(event) {

        const sender = "" + event.getSender().toLowerCase(),
            command = event.getCommand(),
            args = event.getArgs(),
            action = args[0],
            value = args[1];

        if (command.equalsIgnoreCase('overlay')) {
            if (!action) {
                $.say($.whisperPrefix(sender) + $.lang.get('customOverlay.help', ' Use "!overlay [follow | subscribe | donation | timer] value" to set overlay data.'));
            } else if (action.equalsIgnoreCase('follow')) {
                sendData(action, value);
            } else if (action.equalsIgnoreCase('subscribe')) {
                sendData(action, value);
            } else if (action.equalsIgnoreCase('donation')) {
                sendData(action, value);
            } else if (action.equalsIgnoreCase('timer')) {
                sendData(action,  new Date(Date.now().getTime()+value*1000*60));
            } else {
                $.say($.whisperPrefix(sender) + $.lang.get('customOverlay.help'));
            }
        }
    });

    $.bind('twitchFollow', function(event) {
        sendData('follow', event.getFollower());
    });
    $.bind('twitchSubscriber', function(event) {
        sendData('subscribe', event.getSubscriber());
    });

    $.bind('initReady', function() {
        $.registerChatCommand('./custom/custom/customOverlay.js', 'overlay');

        $.registerChatSubcommand('overlay', 'follow', 2);
        $.registerChatSubcommand('overlay', 'subscribe', 2);
        $.registerChatSubcommand('overlay', 'donation', 2);
        $.registerChatSubcommand('overlay', 'timer', 2);
    });

})();