/**
 * 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.
 */

(function() {
  'use strict';

  angular
    .module('horizon.dashboard.machine_learning.datasets')
    .factory('horizon.dashboard.machine_learning.datasets.datasetModel', datasetModel);

  datasetModel.$inject = [
    'horizon.app.core.openstack-service-api.meteos'
  ];

  function datasetModel(meteos) {
    var model = {
      newDatasetSpec: {},
      newCommonDataset: {},

      // API methods
      init: init,
      createDataset: createDataset
    };

    function initNewDatasetSpec() {
      model.newDatasetSpec = {
        method: null,
        display_name: null,
        display_description: null,
        source_dataset_url: null,
        experiment_id: null,
        params: null,
        percent_train: 0.8,
        percent_test: 0.2,
        swift_tenant: null,
        swift_username: null,
        swift_password: null
      };

      model.newCommonDataset = {
        location: null,
        format: 'csv',
        dataset_uuid: null,
        container_name: null,
        object_name: null,
        swift_tenant: null,
        swift_username: null,
        swift_password: null
      };
    }

    function init() {
      // Reset the new Dataset spec
      initNewDatasetSpec();
    }

    function createDataset() {
      var finalSpec = angular.copy(model.newDatasetSpec);
      var commonDataset = angular.copy(model.newCommonDataset);
      var url = "";

      if(commonDataset.location == 'swift'){
        url = 'swift://' +
              commonDataset.container_name + '/' +
              commonDataset.object_name;
      }else{
        url = 'internal://' + commonDataset.dataset_uuid;
      }

      finalSpec.source_dataset_url = url;
      finalSpec.swift_tenant = commonDataset.swift_tenant;
      finalSpec.swift_username = commonDataset.swift_username;
      finalSpec.swift_password = commonDataset.swift_password;

      return meteos.createDataset(finalSpec);
    }

    return model;
  }
})();