import sys
import numpy as np

from PySide6.QtWidgets import (
    QApplication, QMainWindow, QWidget,
    QLabel, QLineEdit, QPushButton,
    QVBoxLayout, QHBoxLayout
)
from PySide6.QtGui import QPixmap
from PySide6.QtCore import Qt

from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("PS-Statikprogramm")
        self.resize(1100, 750)

        zentral_widget = QWidget()
        self.setCentralWidget(zentral_widget)

        hauptlayout = QHBoxLayout()
        zentral_widget.setLayout(hauptlayout)

        # -------------------
        # LINKE SEITE
        # -------------------
        links_widget = QWidget()
        links_layout = QVBoxLayout()
        links_widget.setLayout(links_layout)

        system_label = QLabel("System")
        system_label.setAlignment(Qt.AlignCenter)
        system_label.setStyleSheet("font-size: 16px; font-weight: bold;")
        links_layout.addWidget(system_label)

        bild_label = QLabel()
        pixmap = QPixmap("C:/Users/pauls/iCloudDrive/01_Studium/02_M.Sc/22_WS2526/16_Python Course/Project/Bilder/einfeld2-1.gif")
        bild_label.setPixmap(pixmap.scaled(320, 180, Qt.KeepAspectRatio))
        bild_label.setAlignment(Qt.AlignCenter)
        links_layout.addWidget(bild_label)

        links_layout.addSpacing(10)

        eingabe_titel = QLabel("Eingaben")
        eingabe_titel.setAlignment(Qt.AlignCenter)
        eingabe_titel.setStyleSheet("font-weight: bold;")
        links_layout.addWidget(eingabe_titel)

        self.q_eingabe = QLineEdit()
        self.q_eingabe.setPlaceholderText("Streckenlast q [kN/m]")
        links_layout.addWidget(self.q_eingabe)

        self.l_eingabe = QLineEdit()
        self.l_eingabe.setPlaceholderText("Länge l [m]")
        links_layout.addWidget(self.l_eingabe)

        button = QPushButton("Berechnen")
        button.clicked.connect(self.berechnen)
        links_layout.addWidget(button)

        links_layout.addStretch()

        # -------------------
        # RECHTE SEITE
        # -------------------
        rechts_widget = QWidget()
        rechts_layout = QVBoxLayout()
        rechts_widget.setLayout(rechts_layout)

        titel = QLabel("Ergebnisse")
        titel.setAlignment(Qt.AlignCenter)
        titel.setStyleSheet("font-size: 16px; font-weight: bold;")
        rechts_layout.addWidget(titel)

        self.moment_label = QLabel("Max. Moment: -")
        self.lager_links_label = QLabel("Lager links: -")
        self.lager_rechts_label = QLabel("Lager rechts: -")

        rechts_layout.addWidget(self.moment_label)
        rechts_layout.addWidget(self.lager_links_label)
        rechts_layout.addWidget(self.lager_rechts_label)

        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        rechts_layout.addWidget(self.canvas)

        hauptlayout.addWidget(links_widget, 1)
        hauptlayout.addWidget(rechts_widget, 1)

    def berechnen(self):
        try:
            q = float(self.q_eingabe.text())
            l = float(self.l_eingabe.text())

            m_max = q * l**2 / 8
            r = q * l / 2

            self.moment_label.setText(f"Max. Moment: {m_max:.2f} kNm")
            self.lager_links_label.setText(f"Lager links: {r:.2f} kN")
            self.lager_rechts_label.setText(f"Lager rechts: {r:.2f} kN")

            x = np.linspace(0, l, 200)
            qv = r - q * x
            mv = q * x * (l - x) / 2

            self.figure.clear()

            # -------------------
            # Auflagerkräfte
            # -------------------
            ax0 = self.figure.add_subplot(311)
            ax0.set_title("Auflagerkräfte")

            ax0.plot([0, l], [0, 0])

            ax0.arrow(0, -1, 0, 0.8, head_width=0.3, color="blue")
            ax0.arrow(l, -1, 0, 0.8, head_width=0.3, color="blue")

            ax0.text(0, -1.2, f"{r:.2f} kN", ha="center")
            ax0.text(l, -1.2, f"{r:.2f} kN", ha="center")

            ax0.set_xlim(-1, l + 1)
            ax0.set_yticks([])
            ax0.set_xticks([0, l])
            ax0.set_xticklabels(["A", "B"])

            # -------------------
            # QUERKRAFT
            # -------------------
            ax1 = self.figure.add_subplot(312)

            ax1.plot(x, qv, color="black")

            ax1.fill_between(x, qv, 0, where=(qv >= 0), color="blue", alpha=0.4)
            ax1.fill_between(x, qv, 0, where=(qv < 0), color="red", alpha=0.4)

            ax1.axhline(0)
            ax1.set_title("Querkraftverlauf")
            ax1.set_ylabel("Q [kN]")
            ax1.grid(True)

            # -------------------
            # MOMENT
            # -------------------
            ax2 = self.figure.add_subplot(313)

            # nach unten darstellen
            m_plot = -mv

            ax2.plot(x, m_plot, color="black")

            ax2.fill_between(x, m_plot, 0, where=(mv >= 0), color="blue", alpha=0.4)
            ax2.fill_between(x, m_plot, 0, where=(mv < 0), color="red", alpha=0.4)

            ax2.axhline(0)
            ax2.set_title("Momentenverlauf")
            ax2.set_xlabel("x [m]")
            ax2.set_ylabel("M [kNm]")
            ax2.grid(True)

            self.figure.tight_layout()
            self.canvas.draw()

        except ValueError:
            self.moment_label.setText("Fehler bei Eingabe")
            self.figure.clear()
            self.canvas.draw()


app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())