/* 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 {Helper} from '../utils/helper';
import {AppState} from '../app.service';

export const OWNER_ADMIN: string = 'admin';
export const OWNER_USER: string = 'user';
export const OWNER_RESELLER: string = 'reseller';

@Injectable()
export class Access {

    public userType: string;
    public prohibitedLimits = {};

    constructor(public appState: AppState) {
        this.userType = this.appState.get('userType');
        this.prohibitedLimits[OWNER_RESELLER] = {
            usage: {
                cpu: { mysql: true },
                vmem: true,
                io: {mysql: true },
                inodes: true,
            },
            limits: {
                cpu: { mysql: true },
                vmem: true,
                io: {mysql: true },
                inodes: true,
            },
            faults: {
                vmem: true,
                io: {mysql: true },
                inodes: true,
            },
        };
    }

    public dropPermission(data, etalon) {
        if (this.userType === OWNER_ADMIN) return;

        if (this.userType === OWNER_RESELLER
            && this.prohibitedLimits.hasOwnProperty(this.userType)
        ) {
            Helper.removeBranchFromObject(
                etalon, this.prohibitedLimits[this.userType],
            );
            if (data.hasOwnProperty('mySqlGov')) {
                data['mySqlGov'] = 'error';
            }
        }
    }
}
