class Service {
    constructor($http) {
        this.$http = $http;
    }
    teste() {
        return this.$http;
    }
}
Service.$inject = ['$http'];
let app = angular.module('App', [
        'st.dialogs', 
        'ngAnimate', 
        'ngSanitize', 
        'ngTouch', 
        'ui.select', 
        'ui.grid', 
        'ui.grid.selection', 
        'ui.grid.pagination', 
        'ui.grid.edit', 
        'ui.grid.cellNav', 
        'ui.grid.exporter', 
        'ui.grid.resizeColumns', 
        'ui.grid.moveColumns', 
        'ui.grid.saveState', 
        'ui.grid.pinning', 
        'ui.grid.autoResize', 
        'ui.grid.validate', 
        'ui.grid.st', 
        'pascalprecht.translate'])
    .config(['StGridConfigProvider', function(StGridConfigProvider) {
        StGridConfigProvider.defaults = {
            options: { minimumColumnSize: 70, rowHeight: 24, enableRowSelection: true, enableSelectAll: true, enableSelectionBatchEvent: false, multiSelect: true, enableCellEditOnFocus: true, selectionRowHeaderWidth: 35, enableRowHeaderSelection: false, enableFiltering: true, enableSorting: true, showGridFooter: false, modifierKeysToMultiSelect: false, noUnselect: false, enableGridMenu: true, enableFullRowSelection: false, enableHighlighting: true, exporterMenuPdf: false, paginationPageSizes: [15, 30, 100], paginationPageSize: 15, data: [], exporterMenuExcel: true },
            firstColumns: {
                file: { name: 'updated', displayName: '', visible: true, enableCellEdit: false, width: '35', sort: {}, filters: [{}], enableFiltering: false, enableSorting: false, enableColumnMenu: false, enableHiding: false, pinnedLeft: true, enableColumnMoving: false, enableColumnResizing: false, exporterSuppressExport: true },
                grupo: { name: 'updated', displayName: '', visible: true, enableCellEdit: false, width: '35', sort: {}, filters: [{}], enableFiltering: false, enableSorting: false, enableColumnMenu: false, enableHiding: false, pinnedLeft: true, enableColumnMoving: false, enableColumnResizing: false, exporterSuppressExport: true }
            },
            lastColumns: { CommonEBS: [{ name: 'createdBy', displayName: 'createdBy', headerCellFilter: 'translate', visible: false, enableCellEdit: false }, { name: 'creationDate', displayName: 'creationDate', headerCellFilter: 'translate', visible: false, enableCellEdit: false, type: 'date', cellFilter: 'date:\'dd/MM/yyyy HH:mm:ss\'' }, { name: 'lastUpdatedBy', displayName: 'lastUpdatedBy', headerCellFilter: 'translate', visible: false, enableCellEdit: false }, { name: 'lastUpdateDate', displayName: 'lastUpdateDate', headerCellFilter: 'translate', visible: false, enableCellEdit: false, type: 'date', cellFilter: 'date:\'dd/MM/yyyy HH:mm:ss\'' }] },
            columnDefaults: {
                headerCellFilter: 'translate'
            },
            validators: {
                dup: {
                    perGrid: true,
                    func: (gridData, args, oldValue, newValue, rowEntity, colDef) => {
                        if (!newValue) return true;
                        else return !gridData.some(grid => grid[colDef.name] === rowEntity[colDef.name] && grid.$$hashKey !== rowEntity.$$hashKey);
                    },
                    msg: args => 'dupGridError'
                },
                empty: {
                    func: (args, oldValue, newValue) => {
                        console.log(args, oldValue, newValue);
                        return !isNU0(newValue);
                    },
                    msg: () => 'emptyGridError'
                }

            },
            columnModels: {
                id: {
                    name: 'id', displayName: 'id', visible: false, enableCellEdit: false, width: 70
                }
            }
        };
        StGridConfigProvider.config = {
            setData: true
        }
    }])
    .run(['$templateCache', function($templateCache) {
        $templateCache.put('cellUi/select.html', /*template*/ `
    <div class="ui-grid-cell-contents ui-grid-cell-contents-ui-select">
        <ui-select ng-model="MODEL_COL_FIELD" ui-select-st>
            <ui-select-match>
                <span ng-bind-html="$select.selected.name"></span>
            </ui-select-match>
            <ui-select-choices repeat="item in grid.appScope.selectItems | filter: $select.search track by $index">
                <div ng-bind-html="item.name"></div>
            </ui-select-choices>
        </ui-select>
    </div>`);
        $templateCache.put('cellUi/selectMultiple.html', /*template*/ `
    <div class="ui-grid-cell-contents ui-grid-cell-contents-ui-select">
        <ui-select 
                ng-model="MODEL_COL_FIELD" 
                ui-select-st 
                theme="ui-select-st"
                multiple 
                remove-on-list="{searchBy: 'name'}"
                close-on-select="false">
            <ui-select-match label="name" separator="/">
            </ui-select-match>
            <ui-select-choices repeat="item in grid.appScope.selectItems | filter: $select.search track by $index">
                <span ng-bind-html="item.name"></span>
            </ui-select-choices>
        </ui-select>
    </div>`);
    }])
    .service('Service', Service)
    .controller('Ctrl', function($scope, StGrid, $http, $timeout, StDialogs, Service) {
        let dateColor = {
            teste4: 'red',
            teste3: 'blue'
        }
        let selectItems = [
            { name: 'Option 1' }, { name: 'Option 2' }, { name: 'Option 3' }, { name: 'Option 4' }, { name: 'Option 5' }, { name: 'Option 6' }, { name: 'Option 7' }, { name: 'Option 8' }, { name: 'Option 9' }
        ]
        let grid = new StGrid(this, 'Grid', [
            { name: 'testeid', colModel: 'id' },
            { name: 'teste', displayName: 'teste', dateColor: dateColor, validators: { empty: true }, cellTemplate: `<div><span ng-style="{'color': col.colDef.dateColor[COL_FIELD]}" ng-bind="COL_FIELD"></span></div>` },
            { name: 'teste4', displayName: 'teste4', validators: {dup: true}},
            { name: 'teste5', displayName: 'teste5', cellTemplate: 'cellUi/selectMultiple.html', allowCellFocus: false, validators: { empty: true } },
            { name: 'teste6', displayName: 'teste6', cellTemplate: 'cellUi/select.html', allowCellFocus: false, validators: { empty: true } }
        ], 
        {});
        
        let grid2 = new StGrid(this, 'Grid2', [
            { name: 'id', displayName: 'id', visible: false, enableCellEdit: false, width: 70 },
            { name: 'teste', displayName: 'teste', validators: { dup: true } },
        ]);
        onInit = () => {
            grid.setDataApi($http.get('http://www.mocky.io/v2/5cd36f8d350000ca007a4f18', { params: { "mocky-delay": "1ms", teste: [1, 2, 3] } }), { setData: true});
            console.log(grid);
            console.log(grid2);
            console.log($scope);
        }
        getRows = () => {
            grid.setDataBackApi({
                url: 'http://www.mocky.io/v2/5cd36f8d350000ca007a4f18',
                params: {
                    "mocky-delay": "1ms"
                },
                method: 'GET'
            })
        }
        setLoading = () => {
            StDialogs.confirmDeletion();
        }
        addRow = () => {
            function t(){
                let a = [];
                for (let i = 0; i < 1000; i++) a.push({});
                return a;
            }
            grid.addRows(t());
            grid2.addRows();
        }
        removeRows = () => {
            grid.removeSelectedApi({ method: 'DELETE', url: 'https://www.mocky.io/v2/5185415ba171ea3a00704eed' }, 'id');
        }
        selectMatchFilter = (value) => {
            return value.map(o => o.name).join(' | ');
        }
        onSelect = ($item, rowEntity, colDef) => {
            rowEntity.updated = true;
            grid.validateService.runValidators(rowEntity, colDef, $item, `${$item}1`, grid);
        }
        onInit();
        angular.extend(this, {
            grid,
            grid2,
            getRows,
            setLoading,
            addRow,
            selectItems,
            removeRows,
            selectMatchFilter,
            onSelect
        })
    })