
#import FileUtils
import logging
import scribus
from AgGeometry import *
import Attributes as attr
import AgDraw

logr = logging.getLogger()

class AgDragPreview():
	def __init__(self):
		self.previewItems = {}  # sla import preview
		self.highlight_article = -1

	# placementBoxStates of active article
	def display_neo_drag(self, itemsGroupDict, pointed_article, drag, placementBoxStates=None):
		# logr.debug("display_neo_drag")
		# only Anygraaf compiled scribus 1.5.4 has getMousePosition function
		try:
			if pointed_article:
				# logr.debug("display_neo_drag:"+str(pointed_article)+" placementBoxStates:"+str(placementBoxStates))
				if drag and self.highlight_article:
					if pointed_article != int(self.highlight_article):
						if self.highlight_article > -1:
							self._paint_group_clear_article_ex(itemsGroupDict)  # color=None
						if placementBoxStates:
							self._doc_group_highlight_ex(pointed_article, False, itemsGroupDict, placementBoxStates)
				if not drag and self.highlight_article:
					if self.highlight_article > -1:
						self._paint_group_clear_article_ex(itemsGroupDict)  # color=None
					if placementBoxStates:
						self._doc_group_highlight_ex(pointed_article, False, itemsGroupDict, placementBoxStates)
			else:
				if drag and self.highlight_article > 0:  # clear highlight
					self._paint_group_clear_article_ex(itemsGroupDict)  # color=None  _paint_group_clear(False)
					self.highlight_article = -1
				if not drag and self.highlight_article > -1:
					self._paint_group_clear_article_ex(itemsGroupDict)  # color=None _paint_group_clear(False)
					self.highlight_article = -1

			logr.debug("display_neo_drag DONE")

		except Exception as e:
			logr.warning("display_neo_drag exception:" + str(e))


	def _doc_group_highlight_ex(self, articleNum, allPages, itemsGroupDict, placementBoxStates = None):
		#if placementBoxStates:
		imgCount = AgDraw._placementImgCount(placementBoxStates)
		logr.debug('_doc_group_highlight_ex placementBoxStates:'+str(len(placementBoxStates))+' article:'+str(articleNum)+' itemsGroupDict:'+str(len(itemsGroupDict))+" imgCount:"+str(imgCount))
		try:
			if str(articleNum) in itemsGroupDict:
				itemnames = itemsGroupDict[str(articleNum)]	# doc items
				for itemname in itemnames:
					green = False
					isImage = False
					if placementBoxStates:
						for pb in placementBoxStates:
							if pb.itemName and pb.itemName==itemname and pb.haveBoxInDocument:
								green = True
								if pb.isImage:
									isImage = True

					rect = AgDraw._insertHighlightRect(itemname, green)
					self.previewItems[rect] = rect
					if isImage:
						line, line2 = AgDraw._insertHighlightImgCross(itemname, green)
						self.previewItems[line] = line
						self.previewItems[line2] = line2

					if not isImage:
						AgDraw._insert_stylename_title(itemname)

			logr.debug("_doc_group_highlight_ex done")
			time.sleep(0.08)
			scribus.redrawAll() # crash
			#logr.debug("_doc_group_highlight_ex done2")
			self.highlight_article = articleNum
			#logr.debug("_doc_group_highlight_ex done3")

		except Exception as e:
			logr.warning('_doc_group_highlight exception:'+str(e))

	def _paint_group_clear_article_ex(self, itemsGroupDict):
		#logr.debug('_paint_group_clear_article_ex:' + str(self.highlight_article))
		if self.highlight_article == -1:
			return
		itemnames = itemsGroupDict[str(self.highlight_article)]  # doc items
		for itemname in itemnames:
			AgDraw._remove_stylename_title(itemname)

		for itemname, obj in self.previewItems.items():
			if scribus.objectExists(itemname):
				scribus.deleteObject(itemname)
		self.previewItems.clear()
