module Devops
  module API3
    module Routes
      module ProjectRoutes

        def self.registered(app)

          app.define_policy :read_projects, "Get projects list or project details"
          app.define_policy :create_projects, "Create projects"
          app.define_policy :delete_projects, "Delete projects"
          app.define_policy :update_projects, "Update projects"

          app.get_with_headers "/project_types" do
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).project_types
          end

          app.get_with_headers "/projects" do
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).projects.map(&:to_hash_list)
          end

          hash = {}
          hash["GET"] = lambda {|project|
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).project(project).to_hash
          }

          hash["PUT"] = lambda { |project|
            check_policy(:create_projects)
            r = Devops::API3::Handler::Project.new(request).update_project project
            create_response("Project '#{project}' has been updated.", {id: project})
          }

          hash["DELETE"] = lambda {|project|
            check_policy(:delete_projects)
            info = Devops::API3::Handler::Project.new(request).delete_project(project)
            create_response(info, {id: project})
          }
          app.multi_routes "/project/:project", hash

          app.post_with_headers "/project/:project/users/add" do |project|
            check_policy(:update_projects)
            users = Devops::API3::Handler::Project.new(request).add_project_users(project)
            create_response("Project '#{project}' has been updated.", project_users: users, id: project)
          end

          app.post_with_headers "/project/:project/users/delete" do |project|
            check_policy(:update_projects)
            users = Devops::API3::Handler::Project.new(request).delete_project_users(project)
            create_response("Project '#{project}' has been updated.", project_users: users, id: project)
          end

          app.post_with_headers "/project/:id/description" do |project|
            check_policy(:create_projects)
            Devops::API3::Handler::Project.new(request).set_project_description(project)
            create_response("Project description has been updated", {id: project})
          end

          app.post_with_headers "/project/:id/run_list/add" do |project|
            check_policy(:create_projects)
            rl = Devops::API3::Handler::Project.new(request).add_project_run_list(project)
            create_response("Project run_list has been updated", {id: project, run_list: rl})
          end

          app.put_with_headers "/project/:id/run_list" do |project|
            check_policy(:create_projects)
            rl = Devops::API3::Handler::Project.new(request).set_project_run_list(project)
            create_response("Project run_list has been updated", {id: project, run_list: rl})
          end

          app.post_with_headers "/project/:id/run_list/delete" do |project|
            check_policy(:create_projects)
            rl = Devops::API3::Handler::Project.new(request).delete_project_run_list(project)
            create_response("Project run_list has been updated", {id: project, run_list: rl})
          end

          app.post_with_headers "/project/:project/environment/:env/users/add" do |project, env|
            check_policy(:update_projects)
            users = Devops::API3::Handler::Project.new(request).add_project_env_users(project, env)
            create_response("Project '#{project}' and environment '#{env}' has been updated.", users: users)
          end

          app.post_with_headers "/project/:project/environment/:env/users/delete" do |project, env|
            check_policy(:update_projects)
            users = Devops::API3::Handler::Project.new(request).delete_project_env_users(project, env)
            create_response("Project '#{project}' and environment '#{env}' has been updated.", users: users)
          end

          app.get_with_headers "/project/:project/environment/:env/servers" do |project_id, env|
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).project_env_servers(project_id, env).map(&:to_hash)
          end

          app.get_with_headers "/project/:project/servers" do |project_id|
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).project_servers(project_id).map(&:to_hash)
          end

          # Deletes project servers
          #
          # * *Request*
          #   - method : DELETE
          #   - headers :
          #     - Accept: application/json
          #     - Content-Type: application/json
          #   - body :
          #     {
          #       "dry_run": false # set to true if you'd like to just see list of servers to delete
          #       "environment": null # set to env's identifier to delete that env's servers
          #     }
          #
          # * *Returns* :
          #   200 -
          # {
          #   "to_delete": ['server1', 'server2'],
          #   "deleted": ['server1'],
          #   "failed": ['server2']
          # }
          app.post_with_headers "/project/:project/environment/:env/servers/delete" do |project_id, env|
            check_policy(:update_projects)
            json Devops::API3::Handler::Project.new(request).delete_project_env_servers(project_id, env)
          end

          app.get_with_headers "/project/:project/environment/:env/stacks" do |project, env|
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request, env).project_env_stacks(project, env).map(&:to_hash)
          end

          app.get_with_headers "/project/:project/environments" do |project|
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).project_environments(project).map(&:to_hash)
          end

          app.post_with_headers "/project/:project/environment" do |project|
            check_policy(:create_projects)
            res, env = Devops::API3::Handler::Project.new(request).add_environment(project)
            create_response(res, {environment: env.to_hash}, 200)
          end

          app.get_with_headers "/project/:project/environment/:env/categories" do |project, env|
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).project_environment_categories(project, env).map(&:to_hash)
          end

          app.post_with_headers "/project/:project/environment/:env/category" do |project, env|
            check_policy(:update_projects)
            res, cat = Devops::API3::Handler::Project.new(request).add_category(project, env)
            create_response(res, {category: cat.to_hash}, 200)
          end

          app.get_with_headers "/project/:project/environment/:env/category/:id" do |project, env, category|
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).show_category(project, env, category).to_hash
          end

          app.delete_with_headers "/project/:project/environment/:env/category/:id" do |project, env, category|
            check_policy(:update_projects)
            res = Devops::API3::Handler::Project.new(request).delete_category(project, env, category)
            create_response(res, {category: category}, 200)
          end

          environment_hash = {}
          environment_hash["GET"] = lambda{|project, env|
            check_policy(:read_projects)
            json Devops::API3::Handler::Project.new(request).project_environment(project, env).to_hash
          }

          environment_hash["PUT"] = lambda{|id, environment|
            check_policy(:update_projects)
            begin
              res = Devops::API3::Handler::Project.new(request).update_environment(id, environment)
              create_response(res, nil, 200)
            rescue InvalidRecord => e
              halt_response(e.message)
            end
          }

          environment_hash["DELETE"] = lambda{|id, environment|
            check_policy(:delete_projects)
            begin
              res = Devops::API3::Handler::Project.new(request).delete_environment(id, environment)
              create_response(res, nil, 200)
            rescue InvalidRecord => e
              halt_response(e.message)
            end
          }
          app.multi_routes "/project/:id/environment/:environment", environment_hash

          app.put_with_headers "/project/:id/environment/:environment/update/:field" do |project_id, environment, field|
            check_policy(:create_projects)
            res = Devops::API3::Handler::Project.new(request).update_environment_field(project_id, environment, field)
            create_response(res, nil, 200)
          end

          app.post_with_headers "/project" do
            check_policy(:create_projects)
            begin
              res = Devops::API3::Handler::Project.new(request).create_project
              res = "Created. " + res
              create_response(res, nil, 201)
            rescue InvalidRecord => e
              halt_response(e.message)
            end
          end

