import time

rows = '100'
if demisto.args().has_key('rows'):
    rows = demisto.args()['rows']
days = '1'
if demisto.args().has_key('days'):
    days = demisto.args()['days']

res = '## Exabeam Lockouts'
entry = demisto.executeCommand('xb-lockouts', {'numberOfResults': rows, 'num': days})[0]
if entry['Type'] != entryTypes['error'] and entry['ContentsFormat'] == formats['json']:
    lockouts = demisto.get(entry, 'Contents.lockouts')
    if lockouts:
        res += '\n### Total in the last ' + days + ' days: ' + str(len(lockouts))
        for l in lockouts:
            info = demisto.get(l, 'user.info')
            if info:
                res += '\n\n\n![photo](data:image/png;base64,' +  demisto.get(info, 'photo').replace('\n', '') + ')'
                res += '\n|Account|Email|Name|Title|Department|Devision|DN|Location|Manager|Office|Cell|Group|First Seen|Last Seen|'
                res += '\n|-------|-----|----|-----|----------|--------|--|--------|-------|------|----|-----|----------|---------|'
                res += '\n| ' + demisto.get(info, 'accountId') + ' | ' + demisto.get(info, 'email') + ' | ' + demisto.get(info, 'fullName') + ' | ' + \
                       demisto.get(info, 'title') + ' | ' + demisto.get(info, 'department') + ' | ' + demisto.get(info, 'division') + ' | ' + \
                       demisto.get(info, 'dn') + ' | ' + demisto.get(info, 'location') + ', ' + demisto.get(info, 'country') + ' | ' + \
                       demisto.get(info, 'manager') + ' | ' + demisto.get(info, 'phoneOffice') + ' | ' + demisto.get(info, 'phoneCell') + ' | ' + \
                       demisto.get(info, 'group') + ' | ' + time.ctime(demisto.get(l, 'user.firstSeen') / 1000.0) + ' | ' + time.ctime(demisto.get(l, 'user.lastSeen') / 1000.0) + ' |'
            res += '\nScore: **' + str(demisto.get(l, 'user.riskScore')) + '**'
            res += '\n|ID|Accounts|Start|End|Label|LoginHost|Zones|InitialRisk|Risk|Last Activity Type|Last Activity Time|Account Changes|Account Lockouts|Assets|Events|Failed Logins|Reasons|'
            res += '\n|--|--------|-----|---|-----|---------|-----|-----------|----|------------------|------------------|---------------|----------------|------|------|-------------|-------|'
            lInfo = demisto.get(l, 'lockoutInfo')
            if lInfo:
                res += '\n| ' + demisto.get(lInfo, 'lockoutId') + ' | ' + ','.join(demisto.get(lInfo, 'accounts')) + ' | ' + time.ctime(demisto.get(lInfo, 'startTime') / 1000.0) + ' | ' + \
                       time.ctime(demisto.get(lInfo, 'endTime') / 1000.0) + ' | ' + demisto.get(lInfo, 'label') + ' | ' + demisto.get(lInfo, 'loginHost') + ' | ' + \
                       ','.join(demisto.get(lInfo, 'zones')) + ' | ' + str(demisto.get(lInfo, 'initialRiskScore')) + ' | ' + str(demisto.get(lInfo, 'riskScore')) + ' | ' + \
                       str(demisto.get(lInfo, 'lastActivityType')) + ' | ' + time.ctime(demisto.get(lInfo, 'lastActivityTime') / 1000.0) + ' | ' + \
                       str(demisto.get(lInfo, 'numOfAccountChanges')) + ' | ' + str(demisto.get(lInfo, 'numOfAccountLockouts')) + ' | ' + \
                       str(demisto.get(lInfo, 'numOfAssets')) + ' | ' + str(demisto.get(lInfo, 'numOfEvents')) + ' | ' + str(demisto.get(lInfo, 'numOfFailedLogins')) + ' | ' + \
                       str(demisto.get(lInfo, 'numOfReasons')) + ' |'
    demisto.results({'ContentsFormat': formats['markdown'], 'Type': entryTypes['note'], 'Contents': res})
else:
    demisto.results(entry)
