require 'json'

module NexusCli
  class NexusCliError < StandardError
    class << self
      def status_code(code)
        define_method(:status_code) { code }
      end
    end
  end

  class ArtifactMalformedException < NexusCliError
    def message
      "Please submit your request using 4 colon-separated values. `groupId:artifactId:version:extension`"
    end
    status_code(100)
  end

  class ArtifactNotFoundException < NexusCliError
    def message
      "The artifact you requested information for could not be found. Please ensure it exists inside the Nexus."
    end
    status_code(101)
  end

  class InvalidSettingsException < NexusCliError
    def initialize(key)
      @missing_setting = key
    end

    def message
      "The .nexus_cli file or your overrides are missing the value: #{@missing_setting}"
    end
    status_code(102)
  end

  class MissingSettingsFileException < NexusCliError
    def message
      "The .nexus_cli file is missing or corrupt. You can either fix the .nexus_cli file or pass the --overrides hash."
    end
    status_code(103)
  end

  class NonSecureConnectionException < NexusCliError
    def message
      "Your communication with a server using an SSL certificate failed during validation. You may want to try the --insecure option."
    end
    status_code(104)
  end

  class CouldNotConnectToNexusException < NexusCliError
    def message
      "Could not connect to Nexus. Please ensure the url you are using is reachable."
    end
    status_code(105)
  end

  class PermissionsException < NexusCliError
    def message
      "Your request was denied by the Nexus server due to a permissions error. You will need to administer the Nexus or use a different user/password in .nexus_cli."
    end
    status_code(106)
  end

  class BadUploadRequestException < NexusCliError
    def message
      %{Your request was denied by the Nexus server due to a bad request and your artifact has not been uploaded.
This could mean several things:
  Your .nexus_cli['repository'] is invalid.
  The artifact with this identifier already exists inside the repository and that repository does not allow multiple deployments.}
    end
    status_code(107)
  end

  class NotNexusProException < NexusCliError
    def message
      "You cannot use this feature unless you are using Nexus Professional."
    end
    status_code(108)
  end

  class SearchParameterMalformedException < NexusCliError
    def message
      "Submit your search request specifying one or more 3 colon-separated values: `key:type:value`. The available search types are `equal`, `matches`, `bounded`, and `notequal`."
    end
    status_code(109)
  end

  class BadSearchRequestException < NexusCliError
    def message
      "Your request was denied by the Nexus server due to a bad request. Check that your search parameters contain valid values."
    end
    status_code(110)
  end

  class BadSettingsException < NexusCliError
    def initialize(body)
      @server_response = JSON.pretty_generate(JSON.parse(body))
    end

    def message
      %{Your global_settings.json file is malformed and could not be uploaded to Nexus.
The output from the server was:
  #{@server_response}}
    end
    status_code(111)
  end

  class CreateRepsitoryException < NexusCliError
    def initialize(body)
      @server_response = JSON.pretty_generate(JSON.parse(body))
    end

    def message
      %{Your create repository command failed due to the following:
        #{@server_response}}
    end
    status_code(112)
  end

  class RepositoryDoesNotExistException < NexusCliError
    def message
      "The repository you are trying to delete does not exist."
    end
    status_code(113)
  end

  class RepositoryNotFoundException < NexusCliError
    def message
      "The repository you requested information could not be found. Please ensure the repository exists."
    end
    status_code(114)
  end

  class UnexpectedStatusCodeException < NexusCliError
    def initialize(code)
      @code = code
    end

    def  message
      "The server responded with a #{@code} status code which is unexpected. Please submit a bug."
    end
    status_code(115)
  end

  class N3ParameterMalformedException < NexusCliError
    def message
      "Submit your tag request specifying one or more 2 colon-separated values: `key:value`. The key can only consist of alphanumeric characters."
    end
    status_code(116)
  end
end