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

import { Component } from '@angular/core';
import { Router } from '@angular/router';

import { AppState } from '../app.service';
import { XHR } from '../utils/xhr';
import { BaseComponent } from '../utils/base_component';
import { LoaderSelector } from '../utils/loader';


export const STATUS_PLUGIN_INSTALLED = 'plugin';
export const STATUS_FIREWALL_NOT_INSTALLED = 'no-firewall';
export const STATUS_SERVICE_DISABLED = 'disabled';

@Component({
    selector: 'imunify-status',
    styles: [
        require('./imunify-status.component.less'),
    ],
    providers: [],
    template: require('./imunify-status.component.html'),
})
export class ImunifyStatusComponent extends BaseComponent {
    public constructor(
        private router: Router,
        public appState: AppState,
        public request: XHR
    ) {
        super();
        if (![STATUS_PLUGIN_INSTALLED, STATUS_FIREWALL_NOT_INSTALLED, STATUS_SERVICE_DISABLED].includes(this.status)) {
            this.router.navigate(['incidents']);
        }
        this.loaderSelector = new LoaderSelector('.status-container');
    }


    public get status() {
        return this.appState.get('imunify-status');
    }

    public goToImunify(): void {
        window.location.href = 'imunify360/handlers/index.cgi';
    }

    public enableService(): void {
        this.loading = true;

        this.request.post({
            handler: 'imunify',
            command: 'setServiceStatus',
            status: 1
        }).then((data) => {
            this.loading = false;
            this.router.navigate(['incidents']);
        }).catch((err) => {
            this.loading = false;
        });

    }

    private isPluginInstalled(): boolean {
        return this.status === STATUS_PLUGIN_INSTALLED;
    }

    private isFirewallNotInstalled(): boolean {
        return this.status === STATUS_FIREWALL_NOT_INSTALLED;
    }

    private isServiceDisabled(): boolean {
        return this.status === STATUS_SERVICE_DISABLED;
    }
}
