"""
symvalues.py --

This file implements the Variable Simulation Values window.

Date of creation: 2010-15-11

Copyright
Julian David Colorado, jd.colorado@upm.es
Universidad Politecnica de Madrid

See the file "license.terms" for information on usage and redistribution of this file, and for a
DISCLAIMER OF ALL WARRANTIES.

"""

import wx
from numpy import *
import os
radstodeg = (180/pi)

class SymValuesWindow( wx.Panel ):
	""" Window where the simulation values are displayed """
	def __init__(self, parent, id ):
		""" Window where the simulation values are displayed """
		wx.Panel.__init__(self, parent, id )

		self.Sizer = wx.BoxSizer( wx.VERTICAL)
		#Window Title.
		font = wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD)
		self.Title = wx.StaticText( self, -1, "Variable Simulation Values", (0,0), style=wx.TE_LEFT )
		self.Title.SetFont( font )

		#Window Panels.
		hbox = wx.BoxSizer(wx.HORIZONTAL)
		self.LeftPanel = wx.Panel(self, -1)
		self.LeftPanel.SetBackgroundColour("white")
		self.RightPanel = wx.Panel(self, -1)
		self.RightPanel.SetBackgroundColour("white")

		#Windows sliders
		self.rot1sld = wx.Slider(self.RightPanel, -1, 0, -180, 180, wx.DefaultPosition, (250, -1), wx.SL_AUTOTICKS | wx.SL_HORIZONTAL | wx.SL_LABELS )
		self.Bind(wx.EVT_SLIDER, self.sliderUpdate)
		self.rot1sld.Hide()
		self.tran1sld = wx.Slider(self.RightPanel, -1, 0, -100.0, 100.0, wx.DefaultPosition, (250, -1), wx.SL_AUTOTICKS | wx.SL_HORIZONTAL | wx.SL_LABELS)
		self.Bind(wx.EVT_SLIDER, self.sliderUpdate)
		self.tran1sld.Hide()

		self.rot2sld = wx.Slider(self.RightPanel, -1, 0, -180, 180, wx.DefaultPosition, (250, -1), wx.SL_AUTOTICKS | wx.SL_HORIZONTAL | wx.SL_LABELS )
		self.Bind(wx.EVT_SLIDER, self.sliderUpdate)
		self.rot2sld.Hide()
		self.tran2sld = wx.Slider(self.RightPanel, -1, 0, -10, 10, wx.DefaultPosition, (250, -1), wx.SL_AUTOTICKS | wx.SL_HORIZONTAL | wx.SL_LABELS)
		self.Bind(wx.EVT_SLIDER, self.sliderUpdate)
		self.tran2sld.Hide()

		#Window Control Tree.
		self.SymValuesTree = SymValuesTree( self.LeftPanel, -1,'','')
		self.SymValuesTree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.SymValuesTree )
		self.display = wx.StaticText( self.RightPanel, -1, '',(10,10), style=wx.ALIGN_CENTRE)

		hbox.Add( self.LeftPanel, 0, wx.EXPAND )
		hbox.Add( self.RightPanel, 1, wx.EXPAND )

		self.Sizer.Add(self.Title, 0, wx.TOP )
		self.Sizer.Add(hbox, 1, wx.EXPAND | wx.TOP )
		self.SetSizer(self.Sizer)
		self.Centre()

	def GetSystemValues( self, system, ren, VTKwindow ):
		"""Gets the loaded system links in order to manipulate them"""
		self.Link1 = system.Link1
		self.Link2 = system.Link2
		self.system = system
		self.ren = ren
		self.VTKwindow = VTKwindow

	def UpdateSlidersValues( self ):
		"""It updates the sliders values from the SymValuesWindow"""

		if self.Link1.Type == "translational":
			value = (int) (self.system.d1*10)
			self.tran1sld.SetValue( value )
		else:
			value = int ( self.system.theta1*radstodeg)
			self.rot1sld.SetValue( value )
		if self.Link2.Type == "translational":
			value = int ( self.system.d2*10 )
			self.tran2sld.SetValue( value )
		else:
			value = int ( self.system.theta2*radstodeg)
			self.rot2sld.SetValue( value )

	def UpdateSliderswValues( self, value1, value2 ):
		"""It updates the sliders values from the SymValuesWindow with parameters values"""

		if self.Link1.Type == "translational":
			self.tran1sld.SetValue( value1*10)
			#self.system.d1 = value1
		else:
			self.rot1sld.SetValue( value1*radstodeg )
			#self.system.theta1 = value1
		if self.Link2.Type == "translational":
			self.tran2sld.SetValue( value2*10 )
			#self.system.d2 = value2
		else:
			self.rot2sld.SetValue( value2*radstodeg )
			#self.system.theta2 = value2

	def OnSelChanged(self, event):
			"""Changes the sliding bar according to the Link Type."""
			item =  event.GetItem()
			self.text = self.SymValuesTree.GetItemText(item)
			#self.SymValuesTree = SymValuesTree( self.LeftPanel, -1,self.Link1.Type,self.Link2.Type)
			#Display slide type according to link type.
			if  self.text == "Link 1":
				if self.Link1.Type == "translational":
					self.tran1sld.Show()
					self.tran2sld.Hide()
					self.rot2sld.Hide()
				else:
					self.rot1sld.Show()
					self.rot2sld.Hide()
					self.tran2sld.Hide()
			elif self.text == "Link 2":
				if self.Link2.Type == "translational":
					self.tran2sld.Show()
					self.tran2sld.SetPosition( (0,0) )
					self.rots1ld.Hide()
					self.tran1sld.Hide()
				else:
					self.rot2sld.Show()
					self.rot2sld.SetPosition( (0,0) )
					self.tran1sld.Hide()
					self.rot1sld.Hide()

	def sliderUpdate(self, event):
		"""Updates Link Value."""
		if  self.text == "Link 1":
			if self.Link1.Type == "translational":
				self.system.updateLinkValue( self.ren, self.VTKwindow, self.tran1sld.GetValue()/10.0, "Link 1" )
			else:
				self.system.updateLinkValue( self.ren, self.VTKwindow, self.rot1sld.GetValue(), "Link 1" )
		else:
			if self.Link2.Type == "translational":
				self.system.updateLinkValue( self.ren, self.VTKwindow, self.tran2sld.GetValue(), "Link 2" )
			else:
				self.system.updateLinkValue( self.ren, self.VTKwindow, self.rot2sld.GetValue(), "Link 2" )

	def HideSlides(self ):
		""" Hides all slides if neccesary"""
		self.tran1sld.Hide()
		self.rot1sld.Hide()
		self.tran2sld.Hide()
		self.rot2sld.Hide()

