import Constants from '../../constants.js';
import fs from 'fs'
import superagent from 'superagent'

export default class GrafanaService {

    constructor(config) {
        this.request = superagent;
        this.config = config;
        this.fs = fs;
        this.filePath = './packages/eui-automation/core/services/grafana/';
    }

    async getDasboardJson(dashboadID) {
        try {
            let response = await this.request
                .get(this.config.endpoint + 'uid/' + dashboadID)
                .set('content-type', 'application/json')
                .auth(this.config.user, this.config.password);
            if (response._body) {
                return response._body;
            } else {
                console.error('Dashboard with uid ' + dashboadID + ' was not found');
                return null;
            }
        } catch (error) {
            console.error('Unable to get dashboard, error: ' + error);
            return null;
        }
    }

    async getPanelsInDashboard(dashboardID) {
        let dashboardJson = await this.getDasboardJson(dashboardID);
        return dashboardJson.dashboard.panels;
    }

    async addPanelsToDashboard(dashboardId, panelsToAddArray) {
        let panelFile;
        let panelFileStr;
        let newPanel;
        let dashboardJson = await this.getDasboardJson(dashboardId);
        Array.from(panelsToAddArray).forEach( panel => {
            switch (panel.type) {
                case Constants.Grafana.TextPanelType:
                    panelFile = this.filePath + 'textPanel.json'
                    break;
                case Constants.Grafana.TimeSeriesPanelType:
                    panelFile = this.filePath + 'timeSeriesPanel.json'
                    break;
                case Constants.Grafana.GaugePanelType:
                    panelFile = this.filePath + 'gaugePanel.json'
                    break;
                case Constants.Grafana.TablePanelType:
                    panelFile = this.filePath + 'tablePanel.json'
                    break;
                default:
                    panelFile = this.filePath + 'textPanel.json'
            }
            panelFileStr = this.fs.readFileSync(panelFile, 'utf8');
            newPanel = JSON.parse(panelFileStr);
            newPanel.title = panel.title;
            if (panel.type == Constants.Grafana.TextPanelType) {
                newPanel.options.content = panel.content;
            }
            if (panel.type == Constants.Grafana.TimeSeriesPanelType || panel.type == Constants.Grafana.GaugePanelType || panel.type == Constants.Grafana.TablePanelType) {
                newPanel.datasource.type = this.config.dataSourceType;
                newPanel.datasource.uid = this.config.dataSourceUid;
                newPanel.targets[0].datasource.type = this.config.dataSourceType;
                newPanel.targets[0].datasource.uid = this.config.dataSourceUid;
                newPanel.targets[0].query = panel.queryA;
            }
            if (panel.type == Constants.Grafana.GaugePanelType) {
                newPanel.targets[1].datasource.type = this.config.dataSourceType;
                newPanel.targets[1].datasource.uid = this.config.dataSourceUid;
                newPanel.targets[1].query = panel.queryB;
            }
            dashboardJson.dashboard.panels.push(newPanel);
        })
        
        try {
            let response = await this.request
                .post(this.config.endpoint + 'db')
                .auth(this.config.user, this.config.password)
                .type('json')
                .send(dashboardJson);
            if (response.statusCode !== 200) {
                throw new Error('New panels were not added to dashboard');
            }
        } catch (error) {
            throw new Error('Error on adding new panels: ' + error);
        }
    }

}
