#!/usr/bin/env python
#                                                            _
# PACS ToolKit movescu wrapper
#
# (c) 2016-2019 Fetal-Neonatal Neuroimaging & Developmental Science Center
#                   Boston Children's Hospital
#
#              http://childrenshospital.org/FNNDSC/
#                        dev@babyMRI.org
#

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from    argparse            import RawTextHelpFormatter
from    argparse            import ArgumentParser
from    terminaltables      import SingleTable
from    pfmisc._colors      import Colors
import  json
import  pypx
import  socket
import  pudb

str_defIP   = [l for l in (
                [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
                if not ip.startswith("127.")][:1],
                    [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close())
                for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]

str_name    = "px-move"
str_version = "3.0.0"
str_desc    = Colors.CYAN + """



 _ __ __  ___ __ ___   _____   _____
| '_ \\\ \\/ / '_ ` _ \\ / _ \\ \\ / / _ \\
| |_) |>  <| | | | | | (_) \ V /  __/
| .__//_/\_\_| |_| |_|\___/ \_/ \___|
| |
|_|

                        PACS ToolKit Wrapper
                            "move" images

                       -- version """ + \
             Colors.YELLOW + str_version + Colors.CYAN + """ --

    ``px-move`` is a module / script that provides functionality for
    performing a PACS movescu operation.

    ``px-move`` is tested against both Orthanc as well as some
    commercial PACS offerings.

""" + Colors.NO_COLOUR

def synopsis(ab_shortOnly = False):
    scriptName = os.path.basename(sys.argv[0])
    shortSynopsis =  Colors.YELLOW + '''
    NAME

	    %s

        - PACS move (retrieve)

    SYNOPSIS

            _script mode_:
            px-move                                                 \\
                [--aet <AETitle>]                                   \\
                [--aec <CalledAETitle>]                             \\
                [--serverIP <PACSserverIP>]                         \\
                [--serverPort <PACSserverPort>]                     \\
                [--movescu <movescuAbsolutePath>]                   \\
                [--StudyInstanceUID <studyInstanceUID>]             \\
                [--SeriesInstanceUID <seriesInstanceUID>]           \\
                [-x|--desc]                                         \\
                [-y|--synopsis]                                     \\
                [--version]                                         \\
                [--debugToDir <dir>]                                \\
                [--verbosity <level>]

    BRIEF EXAMPLE

        px-move                                                     \\
            --aec CHRIS                                             \\
            --aet FNNDSC-CHRISDEV                                   \\
            --serverIP 134.174.12.21                                \\
            --serverPort 104                                        \\
            --StudyInstanceUID 1.2.3.435                            \\
            --SeriesInstanceUID 2.3.5.6.7

    ''' % scriptName + Colors.NO_COLOUR

    description = Colors.LIGHT_GREEN + '''

        [--aet <AETitle>]
        The AETitle of *this* entity.

        [--aec <CalledAETitle>]
        The called AETitle of *this* entity. Needed for some PACS systems.

        [--serverIP <PACSserverIP>]
        The IP of the PACS server.

        [--serverPort <PACSserverPort>]
        The port associated with the PACS server.

        [--movescu <movescuAbsolutePath>]
        The absolute location of the 'movescu' executable.

        [--StudyInstanceUID <studyInstanceUID>]
        The <studyInstanceUID> to request.

        [--SeriesDescription <seriesInstanceUID>]
        The <seriesInstanceUID> to request.

        [-x|--desc]
        Provide an overview help page.

        [-y|--synopsis]
        Provide a synopsis help summary.

        [--version]
        Print internal version number and exit.

        [--debugToDir <dir>]
        A directory to contain various debugging output -- these are typically
        JSON object strings capturing internal state. If empty string (default)
        then no debugging outputs are captured/generated. If specified, then
        ``pfcon`` will check for dir existence and attempt to create if
        needed.

        [-v|--verbosity <level>]
        Set the verbosity level. "0" typically means no/minimal output. Allows for
        more fine tuned output control as opposed to '--quiet' that effectively
        silences everything.

    EXAMPLES

        px-move                                                     \\
            --aet CHIPS                                             \\
            --aec ORTHANC                                           \\
            --serverIP 127.0.0.1                                    \\
            --serverPort 104                                        \\
            --StudyInstanceUID 1.2.3.435                            \\
            --SeriesInstanceUID 2.3.5.6.7
    ''' + Colors.LIGHT_PURPLE + '''

    DOCKERIZED EXAMPLES

        docker run  --rm -ti                                            \\
            -P 10402:10402                                              \\
            -v /tmp:/dicom                                              \\
            fnndsc/pypx                                                 \\
            --px-move                                                   \\
            --aet CHIPS                                                 \\
            --aec ORTHANC                                               \\
            --serverIP  10.72.76.155                                    \\
            --serverPort 4242                                           \\
            --colorize dark                                             \\
            --StudyInstanceUID 1.2.3.435                                \\
            --SeriesInstanceUID 2.3.5.6.7


    ''' + Colors.NO_COLOUR

    if ab_shortOnly:
        return shortSynopsis
    else:
        return shortSynopsis + description


parser = ArgumentParser(
            description         = str_desc,
            formatter_class     = RawTextHelpFormatter
        )

# PACS settings
parser.add_argument(
    '--aet',
    action  = 'store',
    dest    = 'aet',
    type    = str,
    default = 'CHRIS-ULTRON-AET',
    help    = 'aet')
parser.add_argument(
    '--aec',
    action  = 'store',
    dest    = 'aec',
    type    = str,
    default = 'CHRIS-ULTRON-AEC',
    help    = 'aec')
parser.add_argument(
    '--serverIP',
    action  = 'store',
    dest    = 'serverIP',
    type    = str,
    default = '192.168.1.110',
    help    = 'PACS server IP')
parser.add_argument(
    '--serverPort',
    action  = 'store',
    dest    = 'serverPort',
    type    = str,
    default = '4242',
    help    = 'PACS server port')
parser.add_argument(
    '--movescu',
    action  = 'store',
    dest    = 'movescu',
    type    = str,
    default = '/usr/bin/movescu',
    help    = '"movescu"" executable absolute location')


# Query settings
parser.add_argument(
    '--SeriesInstanceUID',
    action  = 'store',
    dest    = 'SeriesInstanceUID',
    type    = str,
    default = '',
    help    = 'Series Instance UID')
parser.add_argument(
    '--StudyInstanceUID',
    action  = 'store',
    dest    = 'StudyInstanceUID',
    type    = str,
    default = '',
    help    = 'Study Instance UID')

parser.add_argument(
    "-v", "--verbosity",
    help    = "verbosity level for app",
    dest    = 'verbosity',
    type    = int,
    default = 1)
parser.add_argument(
    "-x", "--desc",
    help    = "long synopsis",
    dest    = 'desc',
    action  = 'store_true',
    default = False
)
parser.add_argument(
    "-y", "--synopsis",
    help    = "short synopsis",
    dest    = 'synopsis',
    action  = 'store_true',
    default = False
)
parser.add_argument(
    '--version',
    help    = 'if specified, print version number',
    dest    = 'b_version',
    action  = 'store_true',
    default = False
)


args        = parser.parse_args()

if args.desc or args.synopsis:
    print(str_desc)
    if args.desc:
        str_help     = synopsis(False)
    if args.synopsis:
        str_help     = synopsis(True)
    print(str_help)
    sys.exit(1)

if args.b_version:
    print("Version: %s" % str_version)
    sys.exit(1)

opts = parser.parse_args()
output = pypx.move(vars(opts))
print(output)