import { find } from 'lodash';
import debug from 'debug';
import template from './show.html';

const logger = debug('redash:http');

function DataSourceCtrl(
  $scope, $route, $routeParams, $http, $location, toastr,
  currentUser, AlertDialog, Events, DataSource,
) {
  Events.record('view', 'page', 'admin/data_source');

  $scope.dataSource = $route.current.locals.dataSource;
  $scope.dataSourceId = $routeParams.dataSourceId;
  $scope.types = $route.current.locals.types;
  $scope.type = find($scope.types, { type: $scope.dataSource.type });
  $scope.canChangeType = $scope.dataSource.id === undefined;

  $scope.helpLinks = {
    athena: 'http://help.redash.io/article/122-amazon-athena-setup',
    bigquery: 'http://help.redash.io/article/124-bigquery-setup',
    url: 'http://help.redash.io/article/120-using-a-url-as-a-data-source',
    mongodb: 'http://help.redash.io/article/157-mongodb-setup',
    google_spreadsheets: 'http://help.redash.io/article/126-google-spreadsheets-setup',
    google_analytics: 'http://help.redash.io/article/125-google-analytics-setup',
    axibasetsd: 'http://help.redash.io/article/123-axibase-time-series-database',
    results: 'http://help.redash.io/article/152-query-results-data-source',
  };

  $scope.$watch('dataSource.id', (id) => {
    if (id !== $scope.dataSourceId && id !== undefined) {
      $location.path(`/data_sources/${id}`).replace();
    }
  });

  $scope.setType = (type) => {
    $scope.type = type;
    $scope.dataSource.type = type.type;
  };

  $scope.resetType = () => {
    $scope.type = undefined;
    $scope.dataSource = new DataSource({ options: {} });
  };

  function deleteDataSource(callback) {
    const doDelete = () => {
      Events.record('delete', 'datasource', $scope.dataSource.id);

      $scope.dataSource.$delete(() => {
        toastr.success('Data source deleted successfully.');
        $location.path('/data_sources/');
      }, (httpResponse) => {
        logger('Failed to delete data source: ', httpResponse.status, httpResponse.statusText, httpResponse.data);
        toastr.error('Failed to delete data source.');
      });
    };

    const title = 'Delete Data source';
    const message = `Are you sure you want to delete the "${$scope.dataSource.name}" data source?`;
    const confirm = { class: 'btn-warning', title: 'Delete' };

    AlertDialog.open(title, message, confirm).then(doDelete, callback);
  }

  function testConnection(callback) {
    Events.record('test', 'datasource', $scope.dataSource.id);

    DataSource.test({ id: $scope.dataSource.id }, (httpResponse) => {
      if (httpResponse.ok) {
        toastr.success('Success');
      } else {
        toastr.error(httpResponse.message, 'Connection Test Failed:', { timeOut: 10000 });
      }
      callback();
    }, (httpResponse) => {
      logger('Failed to test data source: ', httpResponse.status, httpResponse.statusText, httpResponse);
      toastr.error('Unknown error occurred while performing connection test. Please try again later.', 'Connection Test Failed:', { timeOut: 10000 });
      callback();
    });
  }

  $scope.actions = [
    { name: 'Delete', class: 'btn-danger', callback: deleteDataSource },
    {
      name: 'Test Connection', class: 'btn-default pull-right', callback: testConnection, disableWhenDirty: true,
    },
  ];
}

export default function init(ngModule) {
  ngModule.controller('DataSourceCtrl', DataSourceCtrl);

  return {
    '/data_sources/new': {
      template,
      controller: 'DataSourceCtrl',
      title: 'Datasources',
      resolve: {
        dataSource: (DataSource) => {
          'ngInject';

          return new DataSource({ options: {} });
        },
        types: ($http) => {
          'ngInject';

          return $http.get('api/data_sources/types').then(response => response.data);
        },
      },
    },
    '/data_sources/:dataSourceId': {
      template,
      controller: 'DataSourceCtrl',
      title: 'Datasources',
      resolve: {
        dataSource: (DataSource, $route) => {
          'ngInject';

          return DataSource.get({ id: $route.current.params.dataSourceId }).$promise;
        },
        types: ($http) => {
          'ngInject';

          return $http.get('api/data_sources/types').then(response => response.data);
        },
      },
    },
  };
}

@@ -637,32 +637,28 @@
 thena: 'http
+s
 ://
-help.
 redash.io/ar
@@ -659,20 +659,26 @@
 .io/
-article/122-
+help/data-sources/
 amaz
@@ -710,32 +710,28 @@
 query: 'http
+s
 ://
-help.
 redash.io/ar
@@ -732,20 +732,26 @@
 .io/
-article/124-
+help/data-sources/
 bigq
@@ -773,32 +773,28 @@
   url: 'http
+s
 ://
-help.
 redash.io/ar
@@ -795,48 +795,39 @@
 .io/
-article/120-us
+help/data-sources/query
 ing-
-a-
 url
--as-a-data-source
+s
 ',%0A 
@@ -839,32 +839,28 @@
 ngodb: 'http
+s
 ://
-help.
 redash.io/ar
@@ -861,20 +861,26 @@
 .io/
-article/157-
+help/data-sources/
 mong
@@ -917,32 +917,28 @@
 heets: 'http
+s
 ://
-help.
 redash.io/ar
@@ -939,19 +939,36 @@
 .io/
-article/126
+help/data-sources/querying-a
 -goo
@@ -982,23 +982,16 @@
 eadsheet
-s-setup
 ',%0A    g
@@ -1008,32 +1008,28 @@
 ytics: 'http
+s
 ://
-help.
 redash.io/ar
@@ -1030,20 +1030,26 @@
 .io/
-article/125-
+help/data-sources/
 goog
@@ -1086,32 +1086,28 @@
 setsd: 'http
+s
 ://
-help.
 redash.io/ar
@@ -1108,20 +1108,26 @@
 .io/
-article/123-
+help/data-sources/
 axib
@@ -1175,16 +1175,12 @@
 http
+s
 ://
-help.
 reda
@@ -1189,20 +1189,33 @@
 .io/
-article/152-
+help/user-guide/querying/
 quer

Update datasource documentation links