"""Get lists Email Delivery account Service """
# :license: MIT, see LICENSE for more details.

import click

from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.managers.account import AccountManager
from SoftLayer.managers.email import EmailManager
from SoftLayer import utils


@click.command()
@environment.pass_env
def cli(env):
    """Lists Email Delivery Service """
    manager = AccountManager(env.client)
    email_manager = EmailManager(env.client)
    result = manager.get_network_message_delivery_accounts()

    table = formatting.KeyValueTable(['name', 'value'])

    table_information = formatting.KeyValueTable(['id', 'username', 'hostname', 'description', 'vendor'])
    table_information.align['id'] = 'r'
    table_information.align['username'] = 'l'

    for email in result:
        table_information.add_row([email.get('id'), email.get('username'), email.get('emailAddress'),
                                   utils.lookup(email, 'type', 'description'),
                                   utils.lookup(email, 'vendor', 'keyName')])

        overview_table = _build_overview_table(email_manager.get_account_overview(email.get('id')))
        statistics = email_manager.get_statistics(email.get('id'))

        table.add_row(['email information', table_information])
        table.add_row(['email overview', overview_table])
        for statistic in statistics:
            table.add_row(['statistics', _build_statistics_table(statistic)])

    env.fout(table)


def _build_overview_table(email_overview):
    table = formatting.Table(['name', 'value'])
    table.align['name'] = 'r'
    table.align['value'] = 'l'

    table.add_row(['creditsAllowed', email_overview.get('creditsAllowed')])
    table.add_row(['creditsRemain', email_overview.get('creditsRemain')])
    table.add_row(['package', email_overview.get('package')])
    table.add_row(['reputation', email_overview.get('reputation')])
    table.add_row(['requests', email_overview.get('requests')])

    return table


def _build_statistics_table(statistics):
    table = formatting.Table(['name', 'value'])
    table.align['name'] = 'r'
    table.align['value'] = 'l'

    table.add_row(['delivered', statistics.get('delivered')])
    table.add_row(['requests', statistics.get('requests')])
    table.add_row(['bounces', statistics.get('bounces')])
    table.add_row(['opens', statistics.get('opens')])
    table.add_row(['clicks', statistics.get('clicks')])
    table.add_row(['spam Reports', statistics.get('spamReports')])

    return table

@@ -609,16 +609,76 @@
 alue'%5D)%0A
+    table.align%5B'name'%5D = 'r'%0A    table.align%5B'value'%5D = 'l'
 %0A    tab
@@ -1345,17 +1345,17 @@
 (%5B'email
- 
+_
 informat
@@ -1409,17 +1409,17 @@
 (%5B'email
- 
+_
 overview
@@ -1646,37 +1646,93 @@
 ing.Table(%5B'
-name', 'value
+credit_Allowed', 'credits_Remain', 'package', 'reputation', 'requests
 '%5D)%0A    tabl
@@ -1808,26 +1808,8 @@
 ow(%5B
-'creditsAllowed', 
 emai
@@ -1844,45 +1844,8 @@
 ed')
-%5D)%0A    table.add_row(%5B'creditsRemain'
 , em
@@ -1881,40 +1881,28 @@
 in')
-%5D)
+,
 %0A    
-table.add_row(%5B'package',
+              
  ema
@@ -1931,42 +1931,8 @@
 ge')
-%5D)%0A    table.add_row(%5B'reputation'
 , em
@@ -1965,41 +1965,28 @@
 on')
-%5D)%0A    table.add_row(%5B'requests',
+,%0A                  
  ema
@@ -2103,37 +2103,90 @@
 ing.Table(%5B'
-name', 'value
+delivered', 'requests', 'bounces', 'opens', 'clicks', 'spamReports
 '%5D)%0A    tabl
@@ -2258,29 +2258,16 @@
 dd_row(%5B
-'delivered', 
 statisti
@@ -2289,40 +2289,8 @@
 ed')
-%5D)%0A    table.add_row(%5B'requests'
 , st
@@ -2317,40 +2317,28 @@
 ts')
-%5D)
+,
 %0A    
-table.add_row(%5B'bounces',
+              
  sta
@@ -2363,37 +2363,8 @@
 es')
-%5D)%0A    table.add_row(%5B'opens'
 , st
@@ -2388,39 +2388,28 @@
 ns')
-%5D)
+,
 %0A    
-table.add_row(%5B'clicks',
+              
  sta
@@ -2433,44 +2433,8 @@
 ks')
-%5D)%0A    table.add_row(%5B'spam Reports'
 , st

fix team code review comments