#!/usr/bin/python3
# why the shebang here, when it's imported?  Can't really be used stand alone, right?  And fermat.py didn't have one...
# this is 4-5 seconds slower on 1000000 points than Ryan's desktop...  Why?
from ConvexHullSolver import ConvexHullSolver
import time
from operator import methodcaller

from which_pyqt import PYQT_VER
if PYQT_VER == 'PYQT5':
	from PyQt5.QtCore import QLineF, QPointF, QThread, pyqtSignal
elif PYQT_VER == 'PYQT4':
	from PyQt4.QtCore import QLineF, QPointF, QThread, pyqtSignal
else:
	raise Exception('Unsupported Version of PyQt: {}'.format(PYQT_VER))


class ConvexHullSolverThread(QThread):
	def __init__( self, unsorted_points, demo):
		self.points = unsorted_points					
		self.pause = demo
		QThread.__init__(self)

	def __del__(self):
		self.wait()

	show_hull = pyqtSignal(list, tuple)
	display_text = pyqtSignal(str)

# some additional thread signals you can implement and use for debugging, if you like
	show_tangent = pyqtSignal(list, tuple)
	erase_hull = pyqtSignal(list)
	erase_tangent = pyqtSignal(list)

	def run(self):
		assert(type(self.points) == list and type(self.points[0]) == QPointF )

		n = len(self.points)
		print( 'Computing Hull for set of {} points'.format(n) )

		convexHullSolver = ConvexHullSolver()

		t1 = time.time()
		# SORT THE POINTS BY INCREASING X-VALUE
		convexHullSolver.sort_points_by_x(self.points)
		t2 = time.time()
		print('Time Elapsed (Sorting): {:3.3f} sec'.format(t2-t1))

		t3 = time.time()
		# COMPUTE THE CONVEX HULL USING DIVIDE AND CONQUER
		complete_hull_and_points = convexHullSolver.compute_hull(self.points, self)
		t4 = time.time()

		USE_DUMMY = False
		if USE_DUMMY:
			# this is a dummy polygon of the first 3 unsorted points
			polygon = [QLineF(self.points[i],self.points[(i+1)%3]) for i in range(3)]
			
			# when passing lines to the display, pass a list of QLineF objects.  Each QLineF
			# object can be created with two QPointF objects corresponding to the endpoints
			assert( type(polygon) == list and type(polygon[0]) == QLineF )
			# send a signal to the GUI thread with the hull and its color
			self.show_hull.emit(polygon,(255,0,0))

		else:
			# PASS THE CONVEX HULL LINES BACK TO THE GUI FOR DISPLAY
			self.show_hull.emit(complete_hull_and_points[0].getLines(), (255, 0, 0))
			
		# send a signal to the GUI thread with the time used to compute the hull
		self.display_text.emit('Time Elapsed (Convex Hull): {:3.3f} sec'.format(t4-t3))
		print('Time Elapsed (Convex Hull): {:3.3f} sec'.format(t4-t3))
			



