require "devops-client/output/base"
require "devops-client/output/concerns/has_provider"

module Output
  class Image < Base
    include Concerns::HasProvider

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

    def csv
      title, headers, rows = nil, nil, nil
      if !@data.nil?
        headers, rows = create_list(@data, provider_given?)
      elsif !@data.nil?
        headers, rows = create_show
      end
      create_csv headers, rows, with_num?
    end

  private
    def create_list list, provider
      abort(I18n.t("output.not_found.image.list")) if list.empty?
      rows = []
      headers = if provider
        list.each {|l| rows.push [ l["name"], l["id"], l["status"] ]}
        [
          I18n.t("output.table_header.name"),
          I18n.t("output.table_header.id"),
          I18n.t("output.table_header.status")
        ]
      else
        list.each {|l| rows.push  [ l["id"], l["name"], l["remote_user"], l["provider"], time_to_s(l["created_at"]) ] }
        [
          I18n.t("output.table_header.id"),
          I18n.t("output.table_header.name"),
          I18n.t("output.table_header.remote_user"),
          I18n.t("output.table_header.provider"),
          I18n.t("output.table_header.created_at")
        ]
      end
      return headers, rows
    end

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

  end
end
