=begin
 Changes to this file committed after and not including commit-id: ccc0d2c5f9a5ac661e60e6eaf138de7889928b8b
 are released under the following license:

 This file is part of Hopsworks
 Copyright (C) 2018, Logical Clocks AB. All rights reserved

 Hopsworks is free software: you can redistribute it and/or modify it under the terms of
 the GNU Affero General Public License as published by the Free Software Foundation,
 either version 3 of the License, or (at your option) any later version.

 Hopsworks is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 PURPOSE.  See the GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License along with this program.
 If not, see <https://www.gnu.org/licenses/>.

 Changes to this file committed before and including commit-id: ccc0d2c5f9a5ac661e60e6eaf138de7889928b8b
 are released under the following license:

 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
require 'pp'
require 'typhoeus'
require 'concurrent'

module ProjectHelper

  def getProjectId
    "#{Process.pid}"
  end
    
  def with_valid_project
    @project ||= create_project
    get "#{ENV['HOPSWORKS_API']}/project/#{@project[:id]}/dataset/?action=listing"
    if response.code != 200 # project and logged in user not the same
      @project = create_project
    end
    pp "valid project: #{@project[:projectname]}" if defined?(@debugOpt) && @debugOpt == true
  end

  def with_valid_tour_project(type)
    @project ||= create_project
    if type.eql?("spark")
      add_spark_tour_files
    end
    get "#{ENV['HOPSWORKS_API']}/project/#{@project[:id]}/dataset/?action=listing"
    pp "get #{ENV['HOPSWORKS_API']}/project/#{@project[:id]}/dataset/?action=listing" if defined?(@debugOpt) && @debugOpt == true
  end

  def add_spark_tour_files()
    copy_from_local("/srv/hops/spark/examples/jars/spark-examples_#{ENV['SPARK_EXAMPLE_VERSION']}.jar",
                    "/Projects/#{@project[:projectname]}/Resources/spark-examples.jar", @user[:username],
                    "#{@project[:projectname]}__Resources", 750, "#{@project[:projectname]}")
  end

  def create_project(projectName = nil, 
                     services: ["JOBS","JUPYTER","HIVE","KAFKA","SERVING", "FEATURESTORE", "AIRFLOW"],
                     validate_session: true)
    if validate_session
      with_valid_session
    end  
    pName = projectName == nil ? "project_#{getProjectId}_#{random_id_len(2)}" : projectName
    new_project = {projectName: pName, description:"", services: services, projectTeam:[]}
    post "#{ENV['HOPSWORKS_API']}/project", new_project
    expect_status_details(201)
    expect_json(successMessage: regex("Project created successfully.*"))
    get_project_by_name(new_project[:projectName])
  end

  # Do not use this method unless *you have to*. Prefer `create_project` instead
  # which tags the projects with the correct process id and so we are able to delete them.
  def create_project_by_name(projectname)
    with_valid_session
    pp "creating project: #{projectname}" if defined?(@debugOpt) && @debugOpt == true
    project = create_project(projectName = projectname, validate_session: false)
    pp "created project: #{project[:projectname]}" if defined?(@debugOpt) && @debugOpt == true
    project
  end

  def project_expect_status(status)
    body = JSON.parse(response.body)
    expect(response.code).to eq(resolve_status(status, response.code)), "found code:#{response.code} and body:#{body}"
  end

  def update_project(project)
    put "#{ENV['HOPSWORKS_API']}/project/#{project[:projectId]}", project
    expect_status_details(201)
  end

  def raw_delete_project(project, response, headers)
    if !headers["set_cookie"].nil? && !headers["set_cookie"][1].nil?
      cookie = headers["set_cookie"][1].split(';')[0].split('=')
      cookies = {"SESSIONID"=> JSON.parse(response.body)["sessionID"], cookie[0] => cookie[1]}
    else
      cookies = {"SESSIONID"=> JSON.parse(response.body)["sessionID"]}
    end
    request = Typhoeus::Request.new(
      "https://#{ENV['WEB_HOST']}:#{ENV['WEB_PORT']}#{ENV['HOPSWORKS_API']}/project/#{project[:id]}/delete",
      headers: {:cookies => cookies, 'Authorization' => headers["authorization"]},
      method: "post",
      followlocation: true,
      ssl_verifypeer: false,
      ssl_verifyhost: 0,
      forbid_reuse: true)
  end

  def delete_project(project)
    post "#{ENV['HOPSWORKS_API']}/project/#{project[:id]}/delete"
    expect_status_details(200)
    expect_json(successMessage: "The project and all related files were removed successfully.")
  end

  def add_member(member, role)
    with_valid_project
    add_member_to_project(@project, member, role)
  end

  def add_member_to_project(project, member, role)
    post "#{ENV['HOPSWORKS_API']}/project/#{project[:id]}/projectMembers", {projectTeam: [{projectTeamPK: {projectId: project[:id],teamMember: member},teamRole: role}]}
    expect_status_details(200)
    expect_json(successMessage: "One member added successfully")
  end

  def change_member_role(project, member, role)
    post "#{ENV['HOPSWORKS_API']}/project/#{project[:id]}/projectMembers/#{member}", URI.encode_www_form({ role: role}), {content_type: 'application/x-www-form-urlencoded'}
    expect_status_details(200)
  end

  def remove_member(project, member)
    delete "#{ENV['HOPSWORKS_API']}/project/#{project[:id]}/projectMembers/#{member}"
    expect_status_details(200)
  end

  def get_project
    @project
  end

  def get_project_by_name(name)
    Project.find_by(projectName: "#{name}")
  end

  def check_project_limit(limit=0)
    with_valid_session
    get "#{ENV['HOPSWORKS_API']}/users/profile"
    max_num_projects = json_body[:maxNumProjects]
    num_active_projects = json_body[:numActiveProjects]
    if (max_num_projects - num_active_projects) <= limit
      reset_session
      with_valid_project
    end
  end

  def force_remove(project)
    with_admin_session
    delete "#{ENV['HOPSWORKS_API']}/admin/projects/#{project[:id]}?force=true"
    pp "Force removed project:#{project[:projectname]}. Response: #{response.code}"
  end

  def create_max_num_projects
    get "#{ENV['HOPSWORKS_API']}/users/profile"
    max_num_projects = json_body[:maxNumProjects]
    num_active_projects = json_body[:numActiveProjects]
    while num_active_projects < max_num_projects
      post "#{ENV['HOPSWORKS_API']}/project", {projectName: "project_#{Time.now.to_i}"}
      get "#{ENV['HOPSWORKS_API']}/users/profile"
      max_num_projects = json_body[:maxNumProjects]
      num_active_projects = json_body[:numActiveProjects]
    end
  end

  def clean_projects
    with_valid_session
    get "#{ENV['HOPSWORKS_API']}/project/getAll"
    unless json_body.empty?
      json_body.map{|project| project[:id]}.each{|i| post "#{ENV['HOPSWORKS_API']}/project/#{i}/delete" }
    end
  end

  def on_complete(request, project)
    request.on_complete do |response|
      if response.success?
        pp "Delete project response: " + response.code.to_s
      elsif response.timed_out?
        pp "Timed out deleting project: #{response.body}"
      elsif response.code == 0
        pp response.return_message
      else
        pp "Delete project - failed with: #{response.code.to_s} body: #{response.body}."
        force_remove(project) # multiple threads can call this.
      end
    end
  end


  def clean_test_project(project, response, headers)
    request = raw_delete_project(project, response, headers)
    on_complete(request, project)
    pp "check elastic index: #{opensearch_check_project_deleted(project)}"
    request
  end

  def get_testing_projects(project_id: nil)
    Project.select('distinct(id), projectname, username')
        .where("projectname LIKE ? or projectname LIKE ? or projectname LIKE ? or projectname LIKE ? or
                projectname LIKE ? or projectname LIKE ? or projectname LIKE ? or projectname LIKE ?",
               'online_fs%',
               project_id == nil ? "project\_%" : "project\_#{project_id}_%",
               project_id == nil ? "ProJect\_%" : "ProJect\_#{project_id}_%",
               'demo\_%',
               'HOPSWORKS256%',
               'hopsworks256%',
               'prov\_proj\_%',
               'Project_DEMO_%')
  end

  # This function must be added under the first describe of each .spec file to ensure test projects are cleaned up properly
  def clean_all_test_projects(spec = "unknown", projectId = nil)
    projectId = projectId == nil ? getProjectId : projectId
    pp "Cleaning up test projects after #{spec} spec"

    starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    hydra = Typhoeus::Hydra.new(max_concurrency: 10)

    get_testing_projects(project_id: projectId).each { |project|
      response, headers = login_user(project[:username], "Pass123")
      if response.code == 200
        hydra.queue clean_test_project(project, response, headers)
      else
        pp "could not login and delete project:#{project[:projectname]} with user:#{project[:username]}"
      end
    }

    hydra.run
    ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    elapsed = ending - starting
    pp "Finished cleanup - time elapsed " + elapsed.to_s + "s"
  end

  def get_project_inode(project)
    projects_root_inode = INode.where(parent_id:1, name: "Projects")
    expect(projects_root_inode.length).to eq(1), "inode not found for project: #{project[:projectname]}"
    project_inode = INode.where(parent_id:projects_root_inode.first[:id], name: project[:projectname])
    @first = project_inode.first
    @first
  end

  def create_member_in_table(project, user, role)
    ProjectTeam.create(project_id: project[:id], team_member: user[:email], team_role: role)
  end

  def remove_member_from_table(project, user)
    ProjectTeam.where(project_id: project[:id], team_member: user[:email]).delete_all
  end

  def set_docker_image(project, docker_image)
    project.docker_image = docker_image
    project.save()
  end

  def get_kube_project_namespace(project_name)
    project_name.gsub("_","-").downcase
  end

  def get_project_teams_kube_config_map_name(project_name)
    namespace = get_kube_project_namespace(project_name)
    "#{namespace}-project-teams"
  end

  def get_project_teams_kube_config_map(project_name)
    cm_name = get_project_teams_kube_config_map_name(project_name)
    namespace = get_kube_project_namespace(project_name)
    get_kube_config_map(namespace, cm_name)
  end

  def get_serving_kube_config_map()
    get_kube_config_map("hops-system", "hops-system--serving")
  end

  def get_kube_config_map(namespace, cm_name)
    cm = nil
    kube_user = Variables.find_by(id: "kube_user").value
    cmd = "sudo su #{kube_user} /bin/bash -c \"kubectl get cm #{cm_name} --namespace=#{namespace} -o=json\""
    Open3.popen3(cmd) do |_, stdout, _, wait_thr|
      cm = stdout.read
    end
    if !cm.empty?
      cm = JSON.parse(cm)
    end
    cm
  end

  def update_authenticator_kube_config_map(user_roles, project_roles)
    kube_user = Variables.find_by(id: "kube_user").value
    ur = user_roles.map {|r| "\\\\\\\"#{r}\\\\\\\"" }.join(',')
    pr = project_roles.map {|r| "\\\\\\\"#{r}\\\\\\\"" }.join(',')
    # NOTE: we are not including "reSyncCacheSeconds" since it adds complexity and it is not used in ruby tests.
    cmd = "sudo su #{kube_user} /bin/bash -c \"kubectl patch cm hops-system--serving -n hops-system --type merge -p '{ \\\"data\\\": { \\\"authenticator\\\": \\\"{\\\\\\\"allowedUserRoles\\\\\\\":[#{ur}], \\\\\\\"allowedProjectUserRoles\\\\\\\":[#{pr}]}\\\" }}'\""
    result = nil
    Open3.popen3(cmd) do |_, stdout, _, wait_thr|
      result = stdout.read
    end
    result
  end
end
