(function () {
    'use strict';
    angular.module('uniflash').directive('ufSettingsDisplay', SettingsDisplay);
    function SettingsDisplay() {
        var directive = {
            restrict: 'E',
            templateUrl: 'flash_properties/settings_display.html',
            controller: PropertiesDisplayController,
            controllerAs: 'vm',
            scope: {}
        };
        return directive;
    }
    PropertiesDisplayController.$inject = ['$scope', '$state', 'ufPropertiesDB', 'ufPropertiesValue', 'propertiesActionSrv', 'ufSession', 'ufSearch', 'ufQuickSettingsSrv', 'ufMainView', 'ufSettingsDisplayControlSrv', 'ufContext'];
    function PropertiesDisplayController($scope, state, ufPropertiesDB, ufPropertiesValue, propertiesActionSrv, ufSession, ufSearch, ufQuickSettingsSrv, ufMainView, ufSettingsDisplayControlSrv, ufContext) {
        var vm = this;
        vm.state = state;
        vm.ufPropertiesDB = ufPropertiesDB;
        vm.ufPropertiesValue = ufPropertiesValue;
        vm.propertiesActionSrv = propertiesActionSrv;
        vm.ufSession = ufSession;
        vm.ufQuickSettingsSrv = ufQuickSettingsSrv;
        vm.ufSettingsDisplayControlSrv = ufSettingsDisplayControlSrv;
        vm.ufContext = ufContext;
        vm.propertyListFilter = 1;
        vm.showButtonOnly = false;
        vm.propCoreSelect = 'All';
        vm.categoryFilter = 'All';
        vm.settingsUtilFilter = vm.ufPropertiesDB.getFilter();
        vm.displaySelect = '0';
        $scope.$on('$destroy', function () {
            vm.ufPropertiesDB.setFilter('');
            vm.settingsUtilFilter = '';
        });
        // Custom filter for MSP430
        if (state.params.deviceXMLFile.indexOf('MSP430') !== -1) {
            vm.propCoreSelect = 'MSP430';
        }
        vm.setCategoryFilter = function (cat) {
            vm.categoryFilter = cat;
        };
        // filter for the property list
        vm.propListFilter = function (prop) {
            // show only buttons
            if (vm.showButtonOnly && prop.valueType !== 'Button') {
                return false;
            }
            // show only the selected core properties (match target string or if property is generic)
            if ((vm.propCoreSelect !== 'All') && !ufPropertiesDB.matchTarget(prop, vm.propCoreSelect)) {
                return false;
            }
            // filtering ID and name based on the given string
            if ((prop._id.toLowerCase().indexOf(ufSearch.searchStr.toLowerCase()) === -1) &&
                (prop.name.toLowerCase().indexOf(ufSearch.searchStr.toLowerCase()) === -1)) {
                return false;
            }
            // custom case to remore clock control properties from MSP430
            if ((vm.propCoreSelect === 'MSP430') && ((prop._id.indexOf('ClockControl') !== -1) || (prop._id.indexOf('ClockDesc') !== -1))) {
                return false;
            }
            // category does not match
            if ((vm.categoryFilter !== 'All') && !ufPropertiesDB.matchCategory(prop, vm.categoryFilter)) {
                return false;
            }
            return true;
        };
        // filter for the value list (filters on propertyListFilter (0 => show All, 1 => show only modified))
        vm.valueListFilter = function (curValue) {
            if (vm.propertyListFilter === 0) {
                return true;
            }
            return ((vm.propertyListFilter === 1) && curValue.modified);
        };
        vm.goToPrograms = function () {
            ufMainView.go(uniflash.RegisteredViews.Program, vm.state.params.coreName);
        };
        vm.togglePinSetting = function () {
            vm.ufSettingsDisplayControlSrv.showPinSetting = !vm.ufSettingsDisplayControlSrv.showPinSetting;
        };
        vm.toggleDescriptionSetting = function () {
            vm.ufSettingsDisplayControlSrv.showDescription = !vm.ufSettingsDisplayControlSrv.showDescription;
        };
    }
})();
