require "devops-client/output/base"

module Output
  class Category < Base

    def table
      title = nil
      with_separator = false
      headers, rows = case options[:current_command]
      when :show
        title = I18n.t("output.title.category.show", env: options[:env], project: options[:project], cat: options[:cat])
        with_separator = true
        create_show(@data)
      when :servers_list
        title = I18n.t("output.title.environment.servers", env: options[:env], project: options[:project])
        create_servers(@data)
      when :stacks
        title = I18n.t("output.title.environment.stacks", env: options[:env], project: options[:project])
        create_stacks(@data)
      else
        title = I18n.t("output.title.category.list", project: options[:project])
        create_list(@data)
      end
      create_table(headers, rows, title, with_num?, with_separator)
    end

    def csv
      with_num = true
      headers, rows = case @additional_options[:current_command]
      when :list
        create_list(@data)
      when :show
        with_num = false
        create_show(@data)
      when :servers
        create_servers(@data)
      when :test
        create_test(@data)
      end
      create_csv(headers, rows, with_num?)
    end

  private
    def create_list list
      abort(I18n.t("output.not_found.category.list")) if list.empty?
      rows = list.map {|l| [l["id"]]}
      headers = [ I18n.t("output.table_header.id") ]
      return headers, rows
    end

    def create_show show
      provider = show["provider"]
      cm_tool = show["cm_tool"]
      row = [
        show["id"],
        provider["name"],
        provider["account"],
        provider["flavor"],
        provider["image"],
        provider["security_groups"],
        provider["vpc_id"],
        provider["subnet"],
        cm_tool["name"],
        cm_tool["bootstrap_template"]
      ]
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.provider"),
        I18n.t("output.table_header.provider_account"),
        I18n.t("output.table_header.flavor"),
        I18n.t("output.table_header.image"),
        I18n.t("output.table_header.security_groups"),
        I18n.t("output.table_header.vpc_id"),
        I18n.t("output.table_header.subnet"),
        I18n.t("output.table_header.cm_tool"),
        I18n.t("output.table_header.bootstrap_template")
      ]
      return headers, [row]
    end

    def create_servers servers
      abort(I18n.t("output.not_found.category.servers", env: options[:env], project: options[:project])) if servers.empty?
      fields_to_output = %w(id name project environment category cm_name remote_user provider)
      headers_and_rows(servers, fields_to_output)
    end

    def create_stacks(stacks)
      abort(I18n.t("output.not_found.environment.stacks", env: options[:env], project: options[:project])) if stacks.empty?

      fields_to_output = %w(id deploy_env stack_template cloud_stack_id stack_status provider)

      headers_and_rows(stacks, fields_to_output)
    end

  end
end
