/*
 * Copyright 2016 FUJITSU LIMITED
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import Promise from 'bluebird';
import defaultLogsIndexExists from './_logs_exists';
import defaultEventsIndexExists from './_events_exists';
import createLogsDefaultIndex from './_logs_create';
import createEventsDefaultIndex from './_events_create';
import deleteLogsDefaultIndex from './_logs_delete';
import deleteEventsDefaultIndex from './_events_delete';
import kibanaIndex from '../kibanaIndex';

export default (server, userObj) => {
  return () => {
    const indexName = kibanaIndex(server, userObj);
    return defaultLogsIndexExists(server, indexName, userObj)
      .then((logsExists) => {
        if (server.config().get('monasca-kibana-plugin.logs')) {
          server.log(['status', 'info', 'keystone'],
            `Default logs-index pattern is enabled in kibana config file`);
          if (!logsExists) {
            server.log(['status', 'warning', 'keystone'],
              `Default logs-index pattern for ${indexName} does not exist`);
            return createLogsDefaultIndex(server, indexName, userObj);
          } else {
            server.log(['status', 'info', 'keystone'],
              `Default logs-index pattern for ${indexName} already exists`);
          }

        } else {
          server.log(['status', 'info', 'keystone'],
            `Default logs-index pattern is disabled in kibana config file`);
          if (logsExists) {
            server.log(['status', 'warning', 'keystone'],
              `Default logs-index pattern for ${indexName} exists, but it should not`);
            return deleteLogsDefaultIndex(server, indexName, userObj);
          } else {
            server.log(['status', 'info', 'keystone'],
              `Default logs-index pattern for ${indexName} does not exist`);
          }
        }
      })
      .then(() => {
        defaultEventsIndexExists(server, indexName, userObj)
          .then((eventsExists) => {
            if (server.config().get('monasca-kibana-plugin.events')) {
              server.log(['status', 'info', 'keystone'],
                `Default events-index pattern is enabled in kibana config file`);
              if (!eventsExists) {
                server.log(['status', 'warning', 'keystone'],
                  `Default events-index pattern for ${indexName} does not exist`);
                return createEventsDefaultIndex(server, indexName, userObj);
              } else {
                server.log(['status', 'info', 'keystone'],
                  `Default events-index pattern for ${indexName} already exists`);
              }
            } else {
              server.log(['status', 'info', 'keystone'],
                `Default events-index pattern is disabled in kibana config file`);
              if (eventsExists) {
                server.log(['status', 'warning', 'keystone'],
                  `Default events-index pattern for ${indexName} exists, but it should not`);
                return deleteEventsDefaultIndex(server, indexName, userObj);
              } else {
                server.log(['status', 'info', 'keystone'],
                  `Default events-index pattern for ${indexName} does not exist`);
              }
            }
          });
      });
  };
};
