from math import floor

__Key_Angle_Step = 1.0   #degrees

#################################################################
## Default library and output module (for simple script output
#################################################################
__Key_Default_Library = None
__Key_Current_Library = None

__Key_Layer_List = None

__Key_Text_Style = 1

#----------------------------------------------------------------------------
#Set library and output
#----------------------------------------------------------------------------

def get_current_library():
	return __Key_Current_Library

def set_current_library(library):
	global __Key_Current_Library
	__Key_Current_Library = library

def get_current_output ():
	return Key_Current_Output

def set_current_output (output):
	global Key_Current_Output
	Key_Current_Output = output

def get_current_layerlist():
	return __Key_Layer_List

def set_current_layerlist(layerlist):
	global __Key_Layer_List
	__Key_Layer_List = layerlist



#----------------------------------------------------------------------------
# Getting and Setting library variables
#----------------------------------------------------------------------------

# deprecated: get vallue directly from library
def get_grid(library = None):
	if library == None: library = get_current_library()
	return library.grid

# deprecated: get vallue directly from library
def get_unit(library = None):
	if library == None: library = get_current_library()
	return library.unit

# deprecated: set value directly in library
def set_grid (grid_step , library = None):
	if library == None: library = get_current_library()
	library.set_grid(grid_step)

# deprecated: set value directly in library
def set_unit (unit ,library = None):
	if library == None: library = get_current_library()
	library.set_unit(unit)

def get_grids_per_unit(library = None):
	if library == None: library = get_current_library()
	return library.grids_per_unit

#round a distance to a grid value
# deprecated: set value directly in library
def snap_value (value, grids_per_unit = None):
	if grids_per_unit == None: grids_per_unit = get_grids_per_unit()
	return floor(value * grids_per_unit + 0.5) / (grids_per_unit)

from .geometry.coord import coord2
def snap_coordinate(coordinate, grids_per_unit = None):
	if grids_per_unit == None: grids_per_unit = get_grids_per_unit()
	return coord2(floor(coordinate[0] * grids_per_unit +0.5) / (grids_per_unit),
		      floor(coordinate[1] * grids_per_unit + 0.5) / (grids_per_unit))

def snap_shape(coordinates, grids_per_unit = None):
	if grids_per_unit == None: grids_per_unit == get_grids_per_unit()
	from .geometry.shape import gds_shape
	sh = gds_shape(coordinates).snap_to_grid(grids_per_unit)
	return sh

#----------------------------------------------------------------------------
#Setting global variables
#----------------------------------------------------------------------------

def get_angle_step ():
	return __Key_Angle_Step

def set_angle_step (step = __Key_Angle_Step):
	global __Key_Angle_Step
	__Key_Angle_Step = step

def get_text_style ():
	return __Key_Text_Style

def set_text_style (style):
	global __Key_Text_Style
	__Key_Text_Style = style

