require "devops-client/output/base"

module Output
  class Project < Base

    NODE_HEADER = "Node number"
    SUBPROJECT_HEADER = "Subproject"

    def table
      title = nil
      with_separator = false
      headers, rows = case options[:current_command]
      when :show
        title = I18n.t("output.title.project.show", :name => @data["id"])
        with_separator = true
        create_show
      when :servers
        title = ARGV[2]
        title += " " + ARGV[3] unless ARGV[3].nil?
        title = I18n.t("output.title.project.servers", :title => title)
        create_servers(@data)
      when :stacks
        title = ARGV[2]
        title += " " + ARGV[3] unless ARGV[3].nil?
        title = I18n.t("output.title.project.stacks", :title => title)
        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.project.list")
        create_list
      end
      create_table(headers, rows, title, with_num?, with_separator)
    end

    def csv
      with_num = true
      headers, rows = case @additional_options[:output_type]
      when :list
        create_list
      when :show
        with_num = false
        create_show
      when :servers
        create_servers(@data)
      when :test
        create_test(@data)
      end
      create_csv(headers, rows, with_num?)
    end

  private
    def create_list
      abort(I18n.t("output.not_found.project.list")) if @data.empty?
      rows = @data.map {|l| [l["id"]]}
      headers = [ I18n.t("output.table_header.id") ]
      return headers, rows
    end

    def create_show
      rows = []
      rows = [ [@data["id"], @data["description"], @data["environments"].map{|e| e["id"]}, @data["owner"], @data["project_users"], @data["run_list"], time_to_s(@data["created_at"])] ]
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.description"),
        I18n.t("output.table_header.environments"),
        I18n.t("output.table_header.owner"),
        I18n.t("output.table_header.users"),
        I18n.t("output.table_header.run_list"),
        I18n.t("output.table_header.created_at")
      ]
      return headers, rows
    end

    def create_servers servers
      abort(I18n.t("output.not_found.project.servers", name: ARGV[2])) if servers.empty?
      rows = []
      servers.each do |s|
        rows.push [ s["project"], s["deploy_env"], s["chef_node_name"], s["remote_user"], s["provider"], s["id"] ]
      end
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.deploy_env"),
        I18n.t("output.table_header.node_name"),
        I18n.t("output.table_header.remote_user"),
        I18n.t("output.table_header.provider"),
        I18n.t("output.table_header.instance_id")
      ]
      return headers, rows
    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.project.stacks", name: ARGV[2])) 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
