require "devops-client/output/base"

module Output
  class Provider < Base

    def table
      case options[:current_command]
      when :list
        headers, rows = create_list
        title = I18n.t("output.title.provider.list")
      when :accounts
        headers, rows = create_accounts(options[:provider])
        title = I18n.t("output.title.provider.accounts", provider: options[:provider])
      when :flavors
        headers, rows = create_flavors(options[:provider])
        title = I18n.t("output.title.provider.flavors", provider: options[:provider], account: options[:account])
      when :images
        headers, rows = create_images(options[:provider])
        title = I18n.t("output.title.provider.images", provider: options[:provider], account: options[:account])
      when :security_groups
        headers, rows = create_security_groups(options[:provider])
        title = I18n.t("output.title.provider.security_groups", provider: options[:provider], account: options[:account])
      when :vpc
        headers, rows = create_vpc
        title = I18n.t("output.title.provider.vpcs", provider: options[:provider], account: options[:account])
      when :networks
        headers, rows = create_networks(options[:provider])
        title = I18n.t("output.title.provider.networks", provider: options[:provider], account: options[:account])
      end
      create_table(headers, rows, title)
    end

    def csv
      case options[:current_command]
      when :list
        headers, rows = create_list
      when :accounts
        headers, rows = create_accounts
      when :flavors
        headers, rows = create_flavors(options[:provider])
      when :images
        headers, rows = create_images(options[:provider])
      when :security_groups
        headers, rows = create_security_groups(options[:provider])
      when :vpc
        headers, rows = create_vpc
      when :networks
        headers, rows = create_networks(options[:provider])
      end
      create_csv(headers, rows, with_num?)
    end

    private

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

    def create_accounts provider
      headers = [
        I18n.t("output.table_header.id"),
        I18n.t("output.table_header.description"),
        I18n.t("output.table_header.ssh_key")
      ]
      keys = %w(account_name description ssh_key)
      case provider
      when Providers::Aws
        headers.push I18n.t("output.table_header.use_iam_profile")
        keys.push "use_iam_profile"
      when Providers::Openstack
      when Providers::Static
      end
      headers.push I18n.t("output.table_header.created_at")
      rows = @data.map do |account|
        keys.map{|key| account[key]}.push(time_to_s(account["created_at"]))
      end
      [headers, rows]
    end

    def create_flavors provider
      abort(I18n.t("output.not_found.flavor.list")) if @data.nil? or @data.empty?
      headers = nil
      rows = []
      case provider
      when Providers::Aws
        headers = [
                    I18n.t("output.table_header.name"),
                    I18n.t("output.table_header.id"),
                    I18n.t("output.table_header.virtual_cpus"),
                    I18n.t("output.table_header.disk"),
                    I18n.t("output.table_header.ram")
        ]
        @data.each do |l|
          rows << [ l["name"], l["id"], l["cores"], l["disk"], l["ram"] ]
        end
      when Providers::Openstack
        headers = [
                    I18n.t("output.table_header.id"),
                    I18n.t("output.table_header.virtual_cpus"),
                    I18n.t("output.table_header.disk"),
                    I18n.t("output.table_header.ram")
        ]
        @data.each do |l|
          rows << [ l["id"], l["v_cpus"], l["disk"], l["ram"] ]
        end
      end
      return headers, rows
    end

    def create_networks provider
      headers = nil
      rows = []
      case provider
      when Providers::Openstack
        abort(I18n.t("output.not_found.network.list")) if @data.empty?
        headers = [ I18n.t("output.table_header.name"), I18n.t("output.table_header.cidr") ]
        @data.each do |l|
          rows.push [ l["name"], l["cidr"] ]
        end
      when Providers::Aws
        if @data.empty?
          puts(I18n.t("output.not_found.network.list"))
          return nil, nil
        end
        headers = [
          I18n.t("output.table_header.subnet"),
          I18n.t("output.table_header.vpc_id"),
          I18n.t("output.table_header.cidr"),
          I18n.t("output.table_header.zone")
        ]
        @data.each do |l|
          rows.push [ l["subnetId"], l["vpcId"], l["cidr"], l["zone"] ]
        end
      end
      return headers, rows

    end

    def create_images provider
      abort(I18n.t("output.not_found.image.list")) if @data.empty?
      rows = []
      @data.each {|l| rows.push [ l["name"], l["id"], l["status"] ]}
      headers = [
          I18n.t("output.table_header.name"),
          I18n.t("output.table_header.id"),
          I18n.t("output.table_header.status")
        ]
      return headers, rows
    end

    def create_security_groups provider
      abort(I18n.t("output.not_found.security_groups.list")) if @data.empty?
      headers = [
                  I18n.t("output.table_header.name"),
                  I18n.t("output.table_header.protocol"),
                  I18n.t("output.table_header.from"),
                  I18n.t("output.table_header.to"),
                  I18n.t("output.table_header.cidr"),
                  I18n.t("output.table_header.description")
      ]
      rows = []
      @data.each do |name, v|
        next if v.nil? or v.empty?
        p, f, t, c = [], [], [], []
        v["rules"].map do |l|
          p.push l["protocol"]
          f.push l["from"]
          t.push l["to"]
          c.push l["cidr"]
        end
        rows.push [ name, p.join("\n"), f.join("\n"), t.join("\n"), c.join("\n"), v["description"] ]
      end
      return headers, rows
    end

    def create_vpc
      abort(I18n.t("output.not_found.vpc.list")) if @data.empty?
      headers_and_rows(@data, %w(vpc_id cidr))
    end

  end
end
