# -*- coding: utf-8 -*-

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import uno
import inspect
import locale

from com.sun.star.beans import PropertyValue


class LanguageInit:
	language = "IT_"
	
	def __init__(self):
		ctx = XSCRIPTCONTEXT.getComponentContext()
		smgr = ctx.getServiceManager()

		oConfigProv = smgr.createInstance("com.sun.star.configuration.ConfigurationProvider")
		prop = []
		arg =  PropertyValue(
			Name = "nodepath",
			Value = "org.openoffice.Setup/L10N/"
		)
		prop.append(arg)
		getRegContent = oConfigProv.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", prop)

		language = getRegContent.getByName("ooLocale").upper() + "_"
		self.IT_TitleFirstPage = "Imposta Prima Pagina"
		self.IT_OptionLabel1 = "Imposta la Prima Pagina e la successiva con lo stile Prefefinito"
		self.IT_OptionLabel2 = "Imposta tre pagine: 1) Prima pagina, 2) Stile predefinito, 3) Pagina destra"
		self.IT_OptionLabel3 = "Imposta la Prima Pagina, la seconda con lo stile Prefefinito e le successive saranno Pagina Destra/Sinistra"
		self.IT_CancelLabel = "Annulla"
		self.IT_TitleInformation = "Informazioni"
		self.IT_labelInformation = "Per qualsiasi informazione o segnalazione potete contattare:"
		self.IT_labelEmail = "antonio.faccioli@studiostorti.com"
		self.IT_heightPage = "Altezza"
		self.IT_widthPage = "Larghezza"
		self.IT_infoGroup = "Informazioni sullo stile applicato su questa pagina:"

		self.EN_TitleFirstPage = "Set First Page"
		self.EN_OptionLabel1 = "Set the First Page and the next with the Standard style"
		self.EN_OptionLabel2 = "Sets three pages: 1) First, 2) Standard style, 3) Right page"
		self.EN_OptionLabel3 = "Set the First Page, the second with the Standard style and later will be left/right page"
		self.EN_CancelLabel = "Cancel"
		self.EN_TitleInformation = "Information"
		self.EN_labelInformation = "You can contact for any information or report:"
		self.EN_labelEmail = "antonio.faccioli@studiostorti.com"
		self.EN_heightPage = "Height page"
		self.EN_widthPage = "Width page"
		self.EN_infoGroup = "Style information applied to this page:"
		
		self.DE_TitleFirstPage = "Set Erste Seite"
		self.DE_OptionLabel1 = "Legen Sie die erste Seite und die nächste mit dem Standard-Stil"
		self.DE_OptionLabel2 = "Setzt drei seiten: 1) Zuerst 2) Standard-Stil, 3) Rechte Seite"
		self.DE_OptionLabel3 = "Legen Sie die erste Seite, die zweite mit dem Standard-Stil und später nach links / rechts Seite werden"
		self.DE_CancelLabel = "Stornieren"
		self.DE_TitleInformation = "Informationen"
		self.DE_labelInformation = "Sie können für jede Information oder berichten kontaktieren:"
		self.DE_labelEmail = "antonio.faccioli@studiostorti.com"
		self.DE_heightPage = "Höhe Seite"
		self.DE_widthPage = "Breite Seite"
		self.DE_infoGroup = "Style-Informationen auf dieser Seite angewendet:"
		
		self.FR_TitleFirstPage = "Définir la première page"
		self.FR_OptionLabel1 = "Définissez la première page et la suivante avec le style par défaut"
		self.FR_OptionLabel2 = "Définit trois pages: 1) Tout d'abord, 2) le style par défaut, 3) Page de droite"
		self.FR_OptionLabel3 = "Définissez la première page, le second avec le style par défaut et la page suivante sera gauche/droite"
		self.FR_CancelLabel = "Annuler"
		self.FR_TitleInformation = "Information"
		self.FR_labelInformation = "Vous pouvez contacter pour toute information ou rapport:"
		self.FR_labelEmail = "antonio.faccioli@studiostorti.com"
		self.FR_heightPage = "Page Hauteur"
		self.FR_widthPage = "Page Largeur"
		self.FR_infoGroup = "Des informations de style appliqué à cette page:"
		
		self.ES_TitleFirstPage = "Impuesto primera página"
		self.ES_OptionLabel1 = "Coloque la primera página y la siguiente con el estilo Estándar"
		self.ES_OptionLabel2 = "Establece tres páginas: 1) En primer lugar, 2) el estilo por defecto, 3) Página derecha"
		self.ES_OptionLabel3 = "Coloque la primera página, la segunda con el estilo por defecto y más tarde será la izquierda / derecha página"
		self.ES_CancelLabel = "Cancelar"
		self.ES_TitleInformation = "Información"
		self.ES_labelInformation = "Puede ponerse en contacto para cualquier información o informe:"
		self.ES_labelEmail = "antonio.faccioli@studiostorti.com"
		self.ES_heightPage = "Altura de la página"
		self.ES_widthPage = "Ancho de página"
		self.ES_infoGroup = "Información de estilo aplicado a esta página:"
		
		self.PT_TitleFirstPage = "Definir Primeira Página"
		self.PT_OptionLabel1 = "Coloque a primeira página ea próxima com o estilo padrão"
		self.PT_OptionLabel2 = "Define três páginas: 1) primeira página, 2) estilo padrão, 3) Página direita"
		self.PT_OptionLabel3 = "Coloque a primeira página, o segundo com o estilo padrão e, posteriormente, será para a esquerda / direita da página"
		self.PT_CancelLabel = "Cancelar"
		self.PT_TitleInformation = "Informação"
		self.PT_labelInformation = "Você pode entrar em contato para qualquer informação ou relatório:"
		self.PT_labelEmail = "antonio.faccioli@studiostorti.com"
		self.PT_heightPage = "Altura da página"
		self.PT_widthPage = "Largura da página"
		self.PT_infoGroup = "Informações de estilo aplicado a esta página:"
		
	def getLocalString(self, loc):
		return (getattr(self, self.language + loc))


