=begin
Copyright (C) 2013 - 2018, Logical Clocks AB and RISE SICS AB. All rights reserved

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=end
module ProjectHelper
  def with_valid_project
    @project ||= create_project
    get "#{ENV['HOPSWORKS_API']}/project/#{@project[:id]}/dataset/getContent"
    if response.code != 200 # project and logged in user not the same 
      @project = create_project
    end
  end

  def create_project
    with_valid_session
    new_project = {projectName: "project_#{short_random_id}", description:"", status: 0, services: ["JOBS","HIVE","SERVING"], projectTeam:[], retentionPeriod: ""}
    post "#{ENV['HOPSWORKS_API']}/project", new_project
    expect_json(errorMsg: ->(value){ expect(value).to be_empty})
    expect_json(successMessage: regex("Project created successfully.*"))
    expect_status(201)
    get_project_by_name(new_project[:projectName])
  end
  
  def create_project_by_name(projectname)
    with_valid_session
    new_project = {projectName: projectname, description:"", status: 0, services: ["JOBS","HIVE","SERVING"], projectTeam:[], retentionPeriod: ""}
    post "#{ENV['HOPSWORKS_API']}/project", new_project
    expect_json(errorMsg: ->(value){ expect(value).to be_empty})
    expect_json(successMessage: regex("Project created successfully.*"))
    expect_status(201)
    get_project_by_name(new_project[:projectName])
  end
  
  def delete_project(project)
    post "#{ENV['HOPSWORKS_API']}/project/#{project[:id]}/delete"
    expect_json(errorMsg: "")
    expect_json(successMessage: "The project and all related files were removed successfully.")
    expect_status(200)
  end
  
  def add_member(member, role)
    with_valid_project
    post "#{ENV['HOPSWORKS_API']}/project/#{@project[:id]}/projectMembers", {projectTeam: [{projectTeamPK: {projectId: @project[:id],teamMember: member},teamRole: role}]}
    expect_json(errorMsg: ->(value){ expect(value).to be_empty})
    expect_json(successMessage: "One member added successfully")
    expect_status(200)
  end
  
  def get_all_projects
    projects = Project.find_by(username: "#{@user.email}")
    projects
  end
  
  def get_project
    @project
  end
 
  def get_project_by_name(name)
    Project.find_by(projectName: "#{name}")
  end
  
  def check_project_limit(limit=0)
    get "#{ENV['HOPSWORKS_API']}/user/profile"
    max_num_projects = json_body[:maxNumProjects]
    num_created_projects = json_body[:numCreatedProjects]
    if (max_num_projects - num_created_projects) <= limit
      reset_session
      with_valid_project
    end
    
  end
  
  def create_max_num_projects
    get "#{ENV['HOPSWORKS_API']}/user/profile"
    max_num_projects = json_body[:maxNumProjects]
    num_created_projects = json_body[:numCreatedProjects]
    while num_created_projects < max_num_projects
      post "#{ENV['HOPSWORKS_API']}/project", {projectName: "project_#{Time.now.to_i}"}
      get "#{ENV['HOPSWORKS_API']}/user/profile"
      max_num_projects = json_body[:maxNumProjects]
      num_created_projects = json_body[:numCreatedProjects]
    end
  end
  
  def clean_projects
    with_valid_session
    get "#{ENV['HOPSWORKS_API']}/project/getAll"
    if !json_body.empty?
      json_body.map{|project| project[:id]}.each{|i| post "#{ENV['HOPSWORKS_API']}/project/#{i}/delete" }
    end
  end
end
