import sys
import platform
import random
import time
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime, QMetaObject, QObject, QPoint, QRect,
                            QSize, QTime, QUrl, Qt, QEvent)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QIcon, QKeySequence,
                           QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)
from PySide2.QtWidgets import *
from ui_firstversion import Ui_first_version

# Used to start from zero
counter = 100
temp = 0.0


class FirstVersion(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_first_version()
        self.ui.setupUi(self)

        # start_size = QSize(845, 506)
        # qself.resize(start_size)
        # self.setMinimumSize(start_size)

        # Setting the initial value to be zero
        self.changeMeterValue(100)

        # For removing the title bar and setting the bg color of central widget
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        # Applying the blur effect on velocity meter
        self.blur_velocity = QGraphicsBlurEffect(self)
        self.blur_velocity.setBlurRadius(4)
        self.ui.velocity_meter.setGraphicsEffect(self.blur_velocity)

        # Applying the blur effect on progress meter
        self.blur_progress = QGraphicsBlurEffect(self)
        self.blur_progress.setBlurRadius(4)
        self.ui.pressure_meter.setGraphicsEffect(self.blur_progress)

        # Applying the blur effect on acceleration meter
        self.blur_acceleration = QGraphicsBlurEffect(self)
        self.blur_acceleration.setBlurRadius(4)
        self.ui.acceleration_meter.setGraphicsEffect(self.blur_acceleration)

        # Applying the blur effect on main battery temperature bar
        self.blur_main_battery_temp_bar = QGraphicsBlurEffect(self)
        self.blur_main_battery_temp_bar.setBlurRadius(4)
        self.ui.battery_temp_bar.setGraphicsEffect(self.blur_main_battery_temp_bar)

        # Applying the blur effect on brakes temperature bar
        self.blur_brakes_temp_bar = QGraphicsBlurEffect(self)
        self.blur_brakes_temp_bar.setBlurRadius(4)
        self.ui.brakes_temp_bar.setGraphicsEffect(self.blur_brakes_temp_bar)

        # QTimer ==> START
        self.timer = QtCore.QTimer()
        self.timer2 = QtCore.QTimer()

        # Setting on click events on start and stop buttons
        self.ui.btn_start.clicked.connect(self.startRun)
        self.ui.btn_stop.clicked.connect(self.stopRun)
        self.ui.btn_estop.clicked.connect(self.estopRun)

        # For displaying the UI
        self.show()

    def startRun(self):
        # This line is used to connect to tell the timer which function to be run on loop
        self.timer.timeout.connect(self.progress)
        # The function called in .connect() method will run on loop after the number of milliseconds specified
        # in .start() method.
        self.timer.start(750)
        self.ui.pod_state.setText("ACTIVE")

    def stopRun(self):
        # Making the value zero as soon as the stop button is clicked
        self.changeMeterValue(100)
        self.temperatureMeterValue(0)
        # Stopping the timer when the stop button is clicked
        self.timer.stop()
        self.timer2.stop()
        self.ui.pod_state.setText("GROUND")

    def estopRun(self):
        # Stopping the timer when the Estop button is clicked
        self.timer.stop()
        self.timer2.stop()

    def progress(self):
        global counter

        self.timer2.timeout.connect(self.temperature)
        self.timer2.start(375)

        # Generating a random value of velocity and acceleration
        # For now both are kept same
        velocity = random.randint(0, 100)
        while counter != velocity:
            if counter < velocity:
                # INCREASE COUNTER
                counter += 1
                print(counter)

            elif counter > velocity:
                # DECREASE COUNTER
                counter -= 1
                print(counter)
            self.changeMeterValue(counter)
        print("\nI am in start\n")

    def temperature(self):
        global temp
        new_temp = random.randint(0, 100)

        while temp != new_temp:
            if temp < new_temp:
                temp += 1
                print(temp)

            elif temp > new_temp:
                temp -= 1
                print(temp)
            self.temperatureMeterValue(temp)
        print("Hello !!!!!")

    # def progress(self):
    #     global counter
    #     value = counter
    #     velocity = random.uniform(0, 100)
    #
    #     # SET VALUE TO PROGRESS BAR
    #     self.accelerationAndVelocityMeterValue(value)
    #
    #     # CLOSE SPLASH SCREE AND OPEN APP
    #     if counter <= velocity:
    #         # STOP TIMER
    #         self.timer.stop()
    #
    #     # INCREASE COUNTER
    #     counter -= 1

    def changeMeterValue(self, value):
        acc_and_vel_style_sheet = "border-radius: 80;background-color: qconicalgradient(cx:0.5, cy:0.5, angle: {ANGLE}"\
                                  ", stop:0 rgba(0, 225, 255, 255), stop:{STOP} rgba(0, 9, 11, 255));"

        html_text = '<html><head/><body><p><span style="font-weight:600;">{VALUE}</span></p></body></html>'

        velocity_needle_style_sheet = "border-radius: 80;background-color: qconicalgradient(cx:0.5, cy:0.5, angle:" \
                                      "{ANGLE}, stop:0.994318 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 255));"

        acc_needle_style_sheet = "border-radius:80;background-color: qconicalgradient(cx:0.5, cy:0.5, angle:" \
                                 "{ANGLE}, stop:0.994318 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 255));"

        pressure_style_sheet = "border-radius: 70;background-color: qconicalgradient(cx:0.5, cy:0.5, angle: {ANGLE}," \
                               " stop:0 rgba(0, 225, 255, 255), stop:{STOP} rgba(0, 9, 11, 255));"

        pressure_needle_style_sheet = "border-radius:80;background-color: qconicalgradient(cx:0.5, cy:0.5, " \
                                      "angle:{ANGLE}, stop:0.994318 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 255));"

        display_value = (100 - value) / 100.0
        angle = (1 - display_value) * 360
        angle_for_acc_and_vel = angle - 90

        display_value_pressure = (100 - value) / 200.0
        angle_for_pressure = 180 - display_value_pressure * 360

        new_acc_and_vel_style_sheet = acc_and_vel_style_sheet.replace("{STOP}", str(display_value)).replace("{ANGLE}",
                                                                                                            str(angle_for_acc_and_vel))
        new_html_text = html_text.replace("{VALUE}", str(100 - value))
        new_acc_needle_style_sheet = acc_needle_style_sheet.replace("{ANGLE}", str(angle_for_acc_and_vel))
        new_velocity_needle_style_sheet = velocity_needle_style_sheet.replace("{ANGLE}", str(angle_for_acc_and_vel))
        new_pressure_style_sheet = pressure_style_sheet.replace("{STOP}", str(display_value_pressure)).replace(
            "{ANGLE}", str(angle_for_pressure))
        new_pressure_needle_style_sheet = pressure_needle_style_sheet.replace("{ANGLE}", str(angle_for_pressure))

        self.ui.acceleration_meter.setStyleSheet(new_acc_and_vel_style_sheet)
        self.ui.velocity_meter.setStyleSheet(new_acc_and_vel_style_sheet)
        self.ui.pressure_meter.setStyleSheet(new_pressure_style_sheet)

        self.ui.velocity_value.setText(new_html_text)
        self.ui.acceleration_value.setText(new_html_text)

        self.ui.needle_acceleration.setStyleSheet(new_acc_needle_style_sheet)
        self.ui.needle_velocity.setStyleSheet(new_velocity_needle_style_sheet)
        self.ui.needle_pressure.setStyleSheet(new_pressure_needle_style_sheet)

    def temperatureMeterValue(self, value):
        style_sheet = "background:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:{STOP1} rgba(0, 0, 0, 0)," \
                      " stop:{STOP2} rgba(255, 255, 255, 255),  stop:{STOP3} rgba(255, 255, 255, 0));"
        html_text_battery = '<html><head/><body><p><span style="font-weight:600;">{VALUE}</span></p></body></html>'
        html_text_break = '<html><head/><body><p><span style="font-weight:600;">{VALUE}</span></p></body></html>'

        stop1 = (100 - value) / 100.0
        stop2 = stop1 + 0.002
        stop3 = stop2 + 0.005

        new_style_sheet = style_sheet.replace("{STOP1}", str(stop1)).replace("{STOP2}", str(stop2)).replace(
            "{STOP3}", str(stop3))
        new_html_text_battery = html_text_battery.replace("{VALUE}", str(int(value)) + "°C")
        new_html_text_break = html_text_break.replace("{VALUE}", str(int(value)) + "°C")

        self.ui.brakes_temp_needle.setStyleSheet(new_style_sheet)
        self.ui.battery_temp_needle.setStyleSheet(new_style_sheet)
        self.ui.value_temp_battery.setText(new_html_text_battery)
        self.ui.value_temp_brakes.setText(new_html_text_break)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = FirstVersion()
    sys.exit(app.exec_())
