require "devops-client/output/base"

module Output
  class Role < Base

    def table
      title, headers, rows = nil, nil, nil
      case options[:current_command]
      when :list
        headers, rows = prepare_list
        title = "output.title.role.list"
      when :show
        headers, rows = prepare_show
        title = "output.title.role.show"
      when :policies
        headers, rows = prepare_policies
        title = "output.title.role.policies"
      end

      create_table headers, rows, I18n.t(title)
    end

    def csv
      headers, rows = nil, nil
      case options[:current_command]
      when :list
        headers, rows = prepare_list
      when :show
        headers, rows = prepare_show
      when :policies
        headers, rows = prepare_policies
      end
      create_csv headers, rows
    end

    private
    def prepare_list
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.name"),
        I18n.t("output.table_header.description"),
        I18n.t("output.table_header.policies"),
        I18n.t("output.table_header.created_at")
      ]
      rows = @data.map do |role|
        [ role["id"], role["name"], role["description"], role["policies"], time_to_s(role["created_at"]) ]
      end
      return headers, rows
    end

    def prepare_show
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.name"),
        I18n.t("output.table_header.description"),
        I18n.t("output.table_header.policies"),
        I18n.t("output.table_header.created_at")
      ]
      rows = [[ @data["id"], @data["name"], @data["description"], @data["policies"], time_to_s(@data["created_at"]) ]]
      return headers, rows
    end

    def prepare_policies
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.description"),
        I18n.t("output.table_header.dependencies")
      ]
      rows = @data.map do |policy|
        [ policy["id"], policy["description"], policy["dependencies"] ]
      end
      return headers, rows
    end
  end
end
