require "devops-client/output/base"

module Output
  class Environment < Base

    def table
      title = nil
      with_separator = false
      headers, rows = case options[:current_command]
      when :show
        title = I18n.t("output.title.environment.show", id: options[:env], project: options[:project])
        with_separator = true
        create_show(@data)
      when :servers
        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)
      when :test
        title = I18n.t("output.title.project.test", :project => ARGV[2], :env => ARGV[3])
        create_test(@data)
      else
        title = I18n.t("output.title.environment.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.environment.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
      row = [
        show["id"],
        show["categories"].map{|cat| cat["id"]},
        show["users"],
        show["run_list"],
        show["expires"]
      ]
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.categories"),
        I18n.t("output.table_header.users"),
        I18n.t("output.table_header.run_list"),
        I18n.t("output.table_header.expires")
      ]
      return headers, [row]
    end

    def create_servers servers
      abort(I18n.t("output.not_found.servers.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_test test
      rows = []
      headers = [
        I18n.t("output.table_header.server"),
        I18n.t("output.table_header.node_name"),
        I18n.t("output.table_header.creation"),
        I18n.t("output.table_header.bootstrap"),
        I18n.t("output.table_header.deletion")
      ]
      test["servers"].each do |s|
        rows.push [ s["id"],
                    s["chef_node_name"],
                    "#{s["create"]["status"]}\n#{s["create"]["time"]}",
                    "#{s["bootstrap"]["status"]}\n#{s["bootstrap"]["time"]}",
                    "#{s["delete"]["status"]}\n#{s["delete"]["time"]}" ]
      end
      return headers, rows
    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
