require_relative "request_parser"

module Devops
  module API3
    module Parser
      class ProjectParser < RequestParser

        include Devops::API3::Helpers

        def projects
          fields = []
          if @params.key?("fields") and @params["fields"].is_a?(Array)
            Devops::Model::Project.list_fields.each do |k|
              fields.push k if @params["fields"].include?(k)
            end
          end
          fields
        end

        def archived_projects
          @params.include?("archived")
        end

        def project_servers
          @params["environment"]
        end

        def project_stacks
          @params["environment"]
        end

        def create_project
          body = create_object_from_json_body
          project = Devops::Model::Project.new(body)
          #project.validate!
          project
        end

        def users
          create_object_from_json_body(Array)
        end

        def set_description
          body = create_object_from_json_body
          body["description"]
        end

        def set_project_components
          c = create_object_from_json_body(Hash)
          raise InvalidRecord.new("'components' key not found") if c["components"].nil?
          c
        end

        def add_environment
          body = create_object_from_json_body
          Devops::Model::Environment.new(body)
        end

        def update_environment_field
          body = create_object_from_json_body
          raise InvalidRecord.new("'value' key not found") if body["value"].nil?
          body["value"]
        end

        def update_environment
          body = create_object_from_json_body
#          rl = check_array(body["run_list"], "Parameter 'run_list' should be an array of string", String, false, true)
#          Validators::Helpers::RunList.new(rl).validate!
          Devops::Model::EnvironmentFactory.create(body)
        end

        def update
          body = create_object_from_json_body
=begin
          check_string(body["description"], "Parameter 'description' must be a string", true, true)
          rl = check_array(body["run_list"], "Parameter 'run_list' must be an array of string", String, true, true)
          Validators::Helpers::RunList.new(rl).validate! unless rl.nil?
          #Devops::Model::Project.new(body)
=end
          body
        end

        def add_category
          Devops::Model::Category.new(create_object_from_json_body)
        end

        def delete
          body = create_object_from_json_body(Hash, true)
          environment = unless body.nil?
            check_string(body["environment"], "Parameter 'environment' should be a not empty string", true)
          end
        end

        def project_users
          body = create_object_from_json_body
          users = check_array(body["users"], "Parameter 'users' must be a not empty array of strings")
          environment = check_string(body["environment"], "Parameter 'environment' must be a not empty string", true)
          return environment, users
        end

        def run_list
          list = create_object_from_json_body(Array)
          RunListArrayValidator.validate_list(list)
          list
        end

        def deploy
          obj = create_object_from_json_body
          environment = check_string(obj["environment"], "Parameter 'environment' should be a not empty string", true)
          servers = check_array(obj["servers"], "Parameter 'servers' should be a not empty array of strings", String, true)
          return environment, servers
        end

        def delete_project_env_servers
          body = create_object_from_json_body
          dry_run = check_boolean(body["dry_run"], "Parameter 'dry_run' must be a boolean")
          dry_run
        end
      end
    end
  end
end
