let inject = ['stGrids', 'StDialogs'];
export class StGridService {
    static get $inject(){
        return inject
    }
    constructor(...injectables){
        injectNg(injectables, inject, this);
    }
    /**
         * @ngdoc method
         * @name clearGrids
         *
         * @methodOf
         * StGridService
         *
         * @description
         * Clear all grids
    */
    clearGrids(){
        this.stGrids = {};
    }

    /**
         * @ngdoc method
         * @name get grids
         *
         * @methodOf
         * StGridService
         *
         * @description
         * Returns all grids
         * 
         * @return { Object } stGrids - An Object with all grids named
    */
    get grids(){
        return this.stGrids;
    }

    /**
         * @ngdoc method
         * @name _baseGetExists
         *
         * @methodOf
         * StGridService
         *
         * @description
         * Private function to get or returns true if the grid exists
         *
         * @param { string | number } id Id or name of the grid
         * @param { string } type Accepts only 'find' or 'some'
         * If 'find' it will return the grid (if it exists)
         * If 'some' it will return true or false depending if the grid exists or not
         * @return { Class | boolean } StGrid class or true/false
    */
    _baseGetExists(id, type){
        if (angular.isString(id)) return type === 'find' ? this.stGrids[id] : !!this.stGrids[id];
        else if (angular.isNumber(id)) return Object.values(this.stGrids)[type](o => o.api.grid.id === id);
    }

    /**
         * @ngdoc method
         * @name gridExists
         *
         * @methodOf
         * StGridService
         *
         * @description
         * Used to find out if a grid exists or not
         *
         * @param { string | number } id Id or name of the grid
         * @return { boolean } Returns an boolean indicating if the grid exists or not
    */
    gridExists(id){
        return this._baseGetExists(id, 'some');
    }

    /**
         * @ngdoc method
         * @name getGrid
         *
         * @methodOf
         * StGridService
         *
         * @description
         * Used to get the grid by name or id
         *
         * @param { string | number } id Id or name of the grid
         * @return { Class } StGrid class
    */
    getGrid(id){
        return this._baseGetExists(id, 'find');
    }

    /**
         * @ngdoc method
         * @name removeGrid
         *
         * @methodOf
         * StGridService
         *
         * @description
         * Remove one grid by the id or name
         *
         * @param { string | number } id Id or name of the grid
    */
    removeGrid(id){
        let { name } = this.getGrid(id);
        name ? delete this.stGrids[name] : '';
    }
    
    hasAnyUnsaved(config){
        return new Promise((resolve, reject) => {
            let unsaved = Object.keys(this.grids).some(o => this.getGrid(o).hasUnsavedData);
            if (unsaved) {
                if (config.confirm) {
                    let { msg } = config;
                    if (!msg.title) msg.title = 'ui-grid-st-confirm-unsaved-title';
                    if (!msg.msg) msg.msg = 'ui-grid-st-confirm-unsaved-msg';
                    this.StDialogs.confirm(msg.title, msg.msg).result.then(() => {
                        this.clearGrids();
                        resolve();
                    }, () => {
                        reject();
                    })
                } else {
                    reject();
                }
            } else {
                resolve();
            }
        })
    }
}