// @flow
import queryString from 'query-string';

import type { Config } from '../Domain/Config';
import type { EventList } from '../Domain/Event';
import type { Trigger, TriggerList, TriggerState } from '../Domain/Trigger';
import type { Settings } from '../Domain/Settings';
import type { TagStat } from '../Domain/Tag';
import type { PatternList } from '../Domain/Pattern';
import type { NotificationList } from '../Domain/Notification';

export type TagList = {|
    list: Array<string>;
|};

export type TagStatList = {|
    list: Array<TagStat>;
|};

export interface IMoiraApi {
    getPatternList(): Promise<PatternList>;
    getTagList(): Promise<TagList>;
    getTagStats(): Promise<TagStatList>;
    getSettings(): Promise<Settings>;
    getTriggerList(page: number, onlyProblems: boolean, tags: Array<string>): Promise<TriggerList>;
    getTrigger(id: string): Promise<Trigger>;
    setMaintenance(triggerId: string, data: { [metric: string]: number }): Promise<void>;
    getTriggerState(id: string): Promise<TriggerState>;
    getTriggerEvents(id: string, page: number): Promise<EventList>;
    delThrottling(triggerId: string): Promise<void>;
    delMetric(triggerId: string, metric: string): Promise<void>;
    getNotificationList(): Promise<NotificationList>;
}

export default class Api implements IMoiraApi {
    config: Config;

    constructor(config: Config) {
        this.config = config;
    }

    async getPatternList(): Promise<PatternList> {
        const url = `${this.config.apiUrl}/pattern`;
        const response = await fetch(url, { method: 'GET' });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }

    async getTagList(): Promise<TagList> {
        const url = `${this.config.apiUrl}/tag`;
        const response = await fetch(url, { method: 'GET' });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }

    async getTagStats(): Promise<TagStatList> {
        const url = `${this.config.apiUrl}/tag/stats`;
        const response = await fetch(url, { method: 'GET' });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }

    async getSettings(): Promise<Settings> {
        const url = `${this.config.apiUrl}/user/settings`;
        const response = await fetch(url, {
            method: 'GET',
        });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }

    async getTriggerList(page: number, onlyProblems: boolean, tags: Array<string>): Promise<TriggerList> {
        const url =
            this.config.apiUrl +
            '/trigger/page?' +
            queryString.stringify(
                {
                    /* eslint-disable */
                    p: page,
                    /* eslint-enable */
                    size: this.config.paging.triggerList,
                    tags,
                    onlyProblems,
                },
                { arrayFormat: 'index', encode: true }
            );
        const response = await fetch(url, { method: 'GET' });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }

    async getTrigger(id: string): Promise<Trigger> {
        const url = `${this.config.apiUrl}/trigger/${id}`;
        const response = await fetch(url, { method: 'GET' });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }

    async setMaintenance(triggerId: string, data: { [metric: string]: number }): Promise<void> {
        const url = `${this.config.apiUrl}/trigger/${triggerId}/maintenance`;
        const response = await fetch(url, {
            method: 'PUT',
            body: JSON.stringify(data),
        });
        if (response.status === 200) {
            return;
        }
        throw new Error(response);
    }

    async getTriggerState(id: string): Promise<TriggerState> {
        const url = `${this.config.apiUrl}/trigger/${id}/state`;
        const response = await fetch(url, { method: 'GET' });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }

    async getTriggerEvents(id: string, page: number): Promise<EventList> {
        const url = `${this.config.apiUrl}/event/${id}?p=${page}&size=${this.config.paging.eventHistory}`;
        const response = await fetch(url, { method: 'GET' });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }

    async delThrottling(triggerId: string): Promise<void> {
        const url = `${this.config.apiUrl}/trigger/${triggerId}/throttling`;
        const response = await fetch(url, { method: 'DELETE' });
        if (response.status === 200) {
            return;
        }
        throw new Error(response);
    }

    async delMetric(triggerId: string, metric: string): Promise<void> {
        const url = `${this.config.apiUrl}/trigger/${triggerId}/metrics?name=${metric}`;
        const response = await fetch(url, { method: 'DELETE' });
        if (response.status === 200) {
            return;
        }
        throw new Error(response);
    }

    async getNotificationList(): Promise<NotificationList> {
        const url = `${this.config.apiUrl}/notification?start=0&end=-1`;
        const response = await fetch(url, { method: 'GET' });
        if (response.status === 200) {
            return response.json();
        }
        throw new Error(response);
    }
}

@@ -1476,35 +1476,32 @@
     const url = 
-%60$%7B
 this.config.apiU
@@ -1506,17 +1506,20 @@
 iUrl
-%7D
+ + '
 /pattern
 %60;%0A 
@@ -1514,17 +1514,17 @@
 /pattern
-%60
+'
 ;%0A      
@@ -1761,35 +1761,32 @@
     const url = 
-%60$%7B
 this.config.apiU
@@ -1791,14 +1791,17 @@
 iUrl
-%7D
+ + '
 /tag
-%60
+'
 ;%0A  
@@ -2047,35 +2047,32 @@
     const url = 
-%60$%7B
 this.config.apiU
@@ -2073,17 +2073,20 @@
 g.apiUrl
-%7D
+ + '
 /tag/sta
@@ -2087,17 +2087,17 @@
 ag/stats
-%60
+'
 ;%0A      
@@ -2336,35 +2336,32 @@
     const url = 
-%60$%7B
 this.config.apiU
@@ -2362,17 +2362,20 @@
 g.apiUrl
-%7D
+ + '
 /user/se
@@ -2380,17 +2380,17 @@
 settings
-%60
+'
 ;%0A      
@@ -5464,35 +5464,32 @@
     const url = 
-%60$%7B
 this.config.apiU
@@ -5490,17 +5490,20 @@
 g.apiUrl
-%7D
+ + '
 /notific
@@ -5522,17 +5522,17 @@
 0&end=-1
-%60
+'
 ;%0A      

Fix codestyle