#!/usr/bin/env python3

import syd
import click
import os
import sys

# ------------------------------------------------------------------
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])


@click.command(context_settings=CONTEXT_SETTINGS)
@click.option('--patient_id', '-p', default=None, help='Id of a specific patient')
@click.option('--db', default='', help='Db filename (SYD_DB_FILENAME environment variable is used if no filename')
def timeline(db, patient_id):
    db_filename = syd.get_db_filename(db)
    db = syd.open_db(db_filename)
    if patient_id is not None:
        try:
            patient = syd.find(db['Patient'], id=patient_id)
        except:
            print('Could not find patient in the database')
    else:
        patient = syd.find_all(db['Patient'])

    for p in patient:
        res = []
        print('|-Patient', p['name'], 'id :', p['id'])
        inj = syd.find(db['Injection'], patient_id=p['id'])
        inj = syd.sort_elements(inj, 'date')
        for i in inj:
            e = syd.find_acquisition(db, i)
            res.append([i, e])
        for r in res:
            radionuclide = syd.find_one(db['Radionuclide'], id=r[0]['radionuclide_id'])
            print('|')
            print('|----Injection', r[0]['date'], 'id :', r[0]['id'], radionuclide['name'])
            for e in r[1]:
                if r[0] != res[-1][0]:
                    print('|    |')
                    print('|    |----Acquisition', e['acquisition']['date'], 'id:', e['acquisition']['id'],
                          '-->' 'Listmode :', len(e['listmode']), 'DicomSeries :', len(e['dicom_serie']),
                          'DicomStruct :', len(e['dicom_struct']))
                    print('|    |    |')
                    for d in e['dicom_serie']:
                        im = syd.find_one(db['Image'], dicom_series_id=d['id'])
                        if im is not None:
                            print('|    |    |----DicomSeries', d['acquisition_date'], 'id :', d['id'],
                                  d['series_description'], 'Image :', im['id'])
                        else:
                            print('|    |    |----DicomSeries', d['acquisition_date'], 'id :', d['id'],
                                  d['series_description'])

                else:
                    print('     |')
                    print('     |----Acquisition', e['acquisition']['date'], 'id :', e['acquisition']['id'],
                          '-->' 'Listmode :', len(e['listmode']), 'DicomSeries :', len(e['dicom_serie']),
                          'DicomStruct :', len(e['dicom_struct']))
                    print('     |    |')
                    for d in e['dicom_serie']:
                        print('     |    |----DicomSeries', d['acquisition_date'], 'id :', d['id'],
                              d['series_description'])
        print('')
        print('-------------------------------------------------------------------------------------------------------',)
        print('')


# -----------------------------------------------------------------------------
if __name__ == '__main__':
    timeline()