=begin
          # Set components to project
          #
          # * *Request*
          #   - method : POST
          #   - headers :
          #     - Accept: application/json
          #     - Content-Type: application/json
          #   - body :
          #     components: {
          #       "some_id": {
          #         "filename" : "some.war"
          #       }
          #     }
          #
          # * *Returns* :
          #   200 - Updated
          app.post_with_headers "/project/:id/components", :headers => [:accept, :content_type] do |id|
            check_privileges("project", "w")
            res = Devops::API3::Handler::Project.new(request).set_project_components(id)
            create_response(res)
          end
=end


          app.post_with_headers "/project/:id/environment/:env/run_list" do |project, environment|
            check_policy(:create_projects)
            info = Devops::API3::Handler::Project.new(request).set_project_env_run_list(project, environment)
            create_response(info)
          end

          app.post_with_headers "/project/:id/deploy" do |project|
            check_policy(:create_projects)
            handler = Devops::API3::Handler::Project.new(request)
            if request["X-Stream"]
              stream() do |out|
                begin
                  status = handler.deploy_project_stream out, project
                  out << create_status(status)
                rescue IOError => e
                  logger.error e.message
                end
              end
            else
              json handler.deploy_project project
            end
          end

          app.post_with_headers "/project/:project/archive" do |project|
            check_policy(:create_projects)
            info = Devops::API3::Handler::Project.new(request).archive_project(project)
            create_response(info, id: project)
          end

          app.post_with_headers "/project/:project/unarchive" do |project|
            check_policy(:create_projects)
            info = Devops::API3::Handler::Project.new(request).unarchive_project(project)
            create_response(info, id: project)
          end

          app.post_with_headers "/project/test/:id/:env" do |project, environment|
            check_policy(:test_projects)
            json Devops::API3::Handler::Project.new(request).test_project(project, environment)
          end

          puts "Project routes initialized"
        end

      end
    end
  end
end