#restituisce la larghezza della pagina
def width():
	ctx = XSCRIPTCONTEXT.getDocument()
	cur = ctx.getCurrentController().getViewCursor()
	
	page = ctx.StyleFamilies.getByName("PageStyles").getByName(cur.PageStyleName)
	return page.Width

#restituisce l'altezza della pagina
def height():
	ctx = XSCRIPTCONTEXT.getDocument()
	cur = ctx.getCurrentController().getViewCursor()
	
	page = ctx.StyleFamilies.getByName("PageStyles").getByName(cur.PageStyleName)
	return page.Height

#restituisce il nome dello stile della pagina
def displayName():
	ctx = XSCRIPTCONTEXT.getDocument()
	cur = ctx.getCurrentController().getViewCursor()
	
	page = ctx.StyleFamilies.getByName("PageStyles").getByName(cur.PageStyleName)
	return page.DisplayName

#resituisce la dialog box con tutte le info della pagina
def info(*args):
	x = LanguageInit()
	ctx = XSCRIPTCONTEXT.getComponentContext()
	smgr = ctx.getServiceManager()
	
	oConfigProv = smgr.createInstance("com.sun.star.configuration.ConfigurationProvider")
	#org.openoffice.Setup/L10N/
	prop = []
	arg =  PropertyValue(
		Name = "nodepath",
		Value = "org.openoffice.Setup/L10N/"
	)
	prop.append(arg)
	getRegContent = oConfigProv.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", prop)
	#writeMetodi(getRegContent)
	sLocale = getRegContent.getByName("ooLocale")
	
	
	#creazione dell'istanza della dialog
	dp = smgr.createInstance("com.sun.star.awt.DialogProvider")
	#creazione della dialog Information
	dlg = dp.createDialog("vnd.sun.star.script:PortraitOrLandscape.Information?location=application")
	#prelevo il controllo infoGroup
	oInfoGrp = dlg.getControl("infoGroup")
	oInfoGrp.Text = x.getLocalString("infoGroup")
	#prelevo il controllo infoStyle
	oinfoStyle = dlg.getControl("infoStyle")
	oinfoStyle.Text = displayName()
	#prelevo il controllo infoSize
	oinfoSize = dlg.getControl("infoSize")
	oinfoSize.Text = ("{labelAltezza}: {altezza} mm - {labelLung}: {lunghezza} mm").format(labelAltezza=x.getLocalString("heightPage"), altezza=height(), labelLung=x.getLocalString("widthPage"), lunghezza=width())
	#prelevo il controllo labelInformation
	oinfoLabel = dlg.getControl("labelInformation")
	#oinfoLabel.Text = ("Per qualsiasi informazione o segnalazione potete contattare:")
	oinfoLabel.Text = x.getLocalString("labelInformation")
	#prelevo il controllo labelEmail
	emailLabel = dlg.getControl("labelEmail")
	emailLabel.Text = "antonio.faccioli@studiostorti.com"
	#visualizzo la dialog
	dlg.execute()


