require "devops-client/output/base"

module Output
  class Server < Base

    def table
      title = nil
      headers, rows = if outputting_list?
        case options[:type]
        when "chef"
          title = I18n.t("output.title.server.chef")
        when "openstack"
          title = I18n.t("output.title.server.openstack")
        when "ec2"
          title = I18n.t("output.title.server.ec2")
        else
          title = I18n.t("output.title.server.list")
        end
        create_list
      else
        title = I18n.t("output.title.server.show", :name => options[:server])
        create_show
      end
      create_table headers, rows, title
    end

    def csv
      headers, rows = if outputting_list?
        create_list
      else
        create_show
      end
      create_csv headers, rows
    end

  private
    def create_list
      list = @data
      abort(I18n.t("output.not_found.server.list")) if list.empty?
      rows, keys = [], nil
      headers = case options[:type]
      when "chef"
        keys = ["chef_node_name"]
        title = "Chef servers"
        [I18n.t("output.table_header.node_name")]
      when "openstack"
        keys = ["instance_id", "name", "public_ip", "private_ip", "keypair", "flavor", "image", "state"]
        title = "Openstack servers"
        [
          I18n.t("output.table_header.instance_id"),
          I18n.t("output.table_header.node_name"),
          I18n.t("output.table_header.public_ip"),
          I18n.t("output.table_header.private_ip"),
          I18n.t("output.table_header.keypair"),
          I18n.t("output.table_header.flavor"),
          I18n.t("output.table_header.image"),
          I18n.t("output.table_header.state")
        ]
      when "ec2"
        keys = ["instance_id", "name", "ip", "private_ip", "dns_name", "keypair", "flavor", "image", "zone", "state", "launched_at"]
        title = "Ec2 servers"
        [
          I18n.t("output.table_header.instance_id"),
          I18n.t("output.table_header.node_name"),
          I18n.t("output.table_header.public_ip"),
          I18n.t("output.table_header.private_ip"),
          I18n.t("output.table_header.dns"),
          I18n.t("output.table_header.keypair"),
          I18n.t("output.table_header.flavor"),
          I18n.t("output.table_header.image"),
          I18n.t("output.table_header.zone"),
          I18n.t("output.table_header.state"),
          I18n.t("output.table_header.created_at")
        ]
      else
        keys = ["id", "name"]
        title = "Servers"
        [
          I18n.t("output.table_header.instance_id"),
          I18n.t("output.table_header.name")
        ]
      end
      list.each do |l|
        row = []
        keys.each{|k| row.push l[k]}
        rows.push row
      end
      return headers, rows
    end

    def create_show
      rows = []
      headers = [
        I18n.t("output.table_header.instance_id"),
        I18n.t("output.table_header.name"),
        I18n.t("output.table_header.project"),
        I18n.t("output.table_header.environment"),
        I18n.t("output.table_header.category"),
        I18n.t("output.table_header.provider"),
        I18n.t("output.table_header.remote_user"),
        I18n.t("output.table_header.private_ip"),
        I18n.t("output.table_header.created_by"),
        I18n.t("output.table_header.created_at")
      ]
      keys = ["id", "name", "project", "environment", "category", "provider", "remote_user", "private_ip", "created_by"]
      row = []
      keys.each{|k| row.push @data[k]}
      row.push time_to_s(@data["created_at"])
      rows.push row
      return headers, rows
    end

  end
end
