(function () {
    'use strict';
    angular.module('uniflash').directive('ufPropertyCategory', propertyCategory);
    function propertyCategory() {
        var directive = {
            restrict: 'E',
            templateUrl: 'flash_properties/category.html',
            controller: PropertyCategoryController,
            controllerAs: 'vm',
            link: function (scope, element, attrs) {
                scope.vm.category = attrs.category;
                scope.vm.title = attrs.title;
                scope.vm.showButton = attrs.showbutton;
                scope.vm.expand = (attrs.expand === 'true');
                scope.vm.minimal = attrs.minimal;
                scope.vm.hideQuick = attrs.hidequick;
                scope.vm.searchable = (attrs.searchable === 'true');
                scope.vm.toggle = (attrs.toggle === 'true');
                scope.vm.read = (attrs.read === 'true');
            },
            scope: {}
        };
        return directive;
    }
    PropertyCategoryController.$inject = ['ufPropertiesDB', 'ufPropertiesValue', 'propertiesActionSrv', 'ufPrograms', 'ufQuickSettingsSrv', 'ufSettingsDisplayControlSrv', 'ufContext', 'ufDsProgressDialog'];
    function PropertyCategoryController(ufPropertiesDB, ufPropertiesValue, propertiesActionSrv, ufPrograms, ufQuickSettingsSrv, ufSettingsDisplayControlSrv, ufContext, ufDsProgressDialog) {
        var vm = this;
        vm.ufPropertiesDB = ufPropertiesDB;
        vm.ufPropertiesValue = ufPropertiesValue;
        vm.propertiesActionSrv = propertiesActionSrv;
        vm.ufPrograms = ufPrograms;
        vm.ufQuickSettingsSrv = ufQuickSettingsSrv;
        vm.ufSettingsDisplayControlSrv = ufSettingsDisplayControlSrv;
        vm.ufContext = ufContext;
        // toggles all boolean properties in this category
        vm.toggling = function (mode) {
            _.each(vm.ufPropertiesDB.getPropertiesListByCategory([vm.category], false, vm.ufContext.getCurrentCore().name, vm.searchable), function (prop) {
                if (prop.valueType === 'Boolean') {
                    var valueObj = ufPropertiesValue.getValueObj(prop._id, vm.ufContext.getCurrentCore().name);
                    // only if it is not disabled
                    if (!valueObj.disabled) {
                        if (mode === 'flip') {
                            valueObj.value = !valueObj.value;
                        }
                        else if (mode === 'on') {
                            valueObj.value = true;
                        }
                        else if (mode === 'off') {
                            valueObj.value = false;
                        }
                        valueObj.modified = true;
                    }
                }
            });
        };
        // connect to the current core to read device information
        vm.connect = function () {
            var dsCore = ufDsProgressDialog.getDSCore(ufContext.getCurrentCore().name);
            return dsCore.run('getTargetInfo');
        };
    }
})();
