require "devops-client/output/base"

module Output
  class StackTemplate < Base

    def table
      if outputting_list?
        title = I18n.t("output.title.stack_template.list")
        headers, rows = create_list
      else
        title = I18n.t("output.title.stack_template.show", :id => @data["id"])
        headers, rows = create_show
      end
      create_table headers, rows, title, with_num?
    end

    def csv
      if outputting_list?
        headers, rows = create_list
      else
        headers, rows = create_show
      end
      create_csv headers, rows, with_num?
    end

    def json
      JSON.pretty_generate(@data)
    end

  private
    # Note: shouldn't be in Base class
    def provider_given?
      !@options[:given_provider].nil?
    end

    def create_list
      abort(I18n.t("output.not_found.stack_template.list")) if @data.empty?

      fields_to_output = %w(id)
      fields_to_output << 'provider' unless provider_given?

      headers_and_rows(@data, fields_to_output)
    end

    def create_show
      rows = [ [ @data["id"], @data["provider"], @data["template_url"] ] ]
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.provider"),
        I18n.t("output.table_header.template_url")
      ]
      return headers, rows
    end

  end
end
