require "devops-client/output/base"

module Output
  class User < Base

    def table
      title, headers, rows = nil, nil, nil
      with_num = options[:with_num] || true

      case options[:current_command]
      when :list
        headers, rows = prepare_list
        title = "output.title.users.list"
      when :show
      end

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

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

  private
    def prepare_list
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.email"),
        I18n.t("output.table_header.roles"),
        I18n.t("output.table_header.created_at")
      ]
      rows = @data.map{|u| [ u["id"], u["email"], u["roles"], time_to_s(u["created_at"]) ] }
      [headers, rows]
    end

    def create_subheader
      [ [
        I18n.t("output.table_header.number"),
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.email"),
        I18n.t("output.table_header.image"),
        I18n.t("output.table_header.key"),
        I18n.t("output.table_header.project"),
        I18n.t("output.table_header.server"),
        I18n.t("output.table_header.users"),
        I18n.t("output.table_header.script"),
        I18n.t("output.table_header.filter"),
        I18n.t("output.table_header.flavor"),
        I18n.t("output.table_header.group"),
        I18n.t("output.table_header.network"),
        I18n.t("output.table_header.provider"),
        I18n.t("output.table_header.templates")
      ] ]
    end

    def create_rows
      abort(I18n.t("output.not_found.user.list")) if @data.nil? or @data.empty?
      rows = []
      @data.each_with_index do |l, i|
        next if l["privileges"].nil?

        flavor = "#{l["privileges"]["flavor"]}"
        group = "#{l["privileges"]["group"]}"
        image = "#{l["privileges"]["image"]}"
        project = "#{l["privileges"]["project"]}"
        server = "#{l["privileges"]["server"]}"
        key = "#{l["privileges"]["key"]}"
        user = "#{l["privileges"]["user"]}"
        filter = "#{l["privileges"]["filter"]}"
        network = "#{l["privileges"]["network"]}"
        provider = "#{l["privileges"]["provider"]}"
        script = "#{l["privileges"]["script"]}"
        templates = "#{l["privileges"]["templates"]}"

        rows.push  [ (i + 1).to_s, l["id"], l["email"], image, key, project, server, user, script, filter, flavor, group, network, provider, templates]
      end
      rows
    end
  end
end
