from java.io import File
from javax.swing import JOptionPane

from eu.omp.irap.cassis.database.access import AccessDataBase

#=================================
# check if the 'dirname' exists?
def checkExist(dirname, choice):
	if choice == None:
		dirobj = File(dirname)
		if dirobj.exists():
			msg = "The folder \'" + dirname + "\' already exists. Erase all its contents?"
			title = "Jython script"
			ynQuestion = ["Yes", "No"]
			ret = JOptionPane.showOptionDialog(None, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, None, ynQuestion, msg)
			choice = ret
			if ret == JOptionPane.YES_OPTION:
				erase(dirobj)
		else:
			choice = JOptionPane.YES_OPTION
			dirobj.mkdirs()
	else:
		dirobj = File(dirname)
		if dirobj.exists() == False:
			dirobj.mkdirs()
		
	return choice

#===================
# erase a folder
def erase(dirobj):
	files = dirobj.listFiles()
	if files != None:
		for f in files:
			if f.isDirectory():
				erase(f.getAbsoluteFile())
			f.delete()
	return

#=======================
# define a molecule
def molecule(tag, nmol, fwhm, size, tex=100, tkin=10, name="unknown", database="unknown", collision="-no-"):
	name = AccessDataBase.getDataBaseConnection().getMolName(str(tag))
	#AccessGeneriqueDataBase(AccessDatabase.getInstance()).getMoleculeDescriptionDB(tag).getName()
	dic = {}
	dic["tag"] = tag
	dic["nmol"] = nmol
	dic["fwhm"] = fwhm
	dic["size"] = size
	dic["tex"] = tex
	dic["tkin"] = tkin
	dic["name"] = name
	dic["database"] = database
	dic["collision"] = collision
	return dic

#====================
# define a component
def component(model, vlsr, molecules, n=7.5E22, interacting=True, geometry="slab", tex=100.0, tkin=10.0, size=3):
	dic = {}
	dic["model"] = model
	dic["vlsr"] = vlsr
	dic["molecules"] = molecules
	dic["n"] = n
	dic["interacting"] = interacting
	dic["geometry"] = geometry
	# dic["tex"] = tex
	# dic["tkin"] = tkin
	# dic["size"] = size
	return dic
