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

import * as moment from 'moment';
import { Country, COUNTRIES } from './country.model';

export class Incident {
    public id: number;
    public name: string;
    public abuser: string;
    public description: string;
    public plugin: string;
    public retries: number;
    public rule: string;
    public severity: number;
    public country: Country;
    public listname: string;
    public time: any;
    public expanded: boolean = false;

    constructor(data) {
        this.abuser = data.abuser;
        if (!data.country) {
            this.country = new Country();
        }else {
            this.country = new Country(data.country.code, data.country.name);
        }
        this.description = data.description;
        this.id = data.id;
        this.listname = data.listname;
        this.name = data.name;
        this.plugin = data.plugin;
        this.retries = data.retries;
        this.rule = data.rule;
        this.severity = data.severity;
        this.time = moment.unix(new Date(data.timestamp).getTime()).fromNow();
    }
}

export class Search {
    private scheme = {
        'timeframe': {
            'data': {
                '10m': 'Last 10 minutes',
                '30m': 'Last 30 minutes',
                '1h': 'Last hour',
                '4h': 'Last 4 hours',
                'today': 'Today',
                'yesterday': 'Yesterday',
                '7d': 'Last 7 days',
                '30d': 'Last 30 days',
            },
            'default': 'empty',
            'empty': {
                '--since': 0,
            },
            'parameter': '--period',
        },
        'limit': {
            'data': {
                5: '5',
                25: '25',
                50: '50',
                75: '75',
                100: '100 entries',
            },
            'default': '25',
            'empty': '100',
            'parameter': '--limit',
            'no-close': '100',
        },
        'offset': {
            'data': null,
            'default': null,
            'empty': null,
            'parameter': '--offset',
        },
        'searchIP': {
            'data': null,
            'default': '',
            'empty': null,
            'selected': '',
            'parameter': '--by-abuser-ip',
            'validator': 'validateIP',
        },
        'searchCountry': {
            'data': null,
            'default': '',
            'empty': null,
            'selected': '',
            'parameter': '--by-country-code',
            'validator': 'validateCountry',
        }
    };

    public getParams() {
        return Object.keys(this.scheme).map(item => {
            let selected = this.getSelected(item);
            if (!selected) return;
            return typeof selected === 'object'
                ? selected : {[this.scheme[item].parameter]: this.getSelected(item)};
        });
    }

    public getData(item): {} {
        return this.scheme[item].data;
    }

    public getDataKeys(item): {} {
        return Object.keys(this.scheme[item].data);
    }

    public setSelected(item, value): void {
        return this.scheme[item]['selected'] = value;
    }

    public removeSelected(item): void {
        return this.scheme[item]['selected'] = this.scheme[item]['empty'] !== undefined
            ? this.scheme[item]['empty'] : '';
    }

    public getSelected(item): string {
        if (this.scheme[item]['selected'] === undefined) {
            let value = this.scheme[item].default === 'empty'
                ? this.scheme[item].empty : this.scheme[item].default;
            this.setSelected(item, value);
        }
        return this.scheme[item]['selected'];
    }

    public isSelected(item): boolean {
        return typeof this.scheme[item]['selected'] === 'string';
    }

    public hasClose(item): boolean {
        return this.scheme[item]['no-close'] !== this.scheme[item].selected;
    }

    public getSelectedName(item): {} {
        return this.getData(item)[this.scheme[item].selected];
    }

    public getItems(): {} {
        return Object.keys(this.scheme).filter(item => this.scheme[item]['data']);
    }

    public getCountryName(code: string): string {
        return COUNTRIES.find(item => item.code === code)['name'];
    }

    public validateIP(value: string): boolean {
        // tslint:disable-next-line:max-line-length
        let valid = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])?\.){0,3}(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])(?:\/(?:3[0-2]|[0-2]?[0-9])?)?)?$/.test(value);
        this.scheme['searchIP']['error'] = !valid ? 'Not valid part of IP address' : '';
        return valid;
    }

    public validateCountry(value: string): boolean {
        let valid = COUNTRIES.find(
            item => item.name.toLowerCase() === value.toLowerCase()
        );
        this.scheme['searchCountry']['error'] = !valid ? 'Not existing country' : '';
        return valid;
    }

    public validate(item, value) {
        return this[this.scheme[item].validator](value);
    }

    public getError(item: string): string {
        return this.scheme[item]['error'];
    }
}
