"""
SARNet
Author:      Cameron Stauffer
Last Edited: December 05, 2019

PyQT Sweep Module
"""

import sys
import time
from random import random
from compassbeta1 import getHeading
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# global variables

LINES = []
            
class Worker(QThread):
    updateAngle = pyqtSignal(float)
    
    def __init__(self):
        QThread.__init__(self)
        

    """
    def run(self):
        
        while True:
            
            
            #heading = getHeading()
            #heading
            
            
            heading_file = open('/mnt/rd/testdoc', 'r')          
            heading = heading_file.read()
            heading_file.close()
                
            if not heading:
                pass
            else:
                heading = int(heading)
                print('Heading Read: ',heading)
                self.updateAngle.emit(heading)
                print("Compass Reading: "+str(heading))
            time.sleep(0.25)
            """
class CompassWidget(QWidget):

    angleChanged = pyqtSignal(float)
    
    LINES = []
    
    def __init__(self, parent = None):
    
        QWidget.__init__(self, parent)
        
        self._angle = 0.0
        self._margins = 10
        self._pointText = {0: "N", 45: "NE", 90: "E", 135: "SE", 180: "S",
                            225: "SW", 270: "W", 315: "NW"}
        """
        self._worker = Worker()
        self._worker.updateAngle.connect(self.setAngle)
        self._worker.start()
        """
        self.pixmap = QPixmap(self.size())
        self.pixmap.fill(Qt.transparent)
        
        self.max = 4
        self.cursor = 0
        self.count = 0
        self.RSSIlines = []
        self.show()
        
        self.paint = False
        
        timer = QTimer(self)
        timer.timeout.connect(self.onTimeout)
        timer.start(1000)
        
        
    def paintEvent(self, event):
     

        painter = QPainter(self)
        
        painter.begin(self)
        painter.setRenderHint(QPainter.Antialiasing)
            
        painter.fillRect(event.rect(), self.palette().brush(QPalette.Window))
        
        self.drawMarkings(painter)
        
        if self.paint:
            self.drawReadings(painter)
        
        painter.end()
        
        
    def onTimeout(self):
        self.paint = True
        self.update()
        
    
    def drawsetpbystep(self, painter):
        if self.cursor < self.max:
            painter.drawLine(*LINES[self.cursor])
            self.cursor += 1
        self.paint = False
        
   
    def drawMarkings(self, painter):
     
        darkThemeFlag_file = open('/home/pi/Documents/SARNet/GUI/darkThemeFlag')
        darkThemeFlag = darkThemeFlag_file.readline()
        
        painter.save()
        painter.translate(self.width()/2, self.height()/2)
        scale = min((self.width() - self._margins)/120.0,
                    (self.height() - self._margins)/120.0)
        painter.scale(scale, scale)
             
        font = QFont(self.font())
        font.setPixelSize(10)
        metrics = QFontMetricsF(font)
             
        painter.setFont(font)
        painter.setPen(self.palette().color(QPalette.Shadow))
             
        i = 0
        while i < 360:
             
            if i % 45 == 0:
                painter.drawLine(0, 0, 0, -50)
                painter.drawText(-metrics.width(self._pointText[i])/2.0, -52,
                                    self._pointText[i])
            else:
                painter.drawLine(0, -45, 0, -50)
                 
            painter.rotate(15)
            i += 15           
         
        painter.restore()
     
        
    def drawReadings(self, painter):
     
        painter.save()
        painter.translate(self.width()/2, self.height()/2)
        #painter.rotate(self._angle)
        scale = min((self.width() - self._margins)/120.0,
                    (self.height() - self._margins)/120.0)
        painter.scale(scale, scale)
        
        painter.setPen(QColor(255,0,0))
        #painter.setBrush(self.palette().brush(QPalette.Shadow))
        
        actDevID_file = open('/home/pi/Documents/SARNet/GUI/activeDevID', 'r')
        print('Opened activeDeviceID')
        actDevID = actDevID_file.readline()
        print('Read activeDeviceID: ',actDevID)
        
        RSSI_file = open('/home/pi/Documents/SARNet/GUI/RSSIuser_'+actDevID+'.txt')
        print('Opened RSSIuser_'+actDevID+'.txt')
        RSSIread = RSSI_file.readline()
        print('Read RSSIuser_'+actDevID+'.txt')
            
        amplitude = (((int(RSSIread) + 50)/2)+40)*-1
        
        self.RSSIlines.append(amplitude)

        print('Rotated '+str(self.angle)+' degrees')
        print('RSSIlines contents: ',self.RSSIlines[self.count])
        print('Count: ',self.count)
        for i in range(self.count):
            f = self.RSSIlines[i] + 5
            painter.drawLine(0, 0, f, self.RSSIlines[i])
            painter.rotate(self._angle)
        print('Drew '+str(self.count)+' lines')
        self._angle = 0
        
        self.count += 1

        print('Drew a line at an amplitude of '+str(amplitude)+' pix')
        print('Angle: ',self._angle)
        self._angle -= 15
        if self._angle == -360:
            self._angle = 0
        print('Angle: ',self._angle)
        print('*******************************************************')
        
        
        painter.restore()
        
        
    def drawSweepRings(self, painter):
        painter.save()
        painter.translate(self.width()/2, self.height()/2)
        painter.rotate(self._angle)
        scale = min((self.width() - self._margins)/120.0,
                    (self.height() - self._margins)/120.0)
        painter.scale(scale, scale)
        
        ### this draws the circles that make the sweep rings ###
        radx1 = 40;radx2 = 25;radx3 = 10; radx4 = 2
        rady1 = 40;rady2 = 25;rady3 = 10; rady4 = 2
        
        center = QPoint(0, 0)
        painter.drawEllipse(center, radx1, rady1)
        
        center = QPoint(0, 0)
        painter.drawEllipse(center, radx2, rady2)
        
        center = QPoint(0, 0)
        painter.drawEllipse(center, radx3, rady3)
        
        center = QPoint(0, 0)
        painter.drawEllipse(center, radx4, rady4)
        painter.end()
        
            
    def sizeHint(self):
     
        return QSize(150, 150)
    
     
    def angle(self):
        return self._angle
    
    
    @pyqtSlot(float)
    def setAngle(self, angle):
   
       if angle != self._angle:
            self._angle = angle
            self.angleChanged.emit(angle)
            self.update()

    angle = pyqtProperty(float, angle, setAngle)
    
 
if __name__ == "__main__":
        
    app = QApplication(sys.argv)  
    window = QWidget()
    compass = CompassWidget()
    spinBox = QSpinBox()
    spinBox.setRange(0, 359)

    #spinBox.valueChanged.connect(compass.setAngle)
    
    layout = QVBoxLayout()
    layout.addWidget(compass)
    #layout.addWidget(spinBox)
    window.setLayout(layout)
    
    window.setGeometry(506, 263, 300, 215)
    
    window.show()
    sys.exit(app.exec_())