/* Copyright CloudLinux Zug GmbH 2010-2017 All Rights Reserved
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 */

import { Injectable } from '@angular/core';
import { XHR } from '../utils/xhr';
import { AppState } from '../app.service';
import { Helper } from '../utils/helper';
import { NotificationsService } from '../services/notifiaction_service';
import { Panel } from '../utils/panel';

@Injectable()
export class ConfigRepo {

    private static promisePending: Promise<any> = null;
    private command: string  = 'cloudlinux-config';

    constructor(
        private appState: AppState,
        private request: XHR,
        private notifictions: NotificationsService
    ) {}

    public getConfig(): Promise<any> {
        if (ConfigRepo.promisePending === null) {
            ConfigRepo.promisePending = this.request.post({
                command: this.command,
                method: 'get',
            })
            .then(res => {
                ConfigRepo.promisePending = null;
                let mode = '';
                if (Helper.isObject(res.errors)) {
                    for (let param in res.errors) {
                        if (res.errors.hasOwnProperty(param)) {
                            this.notifictions.error('Error', res.errors[param]);
                        }
                    }
                }
                try {
                    mode = res.options.mySQLGovSettings.modeOfOperation;
                } catch (e){}
                this.appState.set('usersMysqlLimitsDisabled',
                    !mode || mode === 'all' || mode === 'off'
                );
                this.appState.set('mySqlGovMode', mode );
                return res;
            })
            .catch(() => {
                ConfigRepo.promisePending = null;
            });
        }
        return ConfigRepo.promisePending;
    }

    public saveConfig(options: {}) {
        return this.request.post({
            command: this.command,
            method: 'set',
            params: {'data': options},
        });
    }

    public resetInodesLimit() {
        return this.request.post({
            command: this.command,
            method: 'set',
            params: {'reset-inodes-limit': ''},
        });
    }

    public getAdmins() {
        if (this.appState.get('isAdmin')) {
            if (this.appState.get('panelName') === Panel.DA) {
                this.request.post({
                    command: 'spa-get-admins'
                }).then((data) => {
                    data.list.push('root');
                    this.appState.set('listOfAdmins', data.list);
                });
            } else if (this.appState.get('panelName') === Panel.PLESK) {
              this.appState.set('listOfAdmins', ['root', 'admin']);
            } else {
              this.appState.set('listOfAdmins', ['root']);
            }
        }
    }

    public async getDomains(): Promise<Array<string>> {
        let data = await this.request.post({
            command: 'spa-get-domains'
        });
        return data.list;
    }
}
