#!/usr/bin/python3.4
# -*-coding:Utf-8 -*
'''module to manage preferences of the script'''
import xml.etree.ElementTree as xmlMod
from save import *
from Preferences.VersionList import *
from Preferences.Output import *
from Preferences.Tiles import *
from Preferences.PresetList.PresetList import *
import os

class Preferences:
	'''class dedicated to script preferences settings'''
	
	
	def __init__(self, xml= None):
		'''initialize preferences object with default value or values extracted from an xml object'''
		if xml is None:
			self.defaultInit()
		else:
			self.fromXml(xml)
	
	
	
	
	
	def defaultInit(self):
		'''initialize preferences object with default value'''
		
		self.blenderVersion = VersionList()
		self.output = Output()
		self.tiles = Tiles()
		self.presets = PresetList()
		self.port = 55814
	
	
	
	
	
	def fromXml(self, xml):
		'''initialize preferences object with values extracted from an xml object'''
		
		self.blenderVersion = VersionList( xml.find('versionsList') )
		self.output = Output( xml.find('output') )
		self.tiles = Tiles(xml.find('tilesSet'))
		self.presets = PresetList(xml.find('presetList'))
		
		self.port = int(xml.find('port').get('value'))
	
	
	
	
	
	def toXml(self):
		'''export preferences settings into xml syntaxed string'''
		xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
		
		xml += '<preferences>\n'
		
		# export blender version list
		xml += self.blenderVersion.toXml()
		
		# export output path
		xml+= self.output.toXml()
		
		# export tiles sizes
		xml+= self.tiles.toXml()
		
		# export preset settings
		xml+= self.presets.toXml()
		
		xml+= '<port value="'+str(self.port)+'" />'
		
		xml += '</preferences>\n'
		
		return xml
	
	
	
	
	
	def menu(self, log, tasks):
		'''method to see preferences settings and access edition menu'''
		log.menuIn('Preferences')
		change = False
		
		while True:
			#print log and preferences
			
			log.print()
			print('''\n    \033[4mPreferences Menu :\033[0m
1- Blender versions
2- Output Path
3- Tiles
4- Presets
9- Change Net Port
0- Save and quit

''')
			
			
		
			#treat available actions
			choice= input('menu?').strip().lower()
			if choice in ['0', 'q', 'quit', 'cancel']:
				log.menuOut()# quit preferences menu
				return
			elif choice == '1':
				change = self.blenderVersion.menu(log, self)
			elif choice == '2':
				change = self.output.menu(log)
			elif choice == '3':
				change = self.tiles.menu(log)
			elif choice == '4':
				change = self.presets.menu(log, self.blenderVersion, tasks)
			else:
				log.error('Unknow request!', False)
			
			if change:
				change = False
				savePreferences(self)
				log.write('New preferences saved')
	
	
	
	
	
	
	
	
	
	
	

@@ -2503,16 +2503,71 @@
  tasks)%0A
+%09%09%09elif choice == '9':%0A%09%09%09%09change = self.editPort(log)%0A
 %09%09%09else:
@@ -2711,28 +2711,140 @@
 ved')%0A%09%0A%09%0A%09%0A%09%0A%09%0A
+%09def editPort(self, log):%0A%09%09'''A method to change the net port to communicate with blender instance'''%0A%09%09%0A%09%0A%09%0A%09%0A
 %09%0A%09%0A%09%0A%09%0A%09%0A%09%0A

add method to edit net port to use