# slicer imports
from __main__ import vtk, slicer

# python includes
import sys
import time

class Helper(object):
    '''
    classdocs
    '''

    @staticmethod
    def Info(message):
        '''

        '''

        print "[VMTK " + time.strftime("%m/%d/%Y %H:%M:%S") + "]: " + str(message)
        sys.stdout.flush()


    @staticmethod
    def Debug(message):
        '''

        '''

        showDebugOutput = 1
        from time import strftime
        if showDebugOutput:
            print "[VMTK " + time.strftime("%m/%d/%Y %H:%M:%S") + "] DEBUG: " + str(message)
            sys.stdout.flush()

    @staticmethod
    def CreateSpace(n):
        '''
        '''
        spacer = ""
        for s in range(n):
          spacer += " "

        return spacer

    @staticmethod
    def CheckIfVmtkIsInstalled():
        '''
        '''
        vmtkInstalled = True
        try:
            fastMarching = vtkvmtkFastMarchingUpwindGradientImageFilter()
            fastMarching = None
        except Exception:
            vmtkInstalled = False

        return vmtkInstalled

    @staticmethod
    def convertFiducialHierarchyToVtkIdList(hierarchyNode,volumeNode):
        '''
        '''
        outputIds = vtk.vtkIdList()

        if not hierarchyNode or not volumeNode:
            return outputIds

        if isinstance(hierarchyNode,slicer.vtkMRMLMarkupsFiducialNode) and isinstance(volumeNode,slicer.vtkMRMLScalarVolumeNode):

            image = volumeNode.GetImageData()


            # now we have the children which are fiducialNodes - let's loop!
            for n in range(hierarchyNode.GetNumberOfFiducials()):

                currentCoordinatesRAS = [0,0,0]

                # grab the current coordinates
                hierarchyNode.GetNthFiducialPosition(n,currentCoordinatesRAS)

                # convert the RAS to IJK
                currentCoordinatesIJK = Helper.ConvertRAStoIJK(volumeNode,currentCoordinatesRAS)

                # strip the last element since we need a 3based tupel
                currentCoordinatesIJKlist = (int(currentCoordinatesIJK[0]),int(currentCoordinatesIJK[1]),int(currentCoordinatesIJK[2]))
                outputIds.InsertNextId(int(image.ComputePointId(currentCoordinatesIJKlist)))



        # IdList was created, return it even if it might be empty
        return outputIds

    @staticmethod
    def ConvertRAStoIJK(volumeNode,rasCoordinates):
        '''
        '''
        rasToIjkMatrix = vtk.vtkMatrix4x4()
        volumeNode.GetRASToIJKMatrix(rasToIjkMatrix)

        # the RAS coordinates need to be 4
        if len(rasCoordinates) < 4:
            rasCoordinates.append(1)

        ijkCoordinates = rasToIjkMatrix.MultiplyPoint(rasCoordinates)

        return ijkCoordinates


    @staticmethod
    def extractROI(originalVolumeID,newVolumeID,rasCoordinates,diameter):
        '''
        '''

        originalVolume = slicer.mrmlScene.GetNodeByID(originalVolumeID)
        newVolume = slicer.mrmlScene.GetNodeByID(newVolumeID)

        # code below converted from cropVolume module by A. Fedorov
        # optimized after that :)

        inputRASToIJK = vtk.vtkMatrix4x4()
        inputIJKToRAS = vtk.vtkMatrix4x4()
        outputIJKToRAS = vtk.vtkMatrix4x4()
        outputRASToIJK = vtk.vtkMatrix4x4()
        volumeXform = vtk.vtkMatrix4x4()
        T = vtk.vtkMatrix4x4()

        originalVolume.GetRASToIJKMatrix(inputRASToIJK)