import os
import sys
import mysql.connector
from mysql.connector import Error
from  PyQt5.QtWidgets  import * 
from  PyQt5.uic  import  loadUi

from  matplotlib.backends.backend_qt5agg  import  ( NavigationToolbar2QT  as  NavigationToolbar )
from PyQt5 import uic, QtCore, QtGui, QtWidgets

import  numpy  as  np 
import  random
     
#class  MatplotlibWidget(QMainWindow):
class  GetChart_plotter(QMainWindow):
        
    def  __init__ (self):
       super().__init__()
       #QMainWindow.__init__(self)
       loadUi("C:\\Python3_11\\Lib\\APT_ControlPanel\\Screens\\004_GetChart_plotter.ui",self)
       #self.setWindowState(QtCore.Qt.WindowMaximized)
       frame_geometry = self.frameGeometry()
       #--->Screan Position at center= sp<---#
       sp = QDesktopWidget().availableGeometry().center()
       frame_geometry.moveCenter(sp)
       self.move(frame_geometry.topLeft())
       self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint)
       self.setWindowTitle("PyQt5 & Matplotlib Example GUI")
       self.key_run.clicked.connect(self.update_graph)
       self.addToolBar(NavigationToolbar(self.MplWidget.canvas,self))
       self.key_exit.setCheckable(True)
       self.key_exit.clicked.connect(self.Exit_screen)
       self.MplWidget.canvas.axes.grid(color = 'gray', linestyle = '--') 


    def  update_graph(self):
       fs  =  500 
       f  =  random.randint(1, 100) 
       ts = 1/fs 
       length_of_signal = 100 
       t = np.linspace(0, 1, length_of_signal)
        
       cosine_signal = np.cos(2*np.pi*f*t) 
       sinus_signal = np.sin (2*np.pi*f*t)

       self.MplWidget.canvas.axes.clear()
       self.MplWidget.canvas.axes.grid(color = 'gray', linestyle = '--') 
       self.MplWidget.canvas.axes.plot(t, cosine_signal) 
       self.MplWidget.canvas.axes.plot(t, sinus_signal) 
       self.MplWidget.canvas.axes.legend (('cosinus','sinus'),loc = 'upper right') 
       self.MplWidget.canvas.axes.set_title('Cosinus - Sinus Signal') 
       self.MplWidget.canvas.draw()
           
    def Exit_screen(self):
       self.close()
       os._exit(0)    

""" if __name__ == "__main__":

   app = QtWidgets.QApplication(sys.argv)
   SC002_GetChart_plotte = QtWidgets.QMainWindow()
   #ui = GetChart_plotter()
   #ui.__init__(SC002_GetChart_plotter)
   SC002_GetChart_plotte.show()
   #sys.exit(app.exec_()) """

# app  =  QApplication([]) 
# window  =  GetChart_plotter() 
# window.show() 
# app.exec_()

"""import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import tmp102

# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []

# Initialize communication with TMP102
tmp102.init()

# This function is called periodically from FuncAnimation
def animate(i, xs, ys):

    # Read temperature (Celsius) from TMP102
    temp_c = round(tmp102.read_temp(), 2)

    # Add x and y to lists
    xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
    ys.append(temp_c)

    # Limit x and y lists to 20 items
    xs = xs[-20:]
    ys = ys[-20:]

    # Draw x and y lists
    ax.clear()
    ax.plot(xs, ys)

    # Format plot
    plt.xticks(rotation=45, ha='right')
    plt.subplots_adjust(bottom=0.30)
    plt.title('TMP102 Temperature over Time')
    plt.ylabel('Temperature (deg C)')

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()"""