Object.defineProperty(exports, "__esModule", { value: true });
var MittiClass_1 = require("../MittiClass");
module.exports = function (actionAPI) {
    //Handle the action when executed
    actionAPI.handle(function (properties, status) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var clipID = properties.clipID != undefined ? properties.clipID : 'none';
        if (connectionID != 'none') {
            if (clipID != 'none') {
                var connection = actionAPI.getConnection('mitti', connectionID);
                var ip = connection.properties.ip;
                var mitti = new MittiClass_1.Mitti(ip);
                mitti.on('connected', function () {
                    mitti
                        .selectClip(parseInt(clipID))
                        .then(function () {
                        status("Clip ".concat(clipID, " has been selected"), 'info');
                    })
                        .catch(function (error) {
                        status(error.message, 'error');
                    });
                });
            }
            else
                status('No clip specified', 'error');
        }
        else
            status('No connection specified', 'error');
    });
    //Handle the interactive editor
    actionAPI.onOpenEditor(function (editorAPI, properties) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var clipID = properties.clipID != undefined ? properties.clipID : 'none';
        var currentIP;
        var currentPort;
        var fields = [
            {
                id: 'connectionID',
                name: 'Connection',
                type: 'connection',
                value: connectionID,
                connectionType: 'mitti'
            },
            {
                id: 'clipID',
                name: 'Clip',
                type: 'select',
                value: clipID,
                values: []
            }
        ];
        function updateAddress(connectionID, callback) {
            if (connectionID != 'none') {
                var connection = actionAPI.getConnection('mitti', connectionID);
                var ip = connection.properties.ip;
                var port = connection.properties.port;
                if (currentIP != ip || currentPort != port) {
                    currentIP = ip;
                    currentPort = port;
                    callback(true);
                }
                else {
                    callback(false);
                }
            }
            else {
                if (currentIP != null || currentPort != null) {
                    currentIP = null;
                    currentPort = null;
                    callback(true);
                }
                else {
                    callback(false);
                }
            }
        }
        function updateSources(callback) {
            var mitti = new MittiClass_1.Mitti(currentIP);
            mitti
                .getClips()
                .then(function (clips) {
                var fieldValues = [];
                for (var clipID in clips) {
                    var query = clips[clipID];
                    var name = query.split(' ').splice(query.split(' ').length - 3, 2);
                    fieldValues.push({ id: clipID, text: "".concat(clipID, " - ").concat(name) });
                }
                fields[1].values = fieldValues;
                callback();
            })
                .catch(function (error) {
                fields[1].values = [];
                callback();
            });
        }
        function validate(fieldValues) {
            fields[0].value = fieldValues.connectionID;
            fields[1].value = fieldValues.clipID;
            editorAPI.saveProperties({ connectionID: fieldValues.connectionID, clipID: fieldValues.clipID });
            updateAddress(fieldValues.connectionID, function (changed) {
                if (changed) {
                    updateSources(function () {
                        editorAPI.setFields(fields);
                    });
                }
                else
                    editorAPI.setFields(fields);
            });
        }
        validate({ connectionID: connectionID, clipID: clipID });
        editorAPI.onFieldChanges(function (fields) {
            var fieldValues = editorAPI.tools.objectifyFieldsValues(fields);
            validate(fieldValues);
        });
    });
};
//# sourceMappingURL=clip.js.map