#cambia l'orientamento della pagina
def changeOrientation(*args):
	ctx = XSCRIPTCONTEXT.getDocument()
	cur = ctx.getCurrentController().getViewCursor()
	#mi faccio restituire lo stile della pagina
	page = ctx.StyleFamilies.getByName("PageStyles").getByName(ctx.getCurrentController().getViewCursor().PageStyleName)
	#writeMetodi(page)
	#controllo se è Landscape
	if page.IsLandscape:
		#se si applico lo stile Orrizzontale
		styleApply(0)
	else:
		#altrimenti applico verticale
		styleApply(9)
	return None
	
	cur = ctx.getCurrentController()
	#prelevo il frame
	#frame = cur.getFrame()
	
	#styleName = ctx.StyleFamilies.getByName("PageStyles").getByIndex(indexStyle).DisplayName
	
def writeMetodi(obj):
	#nomeFile = obj.__class__.__name__
	#module = obj.__class__.__module__
	#if module is None or module == str.__class__.__module__:
	#	nomeFile = obj.__class__.__name__  # Avoid reporting __builtin__
	#else:
	#	nomeFile = module + '.' + obj.__class__.__name__
	if inspect.isclass(obj):
		nomeFile = obj.__class__.__name__
	else:
		nomeFile = "OKKK"
		
	f = open("c:\\" + nomeFile + ".txt", "w")
	for prop in dir(obj):
		f.write(prop + "\n")
	f.close()

#funzione che applica l'orientamento
def styleApply(indexStyle):
	ctx = XSCRIPTCONTEXT.getDocument()
	cur = ctx.getCurrentController()
	#prendo il frame
	frame = cur.getFrame()
	#prendo lo stile
	styleName = ctx.StyleFamilies.getByName("PageStyles").getByIndex(indexStyle).DisplayName
	smgr = XSCRIPTCONTEXT.getComponentContext().getServiceManager()
	#prendo di dispatcher 
	dsp = smgr.createInstance("com.sun.star.frame.DispatchHelper")
	#dsp = smgr.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)
	#imposto le proprietà
	prop = []
	arg =  PropertyValue(
		Name = "Template",
		Value = styleName
	)
	arg2 = PropertyValue(
		Name = "Family",
		Value = 8
	)
	prop.append(arg)
	prop.append(arg2)
	#applico lo stile cambiato
	dsp.executeDispatch(frame, ".uno:StyleApply", "", 0, prop)
	return None

def scrivi(*args):
	ctx = XSCRIPTCONTEXT.getDocument()
	cur = ctx.getCurrentController()
	frame = cur.getFrame()
	writeMetodi(frame)


#funzioni che vengono esposte
g_exportedScripts = info, changeOrientation, scrivi