(function () {
    'use strict';
    angular.module('uniflash').directive('ufConfigCcxml', ufConfigCcxml);
    function ufConfigCcxml() {
        var directive = {
            restrict: 'E',
            controller: ConfigCcxml,
            controllerAs: 'vm',
            templateUrl: 'target_setup/config_ccxml.html',
            scope: {},
        };
        return directive;
    }
    var ConfigCcxml = (function () {
        function ConfigCcxml($log, $state, ticAlerts, ticFileDialogs, ufTargetConfig, ufProgramProviders, ufSession) {
            this.$log = $log;
            this.$state = $state;
            this.ticAlerts = ticAlerts;
            this.ticFileDialogs = ticFileDialogs;
            this.ufTargetConfig = ufTargetConfig;
            this.ufProgramProviders = ufProgramProviders;
            this.ufSession = ufSession;
            this.show = true;
        }
        ConfigCcxml.prototype.getIcon = function () {
            return this.ufProgramProviders.icon;
        };
        ConfigCcxml.prototype.selectCcxmlFile = function () {
            var _this = this;
            return this.ticFileDialogs.openFile('Select Target Configuration File', '.ccxml', 'Target Configurations')
                .then(function (selections) {
                _this.selectedCcxml = selections[0];
            }).catch(function (err) {
                _this.$log.error(err);
            });
        };
        ConfigCcxml.prototype.getFileName = function (filePath) {
            var idxOfSlash = filePath.lastIndexOf('/');
            idxOfSlash = (idxOfSlash < 0) ? 0 : idxOfSlash;
            var idxOfDot = filePath.lastIndexOf('.');
            idxOfDot = (idxOfDot < 0) ? filePath.length : idxOfDot;
            if (idxOfDot < idxOfSlash) {
                throw 'Bad file path: ' + filePath;
            }
            var ret = filePath.substring(idxOfSlash + 1, idxOfDot);
            return ret;
        };
        ConfigCcxml.prototype.parseCcxml = function (ccxmlContent) {
            var _this = this;
            return this.ufTargetConfig.queryConfig(ccxmlContent)
                .then(function (queryConfig) {
                // set our internal data;
                return {
                    device: _this.getFileName(queryConfig.deviceXMLFile),
                    connection: _this.getFileName(queryConfig.connectionXMLFile)
                };
            });
        };
        ConfigCcxml.prototype.startSession = function () {
            var _this = this;
            if (this.selectedCcxml) {
                var contents_1;
                return this.selectedCcxml.getContents()
                    .then(function (data) {
                    contents_1 = data;
                    return _this.parseCcxml(data);
                })
                    .then(function (ret) {
                    return _this.ufSession.newFromCCXML(ret.device, ret.connection, contents_1)
                        .then(function () {
                        _this.$state.go('main.Program', {
                            deviceXMLFile: ret.device,
                            connectionXMLFile: ret.connection
                        });
                    });
                })
                    .catch(function (e) {
                    var msg = e.data || e;
                    _this.ticAlerts.showError('Failed to launch session from ccxml file: ' + msg);
                });
            }
        };
        ConfigCcxml.$inject = ['$log', '$state', 'ticAlerts', 'ticFileDialogs', 'ufTargetConfig', 'ufProgramProviders', 'ufSession'];
        return ConfigCcxml;
    }());
})();
