require "devops-client/handler/handler"
require "devops-client/options/provider_options"
require "devops-client/output/provider"

class Provider < Handler

  output_with Output::Provider

  def initialize(host, def_options={})
    @host, @options = host, def_options
  end

  def handle
    @options_parser = ProviderOptions.new(ARGV, @options)
    @command, @options, @args = @options_parser.parse_options_for!
    extract_parameters
    case @command
    when :account_create
      return account_create_handler
    when :account_delete
      return account_delete_handler
    end
    send("#{@command}_handler")
    output_data = {
      provider: @provider,
      account: @account
    }
    output(output_data)
  end

  def list_handler
    @list = get("/providers").sort!{|x,y| x["id"] <=> y["id"]}
  end

  def account_create_handler
    file = @options[:file]
    unless file.nil?
      json = File.read(file)
      begin
        JSON.parse(json)
      rescue JSON::ParserError => e
        abort(I18n.t("handler.provider.account.create.invalid_json", :file => file))
      end
      post_body("/provider/#{@provider}/account", json)
    else
      create_account
    end
  end

  def account_delete_handler
    delete "/provider/#{@provider}/account/#{@account}" if question(I18n.t("handler.provider.account.question.delete", :account => @account))
  end

  def create_account
    q = {
      "account_name" => @account
    }
    q["description"] = options[:description] || enter_parameter_or_empty(I18n.t("handler.provider.account.create.description") + ": ")
    q["ssh_key"] = options[:ssh_key] || resources_selector.select_available_ssh_keys

    case @provider
    when Providers::Aws
      q["use_iam_profile"] = options[:use_iam_profile] || question(I18n.t("handler.provider.account.create.aws.use_iam_profile"))
      unless q["use_iam_profile"]
        q["access_key_id"] = options[:access_key_id] || enter_parameter(I18n.t("handler.provider.account.create.aws.access_key_id") + ": ")
        q["secret_access_key"] = options[:secret_access_key] || enter_parameter(I18n.t("handler.provider.account.create.aws.secret_access_key") + ": ")
      end
    when Providers::Openstack
    when Providers::Static
      # nothing to do
    end
    json = JSON.pretty_generate(q)
    post_body "/provider/#{@provider}/account", json if question(I18n.t("handler.provider.account.question.create")){puts json}
  end

  def accounts_handler
    @list = get("/provider/#{@provider}/accounts")
  end

  def flavors_handler
    @list = get("/provider/#{@provider}/account/#{@account}/flavors").sort!{|x,y| x["id"] <=> y["id"]}
  end

  def images_handler
    @list = get("/provider/#{@provider}/account/#{@account}/images")
  end

  def networks_handler
    path = "/provider/#{@provider}/account/#{@account}/networks"
    path += "?vpc-id=#{@vpc_id}" if @vpc_id
    @list = get(path)
  end

  def vpc_handler
    abort(I18n.t("handler.provider.vpc.invalid_provider")) unless @provider == Providers::Aws
    @list = get("/provider/#{@provider}/account/#{@account}/vpcs")
  end

  def security_groups_handler
    path = "/provider/#{@provider}/account/#{@account}/security_groups"
    path += "?vpc-id=#{@vpc_id}" if @vpc_id
    @list = get(path)
  end
end
