/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  package: 'foam.nanos.auth',
  name: 'Group',

  implements: [ 'foam.nanos.auth.EnabledAware' ],

  requires: [ 'foam.nanos.app.AppConfig' ],

  documentation: 'A Group of Users.',

  tableColumns: [ 'id', 'description', 'defaultMenu', 'parent' ],

  searchColumns: [ ],

  properties: [
    {
      class: 'String',
      name: 'id',
      documentation: 'Unique name of the Group.'
    },
    {
      class: 'Boolean',
      name: 'enabled',
      value: true
    },
    {
      class: 'String',
      name: 'description',
      documentation: 'Description of the Group.'
    },
    {
      class: 'Reference',
      name: 'parent',
      targetDAOKey: 'groupDAO',
      of: 'foam.nanos.auth.Group',
      documentation: 'Parent group to inherit permissions from.'
    },
    {
      class: 'FObjectArray',
      of: 'foam.nanos.auth.Permission',
      name: 'permissions',
      documentation: 'Permissions set on group.'
    },
    {
      class: 'Reference',
      targetDAOKey: 'menuDAO',
      name: 'defaultMenu',
      documentation: 'Menu user redirects to after login.',
      of: 'foam.nanos.menu.Menu'
    },
    {
      class: 'URL',
      name: 'logo',
      documentation: 'Group logo.'
    },
    {
      class: 'String',
      name: 'topNavigation',
      value: 'foam.nanos.u2.navigation.TopNavigation'
    },
    {
      class: 'String',
      name: 'footerView',
      value: 'foam.nanos.u2.navigation.FooterView'
    },
    {
      class: 'String',
      name: 'groupCSS',
      view: { class: 'foam.u2.tag.TextArea', rows: 16 },
    },
    {
      class: 'String',
      name: 'primaryColor',
      documentation: 'The following color properties can determine the color scheme of the GUI.'
    },
    { class: 'String', name: 'secondaryColor' },
    { class: 'String', name: 'tableColor' },
    { class: 'String', name: 'tableHoverColor' },
    { class: 'String', name: 'accentColor' },
/*    {
      class: 'FObjectProperty',
      of: 'foam.nanos.app.AppConfig',
      name: 'appConfig',
      factory: function() { return this.AppConfig.create(); },
      documentation: 'Custom application configuration for group.'
    }
*/
    /*
      FUTURE
    {
      class: 'FObjectProperty',
      of: 'AuthConfig',
      documentation: 'Custom authentication settings for this group.'
    }
    */
  ],

  methods: [
    {
      name: 'implies',
      javaReturns: 'Boolean',
      args: [
        {
          name: 'permission',
          javaType: 'java.security.Permission'
        }
      ],
      javaCode: `
        if ( getPermissions() == null ) return false;
        for ( int i = 0 ; i < permissions_.length ; i++ ) {
          if ( new javax.security.auth.AuthPermission(permissions_[i].getId()).implies(permission) ) {
            return true;
          }
        }
        return false;`
      ,
      code: function(permissionId) {
        if ( this.permissions == null ) return false;

        for ( var i = 0 ; i < this.permissions.length ; i++ )
          if ( this.permissions[i].implies(permissionId) ) return true;

        return false;
      }
    }
  ]
});

@@ -1277,11 +1277,13 @@
 s: '
-URL
+Image
 ',%0A 
@@ -1335,16 +1335,40 @@
 p logo.'
+,%0A      displayWidth: 60
 %0A    %7D,%0A
@@ -1474,24 +1474,48 @@
 pNavigation'
+,%0A      displayWidth: 45
 %0A    %7D,%0A    
@@ -1615,16 +1615,40 @@
 terView'
+,%0A      displayWidth: 45
 %0A    %7D,%0A
@@ -1753,16 +1753,26 @@
 rows: 16
+, cols: 60
  %7D,%0A    
@@ -1786,38 +1786,37 @@
 %7B%0A      class: '
-String
+Color
 ',%0A      name: '
@@ -1940,38 +1940,37 @@
 ,%0A    %7B class: '
-String
+Color
 ', name: 'second
@@ -1988,38 +1988,37 @@
 ,%0A    %7B class: '
-String
+Color
 ', name: 'tableC
@@ -2011,21 +2011,22 @@
  name: '
-table
+accent
 Color' %7D
@@ -2033,38 +2033,37 @@
 ,%0A    %7B class: '
-String
+Color
 ', name: 'tableH
@@ -2065,13 +2065,8 @@
 able
-Hover
 Colo
@@ -2085,22 +2085,21 @@
 class: '
-String
+Color
 ', name:
@@ -2092,38 +2092,42 @@
 'Color', name: '
-accent
+tableHover
 Color' %7D,%0A/*    

Set Group displayWidths.