/*
 *  Waltz
 * Copyright (c) David Watkins. All rights reserved.
 * The use and distribution terms for this software are covered by the
 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 * which can be found in the file epl-v10.html at the root of this distribution.
 * By using this software in any fashion, you are agreeing to be bound by
 * the terms of this license.
 * You must not remove this notice, or any other, from this software.
 *
 */

import _ from "lodash";
import {
    lifecyclePhaseDisplayNames,
    applicationKindDisplayNames,
    investmentRatingNames
} from "../../common/services/display_names";


const BINDINGS = {
    applications: '=',
    csvName: '@?'

};


function controller(uiGridConstants, $scope, $animate) {

    const vm = this;

    const csvName = angular.isDefined(vm.csvName) ? vm.csvName : 'applications.csv';

    vm.gridOptions = {
        enableGridMenu: true,
        exporterCsvFilename: csvName,
        exporterMenuPdf: false,
        enableSorting: true,
        enableFiltering: true,
        enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER,
        onRegisterApi: (gridApi) => {
            $animate.enabled(gridApi.grid.element, false);
        },
        columnDefs: [
            {
                field: 'name',
                cellTemplate: '<div class="ui-grid-cell-contents"> <a ui-sref="main.app.view ({ id: row.entity[\'id\'] })">{{ COL_FIELD }}</a></div>'
            },
            {
                field: 'kind',
                cellTemplate: '<div class="ui-grid-cell-contents"> {{ COL_FIELD | toDisplayName:"applicationKind" }}</div>',
                filter: {
                    type: uiGridConstants.filter.SELECT,
                    selectOptions: _.map(applicationKindDisplayNames, (label, value) => ({ label, value }))
                }
            },
            {
                field: 'overallRating',
                cellTemplate: '<div class="ui-grid-cell-contents"> {{ COL_FIELD | toDisplayName:"investmentRating" }}</div>',
                filter: {
                    type: uiGridConstants.filter.SELECT,
                    selectOptions: _.map(investmentRatingNames, (label, value) => ({ label, value }))
                }
            },
            {
                field: 'lifecyclePhase',
                cellTemplate: '<div class="ui-grid-cell-contents"> {{ COL_FIELD | toDisplayName:"lifecyclePhase" }}</div>',
                filter: {
                    type: uiGridConstants.filter.SELECT,
                    selectOptions: _.map(lifecyclePhaseDisplayNames, (label, value) => ({ label, value }))
                }
            },
            {
                field: 'description',
                cellTooltip: (row) => row.entity.description
            }
        ],
        data: vm.applications
    };

    $scope.$watch('ctrl.applications', (apps) => vm.gridOptions.data = apps);
}

controller.$inject = ['uiGridConstants', '$scope', '$animate'];


export default () => {
    return {
        restrict: 'E',
        replace: true,
        template: require('./app-table.html'),
        scope: {},
        bindToController: BINDINGS,
        controllerAs: 'ctrl',
        controller
    };
};

@@ -1463,36 +1463,24 @@
           %7D,
-%0A           
  %7B%0A         
@@ -1559,36 +1559,47 @@
 -cell-contents%22%3E
- %7B%7B 
+%3Cspan ng-bind=%22
 COL_FIELD %7C toDi
@@ -1608,17 +1608,18 @@
 layName:
-%22
+%5C'
 applicat
@@ -1625,20 +1625,27 @@
 tionKind
-%22 %7D%7D
+%5C'%22%3E%3C/span%3E
 %3C/div%3E',
@@ -1860,32 +1860,69 @@
 %7D%0A            %7D,
+ %7B%0A                field: 'assetCode'
 %0A            %7B%0A 
@@ -1909,32 +1909,35 @@
 ode'%0A           
+ %7D,
  %7B%0A             
@@ -2021,36 +2021,47 @@
 -cell-contents%22%3E
- %7B%7B 
+%3Cspan ng-bind=%22
 COL_FIELD %7C toDi
@@ -2070,17 +2070,18 @@
 layName:
-%22
+%5C'
 investme
@@ -2088,20 +2088,27 @@
 ntRating
-%22 %7D%7D
+%5C'%22%3E%3C/span%3E
 %3C/div%3E',
@@ -2321,36 +2321,24 @@
           %7D,
-%0A           
  %7B%0A         
@@ -2439,12 +2439,23 @@
 ts%22%3E
- %7B%7B 
+%3Cspan ng-bind=%22
 COL_
@@ -2476,17 +2476,18 @@
 layName:
-%22
+%5C'
 lifecycl
@@ -2496,12 +2496,26 @@
 hase
-%22 %7D%7D
+%5C'%22%3E%3C/span%3E%3C/span%3E
 %3C/di
@@ -2741,28 +2741,16 @@
       %7D,
-%0A           
  %7B%0A     

Add asset code to app list #204