# module for the <eadheader/> or <control/> portion
import xml.etree.cElementTree as ET
import globals
from messages import error
from mixed_content import mixed_content
import wx

def eadheader(EAD, CSheet):
	eadheader_root = EAD[0]
	
	#update GUI progress bar
	from wx.lib.pubsub import pub
	wx.CallAfter(pub.sendMessage, "update_spread", msg="Reading <eadheader>...")
	
	# <eadheader> @findaidstatus
	if "findaidstatus" in eadheader_root.attrib:
		CSheet.find('DraftStatus').text = eadheader_root.attrib['findaidstatus']
	
	#<eadid> @countrycode and @url
	if 'countrycode' in eadheader_root.find('eadid').attrib:
		CSheet.find('CountryCode').text = eadheader_root.find('eadid').attrib['countrycode']
	if 'url' in eadheader_root.find('eadid').attrib:
		CSheet.find('URL').text = eadheader_root.find('eadid').attrib['url']

	
	#<filedesc>
	if eadheader_root.find('filedesc') is None:
		error("Your EAD finding aid does not contain a <filedesc> tag. Since this is a required element in EAD2002, EADMachine is unable to convert the file. Please correct your EAD file and try again.", True)
		
	#<titlestmt>
	if eadheader_root.find('filedesc/titlestmt') is None:
		error("Your EAD finding aid does not contain a <titlestmt> tag. Since this is a required element in EAD2002, EADMachine is unable to convert the file. Please correct your EAD file and try again.", True)
		titlestmt_element = ET.Element('titlestmt')

	#<titleproper>
	if eadheader_root.find('filedesc/titlestmt/titleproper') is None:
		error("Your EAD finding aid does not contain a <titleproper> tag. Since this is a required element in EAD2002, EADMachine is unable to convert the file. Please correct your EAD file and try again.", True)
		
	#<date>
	if eadheader_root.find('filedesc/titlestmt/titleproper/date') is None:
		pass
	else:
		if 'type' in eadheader_root.find('filedesc/titlestmt/titleproper/date').attrib:
			if eadheader_root.find('filedesc/titlestmt/titleproper/date').attrib['type'].lower() == 'bulk':
				CSheet.find('DateBulk').text = eadheader_root.find('filedesc/titlestmt/titleproper/date').text
				if 'normal' in eadheader_root.find('filedesc/titlestmt/titleproper/date').attrib:
					CSheet.find('DateBulkNormal').text = eadheader_root.find('filedesc/titlestmt/titleproper/date').attrib['normal']
			else:
				CSheet.find('DateInclusive').text = eadheader_root.find('filedesc/titlestmt/titleproper/date').text
				if 'normal' in eadheader_root.find('filedesc/titlestmt/titleproper/date').attrib:
					CSheet.find('DateInclusiveNormal').text = eadheader_root.find('filedesc/titlestmt/titleproper/date').attrib['normal']
		else:
			CSheet.find('DateInclusive').text = eadheader_root.find('filedesc/titlestmt/titleproper/date').text
			if 'normal' in eadheader_root.find('filedesc/titlestmt/titleproper/date').attrib:
				CSheet.find('DateInclusiveNormal').text = eadheader_root.find('filedesc/titlestmt/titleproper/date').attrib['normal']
			
	#<subtitle>
	if eadheader_root.find('filedesc/titlestmt/subtitle') is None:
		if EAD.find('frontmatter/titlepage/subtitle') is None:
			pass
		else:
			CSheet.find('Subtitle').text = EAD.find('frontmatter/titlepage/subtitle').text
	else:
		CSheet.find('Subtitle').text = eadheader_root.find('filedesc/titlestmt/subtitle').text
		
	#<author>
	if eadheader_root.find('filedesc/titlestmt/author') is None:
		if EAD.find('frontmatter/titlepage/author') is None:
			pass
		else:
			CSheet.find('ProcessedBy').text = EAD.find('frontmatter/titlepage/author').text
	else:
		CSheet.find('ProcessedBy').text = eadheader_root.find('filedesc/titlestmt/author').text
	
	#<sponsor>
	if eadheader_root.find('filedesc/titlestmt/sponsor') is None:
		if EAD.find('frontmatter/titlepage/sponsor') is None:
			pass
		else:
			CSheet.find('Sponsor').text = EAD.find('frontmatter/titlepage/sponsor').text
	else:
		CSheet.find('Sponsor').text = eadheader_root.find('filedesc/titlestmt/sponsor').text
	
            
	# Edition Statement Section
	from editionstmt import editionstmt
	editionstmt(eadheader_root, CSheet)
	
	# Publication Statement Section
	from publicationstmt import publicationstmt
	publicationstmt(eadheader_root, CSheet)
	if CSheet.find('Publisher/PublisherName') is None:
		if EAD.find('frontmatter/titlepage/publisher') is None:
			pass
		else:
			if EAD.find('frontmatter/titlepage/publisher').text:
				CSheet.find('Publisher/PublisherName').text = EAD.find('frontmatter/titlepage/publisher').text
	else:
		if CSheet.find('Publisher/PublisherName').text:
			pass
		else:
			if EAD.find('frontmatter/titlepage/publisher') is None:
				pass
			else:
				CSheet.find('Publisher/PublisherName').text = EAD.find('frontmatter/titlepage/publisher').text
	if CSheet.find('PublicationDate').text:
		pass
	else:
		if EAD.find('frontmatter/titlepage/date') is None:
			pass
		else:
			CSheet.find('PublicationDate').text = EAD.find('frontmatter/titlepage/date').text