##########################################################################
#
#  Copyright (c) 2015, Image Engine Design Inc. All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#      * Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      * Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials provided with
#        the distribution.
#
#      * Neither the name of John Haddon nor the names of
#        any other contributors to this software may be used to endorse or
#        promote products derived from this software without specific prior
#        written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
#  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################

import re
import inspect

import IECore
import Gaffer
import GafferImage

__currentlyLoadingScript = None

def __getSerialisedGafferVersion( scriptNode ) :
	
	return (
		Gaffer.Metadata.nodeValue( scriptNode, "serialiser:milestoneVersion" ),
		Gaffer.Metadata.nodeValue( scriptNode, "serialiser:majorVersion" ),
		Gaffer.Metadata.nodeValue( scriptNode, "serialiser:minorVersion" ),
		Gaffer.Metadata.nodeValue( scriptNode, "serialiser:patchVersion" )
		)


def __convertFormat( fmt ):

	if not __currentlyLoadingScript :
		return fmt

	gafferVersion = __getSerialisedGafferVersion( __currentlyLoadingScript )

	# TODO : Determine which version of Gaffer inclusive image bounds
	# will be included in. Presuming 0.16.0.0 for now.
	if ( gafferVersion < ( 0, 16, 0, 0 ) ) :
		displayWindow = fmt.getDisplayWindow()
		displayWindow.max += IECore.V2i( 1 )

		return GafferImage.Format( displayWindow, fmt.getPixelAspect() )

	return fmt

def __FormatPlug__setValue( self, *args, **kwargs ) :

	if args and isinstance( args[0], GafferImage.Format ) :
		args = ( __convertFormat( args[0] ), ) + args[1:]

	return self.__originalSetValue( *args, **kwargs )

def __Format__registerFormat( *args, **kwargs ) :

	if args and isinstance( args[0], GafferImage.Format ) :
		args = ( __convertFormat( args[0] ), ) + args[1:]

	return GafferImage.Format.__originalRegisterFormat( *args, **kwargs )

def __ScriptNode__load( self, *args, **kwargs ) :

	global __currentlyLoadingScript
	__currentlyLoadingScript = self

	try:
		self.__originalLoad( *args, **kwargs )
	finally:
		__currentlyLoadingScript = None

Gaffer.ScriptNode.__originalLoad = Gaffer.ScriptNode.load
Gaffer.ScriptNode.load = __ScriptNode__load

GafferImage.FormatPlug.__originalSetValue = GafferImage.FormatPlug.setValue
GafferImage.FormatPlug.setValue = __FormatPlug__setValue

GafferImage.Format.__originalRegisterFormat = GafferImage.Format.registerFormat
GafferImage.Format.registerFormat = __Format__registerFormat

Format : Add compatibility conversion for old serialised Formats with (0,0) min values.