class SymValuesTree( wx.TreeCtrl ):
	""" Ctrl Tree that stores the system variable values."""
	def __init__( self, parent, id,type1,type2):
		"""Creates the simulation values tree."""
		if (os.name=='nt'):
			wx.TreeCtrl.__init__(self, parent, id, wx.DefaultPosition, wx.Size(100, 80), style=wx.TR_FULL_ROW_HIGHLIGHT | wx.TR_EXTENDED | wx.TR_HIDE_ROOT | wx.TR_HAS_BUTTONS )
		else:
			wx.TreeCtrl.__init__(self, parent, id, wx.DefaultPosition, wx.Size(150, 90), style=wx.TR_FULL_ROW_HIGHLIGHT | wx.TR_EXTENDED | wx.TR_HIDE_ROOT | wx.TR_HAS_BUTTONS )
		root = self.AddRoot('Values')
		varval = self.AppendItem(root, 'Variable Values')
		if(type1!=''):
			if type1== "translational":
				self.link1='Link 1 (cm)'
			elif type1!= "translation":
				self.link1='Link 1 (deg)'
			if type2== "translational":
				self.link2='Link 2 (cm)'
			elif type2!= "translation":
				self.link2='Link 2 (deg)'
			self.AppendItem(varval, self.link1)
			self.AppendItem(varval, self.link2)
		else:
			self.AppendItem(varval, "Link 1")
			self.AppendItem(varval, "Link 2")

		self.Expand(varval)
		#self.Expand(root)
		self.Centre()
