require "app/api3/parsers/provider"
require_relative "request_handler"
require "commands/image"

module Devops
  module API3
    module Handler
      class Provider < RequestHandler

        set_parser Devops::API3::Parser::ProviderParser

        extend ImageCommands

        def flavors provider, account
          ::Provider.get_connector(provider, account).flavors
        end

        def networks provider, account
          p = ::Provider.get_connector(provider, account)
          available_keys = ["vpc-id"]
          p.networks_detail(@request.params.select{|k,v| available_keys.include?(k)})
        end

        def security_groups provider, account
          available_keys = ["vpc-id"]
          ::Provider.get_connector(provider, account).security_groups(@request.params.select{|k,v| available_keys.include?(k)})
        end

        def images provider, account
          Provider.get_available_provider_images(provider, account)
        end

        def available_providers
          DevopsConfig.config[:providers] || DevopsConfig.config["providers"] || []
        end

        def providers
          ::Provider.providers
        end

        def accounts provider
          provider_object = ::Provider.provider(provider)
          provider_object.accounts
        end

        def add_account provider
          provider_object = ::Provider.provider(provider)
          body = parser.account
          body["provider"] = provider
          account = provider_object.create_account(body)
          account.save!
          DevopsLogger.logger.info("Added #{provider} account '#{account.account_name}'")
          provider_object.create_connector(DevopsConfig.config, account)
          DevopsLogger.logger.info("Connector for provider '#{provider}' and account '#{account.account_name}' has been created")
          account
        rescue Mongoid::Errors::Validations => e
          raise Devops::Exception::ValidationError.create_from_db_exception(e)
        end

        def delete_account name, provider
          provider_object = ::Provider.provider(provider)
          account = provider_object.account(name)
          provider_object.delete_connector(name)
          DevopsLogger.logger.info("Connector for provider '#{provider}' and account '#{account.account_name}' has been removed")

          account_hash = account.to_hash
          account.delete
          DevopsLogger.logger.info("Removed #{provider} account '#{account.account_name}'")
          account_hash
        rescue Mongoid::Errors::DocumentNotFound
          raise Devops::Exception::RecordNotFound.new("Account '#{name}' not found")
        end

        def account_vpcs provider, name
          provider_object = ::Provider.provider(provider)
          account = provider_object.account(name)
          p = ::Provider.get_connector(provider, name)
          p.describe_vpcs
        rescue Mongoid::Errors::DocumentNotFound
          raise Devops::Exception::RecordNotFound.new("Account '#{name}' not found")
        end

      end
    end
  end
end
